tripal_core.chado_nodes.properties.api.inc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. <?php
  2. /**
  3. * @file
  4. * API to manage the chado prop table for various Tripal Node Types
  5. *
  6. * How To Use:
  7. * @code
  8. function chado_example_form($form, $form_state) {
  9. // Default values for form elements can come in the following ways:
  10. //
  11. // 1) as elements of the $node object. This occurs when editing an existing node
  12. // 2) in the $form_state['values'] array which occurs on a failed validation or
  13. // ajax callbacks when the ajax call originates from non-submit fields other
  14. // than button
  15. // 3) in the $form_state['input'] array which occurs on ajax callbacks from submit
  16. // form elements (e.g. buttons) and the form is being rebuilt but has not yet
  17. // been validated
  18. //
  19. // The reference elements added by this function do use AJAX calls from buttons,
  20. // therefore, it is important to check for form values in the $form_state['values']
  21. // for case #2 above, and in the $form_state['input'] for case #3.
  22. // See the chado analysis node form for an example.
  23. // Next, add in all the form array definition particular to your node type
  24. // To add in the chado properties form elements, you first need to prepare the arguments
  25. // for the function call.
  26. $details = array(
  27. 'property_table' => 'example_property', // the name of the table linking additional properties to this node
  28. 'chado_id_field' => 'example_id', // key to link to the chado content created by this node
  29. 'chado_id' => $example_id, // the value of the above key
  30. 'cv_name' => 'example_prop_cv', // the name of the cv governing the _prop.type_id
  31. 'fieldset_title' => 'Additional References', // the non-translated title for this fieldset
  32. 'additional_instructions' => '' // a non-stranslated string providing additional instructions
  33. );
  34. // Finally, and add the additional form elements to the form
  35. chado_add_node_form_properties($form, $form_state, $details);
  36. return $form;
  37. }
  38. function chado_example_insert($node) {
  39. // if there is an example_id in the $node object then this must be a sync so
  40. // we can skip adding the chado_example as it is already there, although
  41. // we do need to proceed with the rest of the insert
  42. if (!property_exists($node, 'example_id')) {
  43. // Add record to chado example table
  44. // Add to any other tables needed
  45. // Add all properties
  46. // Existing _property links will be cleared and then re-added
  47. tripal_api_chado_node_properties_form_update_properties(
  48. $node, // the node object passed in via hook_insert()
  49. 'example_property', // the name of the _property linking table
  50. 'example', // the name of the base chado table for the node
  51. 'example_id', // key to link to the chado content created by this node
  52. $node->example_id // value of the above key
  53. );
  54. }
  55. // Add record to chado_example linking example_id to new node
  56. }
  57. function chado_example_update($node) {
  58. // Update record in chado example table
  59. // Update any other tables needed
  60. // Update all properties
  61. // Existing _property links will be cleared and then re-added
  62. tripal_api_chado_node_properties_form_update_properties(
  63. $node, // the node object passed in via hook_insert()
  64. 'example_property', // the name of the _property linking table
  65. 'example', // the name of the base chado table for the node
  66. 'example_id', // key to link to the chado content created by this node
  67. $node->example_id // value of the above key
  68. );
  69. // Don't need to update chado_example linking table since niether example_id or nid can be changed in update
  70. }
  71. * @endcode
  72. *
  73. * @ingroup tripal_chado_node_api
  74. */
  75. /**
  76. * Provides a form for adding to BASEprop table
  77. *
  78. * @param $form
  79. * The Drupal form array into which the property form elements will be added
  80. * @param $form_state
  81. * The corresponding form_state array for the form
  82. * @param $details
  83. * An array defining details used by this form.
  84. * Required keys that are always required:
  85. * - property_table: the name of the property table (e.g.: featureprop, stockprop, etc.)
  86. * Required keys for forms that update a record.
  87. * - chado_id: the id of the record to which properties will be associated (e.g.: if a
  88. * feature has a feature_id of 999 and we want to associate properties for that feature
  89. * then the chado_id option should be 999)
  90. * Require ONE of the following to identify the controlled vocabulary containing the properties to use:
  91. * -cv_id: the unique key from the cv table
  92. * -cv_name: the cv.name field uniquely identifying the controlled vocabulary
  93. * Optional keys include:
  94. * - chado_id_field: the foreign key field that links properties to the
  95. * chado_id record. If this value is not specified it is determined using the
  96. * traditional Chado naming scheme for property tables.
  97. *
  98. * @ingroup tripal_chado_node_api
  99. */
  100. function chado_add_node_form_properties(&$form, &$form_state, $details) {
  101. // make sure the property table exists before proceeding.
  102. if (!chado_table_exists($details['property_table'])) {
  103. drupal_set_message("Cannot add property elements to the form. The property table, '" .
  104. $details['property_table'] . "', does not exists", "error");
  105. tripal_report_error('tcprops_form', TRIPAL_ERROR, "Cannot add property elements to the form.
  106. The property table, '%name', cannot be found.", array('%name' => $details['property_table']));
  107. return;
  108. }
  109. // if the chado_id_field is not specified then set it using the
  110. // typical chado naming scheme
  111. if (!array_key_exists('chado_id_field', $details)) {
  112. $chado_id_table = preg_replace('/prop$/', '', $details['property_table']);
  113. $chado_id_field = $chado_id_table . '_id';
  114. $details['chado_id_field'] = $chado_id_field;
  115. }
  116. // make sure the specified cv exists
  117. if (isset($details['cv_name'])) {
  118. // make sure the cv_name is real
  119. $result = chado_select_record('cv',array('cv_id'),array('name' => $details['cv_name']));
  120. if (count($result) == 0) {
  121. drupal_set_message("Cannot add property elements to the form. The CV name, '" .
  122. $details['cv_name'] . "', does not exists", "error");
  123. tripal_report_error('tcprops_form', TRIPAL_ERROR, "Cannot add property elements to the form.
  124. The CV named, '%name', cannot be found.", array('%name' => $details['cv_name']));
  125. return;
  126. }
  127. // add the cv_id option to the details array
  128. $details['cv_id'] = $result[0]->cv_id;
  129. }
  130. elseif (isset($details['cv_id'])) {
  131. // make sure the cv_id is real
  132. $result = chado_select_record('cv', array('name'), array('cv_id' => $details['cv_id']));
  133. if (count($result) == 0) {
  134. drupal_set_message("Cannot add property elements to the form. The CV ID, '" .
  135. $details['cv_id'] . "', does not exist", "error");
  136. tripal_report_error('tcprops_form', TRIPAL_ERROR, "Cannot add property elements
  137. to the form. The CV ID, '%id', cannot be found.", array('%id' => $details['cv_id']));
  138. return;
  139. }
  140. // add the cv_name option to the details array
  141. $details['cv_name'] = $result[0]->name;
  142. }
  143. else {
  144. drupal_set_message("Please provide either a 'cv_name' or 'cv_id' as an
  145. option for adding properties to the form", "error");
  146. tripal_report_error('tcprops_form', TRIPAL_ERROR, "Please provide either a
  147. 'cv_name' or 'cv_id' as an option for adding properties to the form", array());
  148. return;
  149. }
  150. // Set Defaults for optional fields
  151. $details['fieldset_title'] = 'Properties';
  152. $details['additional_instructions'] = '';
  153. // Get Property Types for the Select List
  154. if (isset($details['select_options'])) {
  155. $property_options = $details['select_options'];
  156. }
  157. // if the select options are not provided then try to get them on our own
  158. else {
  159. // if the vocabulary name is provided in the details then use that to
  160. // get the terms
  161. if (isset($details['cv_name'])) {
  162. $property_options = array();
  163. $property_options[] = 'Select a Property';
  164. $sql = "
  165. SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition, CV.cv_id as cv_id
  166. FROM {cvterm} CVT
  167. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  168. WHERE
  169. CV.name = :cv_name AND
  170. NOT CVT.is_obsolete = 1
  171. ORDER BY CVT.name ASC
  172. ";
  173. $prop_types = chado_query($sql, array(':cv_name' => $details['cv_name']));
  174. while ($prop = $prop_types->fetchObject()) {
  175. $property_options[$prop->cvterm_id] = $prop->name;
  176. }
  177. }
  178. // if the cv_id is set in the $details array then use that to get the terms
  179. elseif (isset($details['cv_id'])) {
  180. $property_options = array();
  181. $property_options[] = 'Select a Property';
  182. $sql = "
  183. SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition, CV.name as cv_name
  184. FROM {cvterm} CVT
  185. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  186. WHERE
  187. CV.cv_id = :cv_id AND
  188. NOT CVT.is_obsolete = 1
  189. ORDER BY CVT.name ASC
  190. ";
  191. $prop_types = chado_query($sql, array(':cv_id' => $details['cv_id']));
  192. while ($prop = $prop_types->fetchObject()) {
  193. $property_options[$prop->cvterm_id] = $prop->name;
  194. }
  195. }
  196. }
  197. // Tell tripal administrators how to add terms to the property types drop down.
  198. if (empty($property_options)) {
  199. $tripal_message = tripal_set_message(
  200. t('There are currently no proeprty types! To add additional properties to the drop
  201. down list, you need to <a href="@cvtermlink">add a controlled vocabulary term</a>
  202. to the %cv_name controlled vocabulary.',
  203. array(
  204. '%cv_name' => $details['cv_name'],
  205. '@cvtermlink' => url('admin/tripal/chado/tripal_cv/cv/' . $details['cv_id'] . '/cvterm/add')
  206. )
  207. ),
  208. TRIPAL_WARNING,
  209. array('return_html' => TRUE)
  210. );
  211. }
  212. else {
  213. $tripal_message = tripal_set_message(
  214. t('To add additional properties to the drop down list, you need to <a href="@cvtermlink">add
  215. a controlled vocabulary term</a> to the %cv_name controlled vocabulary.',
  216. array(
  217. '%cv_name' => $details['cv_name'],
  218. '@cvtermlink' => url('admin/tripal/chado/tripal_cv/cv/' . $details['cv_id'] . '/cvterm/add')
  219. )
  220. ),
  221. TRIPAL_INFO,
  222. array('return_html' => TRUE)
  223. );
  224. }
  225. // the fieldset of the property elements
  226. $form['properties'] = array(
  227. '#type' => 'fieldset',
  228. '#title' => t($details['fieldset_title']),
  229. '#description' => t('You may add additional properties by selecting a property type
  230. from the dropdown and adding text. You may add as many properties as desired by
  231. clicking the add button on the right. To remove a property, click the remove button.'
  232. . $details['additional_instructions']),
  233. '#prefix' => "<div id='properties-fieldset'>",
  234. '#suffix' => '</div>',
  235. '#weight' => 8
  236. );
  237. $form['properties']['admin_message'] = array(
  238. '#type' => 'markup',
  239. '#markup' => $tripal_message
  240. );
  241. // this form element is a tree, so that we don't puke all of the values into then node variable
  242. // it is set as a tree, and keeps them in the $form_state['values']['property_table'] heading.
  243. $form['properties']['property_table'] = array(
  244. '#type' => 'markup',
  245. '#tree' => TRUE,
  246. '#prefix' => '<div id="tripal-generic-edit-properties-table">',
  247. '#suffix' => '</div>',
  248. '#theme' => 'chado_node_properties_form_table'
  249. );
  250. // Add defaults into form_state to be used elsewhere
  251. $form['properties']['property_table']['details'] = array(
  252. '#type' => 'hidden',
  253. '#value' => serialize($details)
  254. );
  255. /* Properties can come to us in two ways:
  256. *
  257. * 1) In the form state in the $form_state['chado_properties']. Data is in this field
  258. * when an AJAX call updates the form state or a validation error.
  259. *
  260. * 2) Directly from the database if the record already has properties associated. This
  261. * data is only used the first time the form is loaded. On AJAX calls or validation
  262. * errors the fields on the form are populated from the $form_state['chado_properties']
  263. * entry.
  264. */
  265. if (isset($form_state['chado_properties'])) {
  266. $existing_properties = $form_state['chado_properties'];
  267. }
  268. else {
  269. if (isset($details['cv_name'])) {
  270. $existing_properties = chado_query(
  271. "SELECT PP.".$details['property_table']."_id property_id, CVT.cvterm_id as type_id, CVT.name as type_name, CVT.definition, PP.value, PP.rank
  272. FROM {" . $details['property_table'] . "} PP
  273. INNER JOIN {cvterm} CVT ON CVT.cvterm_id = PP.type_id
  274. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  275. WHERE
  276. PP." . $details['chado_id_field'] . " = :chado_id AND
  277. CV.name = '" .$details['cv_name']. "'
  278. ORDER BY CVT.name, PP.rank",
  279. array(':chado_id' => $details['chado_id'])
  280. );
  281. } elseif (isset($details['cv_id'])) {
  282. $existing_properties = chado_query(
  283. "SELECT PP.".$details['property_table']."_id property_id, CVT.cvterm_id as type_id, CVT.name as type_name, CVT.definition, PP.value, PP.rank
  284. FROM {" . $details['property_table'] . "} PP
  285. INNER JOIN {cvterm} CVT ON CVT.cvterm_id = PP.type_id
  286. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  287. WHERE
  288. PP." . $details['chado_id_field'] . " = :chado_id AND
  289. CV.cv_id = '" .$details['cv_id']. "'
  290. ORDER BY CVT.name, PP.rank",
  291. array(':chado_id' => $details['chado_id'])
  292. );
  293. }
  294. }
  295. /* The format of the $existing_properties array is either:
  296. *
  297. * From the chado_properties array:
  298. * $form_state['chado_properties'] = array(
  299. * '[type_id]-[rank]' => array(
  300. * 'type_id' => [the cvterm.cvterm_id value]
  301. * 'type_name' => [the cvterm.name value]
  302. * 'property_id' => [the property.property_id value, or NULL if it doesn't yet exist],
  303. * 'value' => [the BASEprop.value value],
  304. * 'rank' => [the BASEprop.rank value],
  305. * ),
  306. * );
  307. *
  308. * OR
  309. * Populated from the database:
  310. * $existing_property = array(
  311. * 0 => array(
  312. * 'property_id' => [the property.property_id value, or NULL if it doesn't yet exist],
  313. * 'type_id' => [the cvterm.cvterm_id value]
  314. * 'type_name' => [the cvterm.name value]
  315. * 'value' => [the BASEprop.value value],
  316. * 'rank' => [the BASEprop.rank value],
  317. * ),
  318. * );
  319. *
  320. * NOTE: The main difference is the key
  321. *
  322. * Loop on the array elements of the $existing_properties array and add
  323. * an element to the form for each one as long as it's also in the
  324. * $properties_options array.
  325. */
  326. foreach ($existing_properties as $property) {
  327. if (array_key_exists($property->type_id, $property_options)) {
  328. $form['properties']['property_table'][$property->type_id]['#type'] = 'markup';
  329. $form['properties']['property_table'][$property->type_id]['#value'] = '';
  330. $form['properties']['property_table'][$property->type_id][$property->rank]['#type'] = 'markup';
  331. $form['properties']['property_table'][$property->type_id][$property->rank]['#value'] = '';
  332. $form['properties']['property_table'][$property->type_id][$property->rank]['prop_type_id'] = array(
  333. '#type' => 'hidden',
  334. '#value' => $property->type_id
  335. );
  336. $form['properties']['property_table'][$property->type_id][$property->rank]['prop_value'] = array(
  337. '#type' => 'hidden',
  338. '#value' => $property->value
  339. );
  340. $form['properties']['property_table'][$property->type_id][$property->rank]['property_id'] = array(
  341. '#type' => 'hidden',
  342. '#value' => $property->property_id
  343. );
  344. $form['properties']['property_table'][$property->type_id][$property->rank]['type'] = array(
  345. '#type' => 'markup',
  346. '#markup' => $property->type_name
  347. );
  348. $form['properties']['property_table'][$property->type_id][$property->rank]['value'] = array(
  349. '#type' => 'markup',
  350. '#markup' => $property->value
  351. );
  352. $form['properties']['property_table'][$property->type_id][$property->rank]['rank'] = array(
  353. '#type' => 'markup',
  354. '#markup' => $property->rank
  355. );
  356. // remove button
  357. $form['properties']['property_table'][$property->type_id][$property->rank]['property_action'] = array(
  358. '#type' => 'submit',
  359. '#value' => t('Remove'),
  360. '#name' => "property_remove-".$property->type_id.'-'.$property->rank,
  361. '#ajax' => array(
  362. 'callback' => "chado_add_node_form_properties_ajax_update",
  363. 'wrapper' => 'tripal-generic-edit-properties-table',
  364. 'effect' => 'fade',
  365. 'method' => 'replace',
  366. 'prevent' => 'click'
  367. ),
  368. // When this button is clicked, the form will be validated and submitted.
  369. // Therefore, we set custom submit and validate functions to override the
  370. // default node form submit. In the validate function we validate only the
  371. // property fields and in the submit we remove the indicated property
  372. // from the chado_properties array. In order to keep validate errors
  373. // from the node form validate and Drupal required errors for non-property fields
  374. // preventing the user from removing properties we set the #limit_validation_errors below
  375. '#validate' => array('chado_add_node_form_properties_remove_button_validate'),
  376. '#submit' => array('chado_add_node_form_properties_remove_button_submit'),
  377. // Limit the validation of the form upon clicking this button to the property_table tree
  378. // No other fields will be validated (ie: no fields from the main form or any other api
  379. // added form).
  380. '#limit_validation_errors' => array(
  381. array('property_table') // Validate all fields within $form_state['values']['property_table']
  382. )
  383. );
  384. }
  385. }
  386. // Form elements for adding a new property
  387. //---------------------------------------------
  388. $form['properties']['property_table']['new'] = array(
  389. '#type' => 'markup',
  390. '#prefix' => '<span class="addtl-properties-add-new-property">',
  391. '#suffix' => '</span>'
  392. );
  393. $form['properties']['property_table']['new']['type'] = array(
  394. '#type' => 'select',
  395. '#options' => $property_options, // Set at top of form
  396. );
  397. $form['properties']['property_table']['new']['value'] = array(
  398. '#type' => 'textarea',
  399. '#rows' => 1,
  400. );
  401. // add button
  402. $form['properties']['property_table']['new']['property_action'] = array(
  403. '#type' => 'submit',
  404. '#value' => t('Add'),
  405. '#name' => "property-add",
  406. '#ajax' => array(
  407. 'callback' => "chado_add_node_form_properties_ajax_update",
  408. 'wrapper' => 'tripal-generic-edit-properties-table',
  409. 'effect' => 'fade',
  410. 'method' => 'replace',
  411. 'prevent' => 'click'
  412. ),
  413. // When this button is clicked, the form will be validated and submitted.
  414. // Therefore, we set custom submit and validate functions to override the
  415. // default node form submit. In the validate function we validate only the
  416. // additional property fields and in the submit we add them to the chado_properties
  417. // array. In order to keep validate errors from the node form validate and Drupal
  418. // required errors for non-property fields preventing the user from adding properties we
  419. // set the #limit_validation_errors below
  420. '#validate' => array('chado_update_node_form_properties_add_button_validate'),
  421. '#submit' => array('chado_add_node_form_properties_add_button_submit'),
  422. // Limit the validation of the form upon clicking this button to the property_table tree
  423. // No other fields will be validated (ie: no fields from the main form or any other api
  424. // added form).
  425. '#limit_validation_errors' => array(
  426. array('property_table') // Validate all fields within $form_state['values']['property_table']
  427. )
  428. );
  429. }
  430. /**
  431. * Validate the user input for creating a new property
  432. * Called by the add button in chado_add_node_form_properties
  433. *
  434. * @ingroup tripal_core
  435. */
  436. function chado_update_node_form_properties_add_button_validate($form, &$form_state) {
  437. // Ensure the type_id is supplied & Valid
  438. $cvterm = chado_select_record(
  439. 'cvterm',
  440. array('cvterm_id', 'name'),
  441. array('cvterm_id' => $form_state['values']['property_table']['new']['type'])
  442. );
  443. if (!isset($cvterm[0])) {
  444. form_set_error('property_table][new][cvterm', 'Please select a property type before attempting to add a new property.');
  445. }
  446. else {
  447. $form_state['values']['property_table']['new']['type_name'] = $cvterm[0]->name;
  448. }
  449. // Ensure value is supplied
  450. if (empty($form_state['values']['property_table']['new']['value'])) {
  451. form_set_error('property_table][new][value','You must enter the property value before attempting to add a new property.');
  452. }
  453. }
  454. /**
  455. * Called by the add button in chado_add_node_form_properties
  456. *
  457. * Create an array of properties in the form state. This array will then be
  458. * used to rebuild the form in subsequent builds
  459. *
  460. * @ingroup tripal_core
  461. */
  462. function chado_add_node_form_properties_add_button_submit(&$form, &$form_state) {
  463. $details = unserialize($form_state['values']['property_table']['details']);
  464. // if the chado_additional_properties array is not set then this is the first time modifying the
  465. // property table. this means we need to include all the properties from the db
  466. if (!isset($form_state['chado_properties'])) {
  467. chado_add_node_form_properties_create_property_formstate_array($form, $form_state);
  468. }
  469. // get details for the new property
  470. $property = array(
  471. 'type_id' => $form_state['values']['property_table']['new']['type'],
  472. 'type_name' => $form_state['values']['property_table']['new']['type_name'],
  473. 'property_id' => NULL,
  474. 'value' => $form_state['values']['property_table']['new']['value'],
  475. 'rank' => '0',
  476. );
  477. // get max rank
  478. $rank = chado_get_table_max_rank(
  479. $details['property_table'],
  480. array(
  481. $details['chado_id_field'] => $details['chado_id'],
  482. 'type_id' => $property['type_id']
  483. )
  484. );
  485. $property['rank'] = strval($rank + 1);
  486. $key = $property['type_id'] . '-' . $property['rank'];
  487. $form_state['chado_properties'][$key] = (object) $property;
  488. $form_state['rebuild'] = TRUE;
  489. }
  490. /**
  491. * There is no user input for the remove buttons so there is no need to validate
  492. * However, both a submit & validate need to be specified so this is just a placeholder
  493. *
  494. * Called by the many remove buttons in chado_add_node_form_properties
  495. *
  496. * @ingroup tripal_core
  497. */
  498. function chado_add_node_form_properties_remove_button_validate($form, $form_state) {
  499. // No Validation needed for remove
  500. }
  501. /**
  502. * Remove the correct property from the form
  503. * Called by the many remove buttons in chado_add_node_form_properties
  504. *
  505. * @ingroup tripal_core
  506. */
  507. function chado_add_node_form_properties_remove_button_submit(&$form, &$form_state) {
  508. // if the chado_properties array is not set then this is the first time modifying the
  509. // property table. this means we need to include all the properties from the db
  510. if (!isset($form_state['chado_properties'])) {
  511. chado_add_node_form_properties_create_property_formstate_array($form, $form_state);
  512. }
  513. // remove the specified property from the form property table
  514. if(preg_match('/property_remove-([^-]+-[^-]+)/',$form_state['triggering_element']['#name'],$match)) {
  515. $key = $match[1];
  516. if (array_key_exists($key, $form_state['chado_properties'])) {
  517. unset($form_state['chado_properties'][$key]);
  518. }
  519. }
  520. $form_state['rebuild'] = TRUE;
  521. }
  522. /**
  523. * Ajax function which returns the section of the form to be re-rendered
  524. *
  525. * @ingroup tripal_core
  526. */
  527. function chado_add_node_form_properties_ajax_update($form, $form_state) {
  528. return $form['properties']['property_table'];
  529. }
  530. /**
  531. * Creates an array in form_state containing the existing properties. This array is
  532. * then modified by the add/remove buttons and used as a source for rebuilding the form.
  533. * This function get's called at each button (add and remove) button submits the first
  534. * time one of the button's is clicked to instantiates the $form_state['chado_properties'] array
  535. *
  536. * $form_state['chado_properties'] = array(
  537. * '[type_id]-[rank]' => array(
  538. * 'type_id' => [the cvterm.cvterm_id value]
  539. * 'type_name' => [the cvterm.name value]
  540. * 'property_id' => [the property.property_id value, or NULL if it doesn't yet exist],
  541. * 'value' => [the BASEprop.value value],
  542. * 'rank' => [the BASEprop.rank value],
  543. * ),
  544. * );
  545. *
  546. * @ingroup tripal_core
  547. */
  548. function chado_add_node_form_properties_create_property_formstate_array($form, &$form_state) {
  549. $form_state['chado_properties'] = array();
  550. foreach (element_children($form['properties']['property_table']) as $type_id) {
  551. if ($type_id != 'new') {
  552. foreach (element_children($form['properties']['property_table'][$type_id]) as $rank) {
  553. $element = $form['properties']['property_table'][$type_id][$rank];
  554. $property = array(
  555. 'type_id' => $element['prop_type_id']['#value'],
  556. 'type_name' => $element['type']['#markup'],
  557. 'property_id' => $element['property_id']['#value'],
  558. 'value' => $element['value']['#markup'],
  559. 'rank' => $element['rank']['#markup']
  560. );
  561. $key = $property['type_id'] . '-' . $property['rank'];
  562. $form_state['chado_properties'][$key] = (object) $property;
  563. }
  564. }
  565. }
  566. }
  567. /**
  568. * Function to theme the add/remove properties form into a table
  569. *
  570. * @ingroup tripal_chado_node_api
  571. */
  572. function theme_chado_add_node_form_properties($variables) {
  573. $element = $variables['element'];
  574. $header = array(
  575. 'type' => t('Type'),
  576. 'value' => t('Value'),
  577. 'property_action' => t('Actions'),
  578. );
  579. $rows = array();
  580. foreach (element_children($element) as $type_id) {
  581. if ($type_id == 'new') {
  582. $row = array();
  583. $row['data'] = array();
  584. foreach ($header as $fieldname => $title) {
  585. $row['data'][] = drupal_render($element[$type_id][$fieldname]);
  586. }
  587. $rows[] = $row;
  588. }
  589. else {
  590. foreach (element_children($element[$type_id]) as $version) {
  591. $row = array();
  592. $row['data'] = array();
  593. foreach ($header as $fieldname => $title) {
  594. $row['data'][] = drupal_render($element[$type_id][$version][$fieldname]);
  595. }
  596. $rows[] = $row;
  597. }
  598. }
  599. }
  600. return theme('table', array(
  601. 'header' => $header,
  602. 'rows' => $rows
  603. ));
  604. }
  605. /**
  606. * This function is used in a hook_insert, hook_update for a node form
  607. * when the chado node properties form has been added to the form. It retrieves all of the properties
  608. * and returns them in an array of the format:
  609. *
  610. * $dbxefs[<type_id>][<rank>] = <value>
  611. *
  612. * This array can then be used for inserting or updating properties
  613. *
  614. * @param $node
  615. *
  616. * @return
  617. * A property array
  618. *
  619. * @ingroup tripal_chado_node_api
  620. */
  621. function chado_retrieve_node_form_properties($node) {
  622. $properties = array();
  623. if (isset($node->property_table)) {
  624. foreach ($node->property_table as $type_id => $elements) {
  625. if ($type_id != 'new' AND $type_id != 'details') {
  626. foreach ($elements as $rank => $element) {
  627. $properties[$type_id][$rank] = $element['prop_value'];
  628. }
  629. }
  630. }
  631. }
  632. return $properties;
  633. }
  634. /**
  635. * This function is used in hook_insert or hook_update and handles inserting of any new
  636. * properties
  637. *
  638. * @param $node
  639. * The node passed into hook_insert & hook_update
  640. * @param $details
  641. * - property_table: the name of the _property linking table (ie: feature_property)
  642. * - base_table: the name of the base table (ie: feature)
  643. * - foreignkey_name: the name of the foreign key used to link to the node content (ie: feature_id)
  644. * - foreignkey_value: the value of the foreign key (ie: 445, if there exists a feature where feature_id=445)
  645. * @param $retrieved_properties
  646. * An array of properties from chado_retrieve_node_form_properties($node). This can be used if you need
  647. * special handling for some of the properties (See FeatureMap chado_featuremap_insert for an example)
  648. *
  649. * @ingroup tripal_chado_node_api
  650. */
  651. function chado_update_node_form_properties($node, $details, $retrieved_properties = FALSE) {
  652. $details['foreignkey_value'] = (isset($details['foreignkey_value'])) ? $details['foreignkey_value'] : 0;
  653. if (isset($node->property_table) AND ($details['foreignkey_value'] > 0)) {
  654. // First remove existing property links
  655. chado_delete_record($details['property_table'], array($details['foreignkey_name'] => $details['foreignkey_value']));
  656. // Add back in property links and insert properties as needed
  657. if ($retrieved_properties) {
  658. $properties = $retrieved_properties;
  659. }
  660. else {
  661. $properties = chado_retrieve_node_form_properties($node);
  662. }
  663. foreach ($properties as $type_id => $ranks) {
  664. foreach ($ranks as $rank => $value) {
  665. $success = chado_insert_record(
  666. $details['property_table'],
  667. array(
  668. $details['foreignkey_name'] => $details['foreignkey_value'],
  669. 'type_id' => $type_id,
  670. 'value' => $value,
  671. 'rank' => $rank
  672. )
  673. );
  674. if (!$success) {
  675. tripal_report_error('tripal_' . $details['base_table'], TRIPAL_ERROR,
  676. $details['base_table'] . ' Insert: Unable to insert property type_id %cvterm with value %value.',
  677. array('%cvterm' => $type_id, '%value' => $value));
  678. }
  679. }
  680. }
  681. }
  682. }