tripal_core.chado_nodes.properties.api.inc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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. 'base_foreign_key' => 'example_id', // key to link to the chado content created by this node
  29. 'base_key_value' => $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 needed by this form. Required Keys are:
  84. * - property_table: the name of the property linking table (ie: featureprop)
  85. * - base_foreign_key: the name of the foreign key linking this table to the non-property table (ie: feature_id)
  86. * - base_key_value: the value of the base_foreign_key for the current form (ie: 999 if the feature_id=999)
  87. * Require ONE of the following:
  88. * The controlled vocabulary governing the property types
  89. * -cv_id: the unique key from the cv table
  90. * -cv_name: the cv.name field uniquely identifying the controlled vocab
  91. * Optional keys include:
  92. * - fieldset_title: the non-translated title for this fieldset
  93. * - additional_instructions: a non-translated string providing additional instructions
  94. * - select_options: must be an array where the [key] is a valid cvterm_id and
  95. * the [value] is the human-readable name of the option. This is generated from the cv_name/id by default
  96. *
  97. * @ingroup tripal_chado_node_api
  98. */
  99. function chado_add_node_form_properties(&$form, &$form_state, $details) {
  100. // Set Defaults for optional fields
  101. $details['fieldset_title'] = 'Properties';
  102. $details['additional_instructions'] = '';
  103. // Get Property Types for the Select List
  104. if (isset($details['select_options'])) {
  105. $property_options = $details['select_options'];
  106. }
  107. else {
  108. if (isset($details['cv_name'])) {
  109. $property_options = array();
  110. $property_options[] = 'Select a Property';
  111. $sql = "
  112. SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition, CV.cv_id as cv_id
  113. FROM {cvterm} CVT
  114. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  115. WHERE
  116. CV.name = :cv_name AND
  117. NOT CVT.is_obsolete = 1
  118. ORDER BY CVT.name ASC
  119. ";
  120. $prop_types = chado_query($sql, array(':cv_name' => $details['cv_name']));
  121. while ($prop = $prop_types->fetchObject()) {
  122. $property_options[$prop->cvterm_id] = $prop->name;
  123. $details['cv_id'] = $prop->cv_id;
  124. }
  125. if (!isset($details['cv_id'])) {
  126. $result = chado_select_record('cv',array('cv_id'),array('name' => $details['cv_name']));
  127. $details['cv_id'] = $result[0]->cv_id;
  128. }
  129. } elseif (isset($details['cv_id'])) {
  130. $property_options = array();
  131. $property_options[] = 'Select a Property';
  132. $sql = "
  133. SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition, CV.name as cv_name
  134. FROM {cvterm} CVT
  135. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  136. WHERE
  137. CV.cv_id = :cv_id AND
  138. NOT CVT.is_obsolete = 1
  139. ORDER BY CVT.name ASC
  140. ";
  141. $prop_types = chado_query($sql, array(':cv_id' => $details['cv_id']));
  142. while ($prop = $prop_types->fetchObject()) {
  143. $property_options[$prop->cvterm_id] = $prop->name;
  144. $details['cv_name'] = $prop->cv_name;
  145. }
  146. if (!isset($details['cv_name'])) {
  147. $result = chado_select_record('cv',array('name'),array('cv_id' => $details['cv_id']));
  148. $details['cv_name'] = $result[0]->name;
  149. }
  150. }
  151. }
  152. // Tell tripal administrators how to add terms to the property types drop down.
  153. if (empty($property_options)) {
  154. $tripal_message = tripal_set_message(
  155. t('There are currently no proeprty types! To add additional properties to the drop
  156. down list, you need to <a href="@cvtermlink">add a controlled vocabulary term</a>
  157. to the %cv_name controlled vocabulary.',
  158. array(
  159. '%cv_name' => $details['cv_name'],
  160. '@cvtermlink' => url('admin/tripal/chado/tripal_cv/cv/'.$details['cv_id'].'/cvterm/add')
  161. )
  162. ),
  163. TRIPAL_WARNING,
  164. array('return_html' => TRUE)
  165. );
  166. }
  167. else {
  168. $tripal_message = tripal_set_message(
  169. t('To add additional properties to the drop down list, you need to <a href="@cvtermlink">add
  170. a controlled vocabulary term</a> to the %cv_name controlled vocabulary.',
  171. array(
  172. '%cv_name' => $details['cv_name'],
  173. '@cvtermlink' => url('admin/tripal/chado/tripal_cv/cv/'.$details['cv_id'].'/cvterm/add')
  174. )
  175. ),
  176. TRIPAL_INFO,
  177. array('return_html' => TRUE)
  178. );
  179. }
  180. // the fieldset of the property elements
  181. $form['properties'] = array(
  182. '#type' => 'fieldset',
  183. '#title' => t($details['fieldset_title']),
  184. '#description' => t('You may add additional properties by selecting a property type
  185. from the dropdown and adding text. You may add as many properties as desired by
  186. clicking the add button on the right. To remove a property, click the remove button.'
  187. . $details['additional_instructions']),
  188. '#prefix' => "<div id='properties-fieldset'>",
  189. '#suffix' => '</div>',
  190. '#weight' => 8
  191. );
  192. $form['properties']['admin_message'] = array(
  193. '#type' => 'markup',
  194. '#markup' => $tripal_message
  195. );
  196. // this form element is a tree, so that we don't puke all of the values into then node variable
  197. // it is set as a tree, and keeps them in the $form_state['values']['property_table'] heading.
  198. $form['properties']['property_table'] = array(
  199. '#type' => 'markup',
  200. '#tree' => TRUE,
  201. '#prefix' => '<div id="tripal-generic-edit-properties-table">',
  202. '#suffix' => '</div>',
  203. '#theme' => 'chado_node_properties_form_table'
  204. );
  205. // Add defaults into form_state to be used elsewhere
  206. $form['properties']['property_table']['details'] = array(
  207. '#type' => 'hidden',
  208. '#value' => serialize($details)
  209. );
  210. /* Properties can come to us in two ways:
  211. *
  212. * 1) In the form state in the $form_state['chado_properties']. Data is in this field
  213. * when an AJAX call updates the form state or a validation error.
  214. *
  215. * 2) Directly from the database if the record already has properties associated. This
  216. * data is only used the first time the form is loaded. On AJAX calls or validation
  217. * errors the fields on the form are populated from the $form_state['chado_properties']
  218. * entry.
  219. */
  220. if (isset($form_state['chado_properties'])) {
  221. $existing_properties = $form_state['chado_properties'];
  222. }
  223. else {
  224. if (isset($details['cv_name'])) {
  225. $existing_properties = chado_query(
  226. "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
  227. FROM {" . $details['property_table'] . "} PP
  228. INNER JOIN {cvterm} CVT ON CVT.cvterm_id = PP.type_id
  229. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  230. WHERE
  231. PP." . $details['base_foreign_key'] . " = :base_key_value AND
  232. CV.name = '" .$details['cv_name']. "'
  233. ORDER BY CVT.name, PP.rank",
  234. array(':base_key_value' => $details['base_key_value'])
  235. );
  236. } elseif (isset($details['cv_id'])) {
  237. $existing_properties = chado_query(
  238. "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
  239. FROM {" . $details['property_table'] . "} PP
  240. INNER JOIN {cvterm} CVT ON CVT.cvterm_id = PP.type_id
  241. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  242. WHERE
  243. PP." . $details['base_foreign_key'] . " = :base_key_value AND
  244. CV.cv_id = '" .$details['cv_id']. "'
  245. ORDER BY CVT.name, PP.rank",
  246. array(':base_key_value' => $details['base_key_value'])
  247. );
  248. }
  249. }
  250. /* The format of the $existing_properties array is either:
  251. *
  252. * From the chado_properties array:
  253. * $form_state['chado_properties'] = array(
  254. * '[type_id]-[rank]' => array(
  255. * 'type_id' => [the cvterm.cvterm_id value]
  256. * 'type_name' => [the cvterm.name value]
  257. * 'property_id' => [the property.property_id value, or NULL if it doesn't yet exist],
  258. * 'value' => [the BASEprop.value value],
  259. * 'rank' => [the BASEprop.rank value],
  260. * ),
  261. * );
  262. *
  263. * OR
  264. * Populated from the database:
  265. * $existing_property = array(
  266. * 0 => array(
  267. * 'property_id' => [the property.property_id value, or NULL if it doesn't yet exist],
  268. * 'type_id' => [the cvterm.cvterm_id value]
  269. * 'type_name' => [the cvterm.name value]
  270. * 'value' => [the BASEprop.value value],
  271. * 'rank' => [the BASEprop.rank value],
  272. * ),
  273. * );
  274. *
  275. * NOTE: The main difference is the key
  276. *
  277. * Loop on the array elements of the $existing_properties array and add
  278. * an element to the form for each one as long as it's also in the
  279. * $properties_options array.
  280. */
  281. foreach ($existing_properties as $property) {
  282. if (array_key_exists($property->type_id, $property_options)) {
  283. $form['properties']['property_table'][$property->type_id]['#type'] = 'markup';
  284. $form['properties']['property_table'][$property->type_id]['#value'] = '';
  285. $form['properties']['property_table'][$property->type_id][$property->rank]['#type'] = 'markup';
  286. $form['properties']['property_table'][$property->type_id][$property->rank]['#value'] = '';
  287. $form['properties']['property_table'][$property->type_id][$property->rank]['prop_type_id'] = array(
  288. '#type' => 'hidden',
  289. '#value' => $property->type_id
  290. );
  291. $form['properties']['property_table'][$property->type_id][$property->rank]['prop_value'] = array(
  292. '#type' => 'hidden',
  293. '#value' => $property->value
  294. );
  295. $form['properties']['property_table'][$property->type_id][$property->rank]['property_id'] = array(
  296. '#type' => 'hidden',
  297. '#value' => $property->property_id
  298. );
  299. $form['properties']['property_table'][$property->type_id][$property->rank]['type'] = array(
  300. '#type' => 'markup',
  301. '#markup' => $property->type_name
  302. );
  303. $form['properties']['property_table'][$property->type_id][$property->rank]['value'] = array(
  304. '#type' => 'markup',
  305. '#markup' => $property->value
  306. );
  307. $form['properties']['property_table'][$property->type_id][$property->rank]['rank'] = array(
  308. '#type' => 'markup',
  309. '#markup' => $property->rank
  310. );
  311. // remove button
  312. $form['properties']['property_table'][$property->type_id][$property->rank]['property_action'] = array(
  313. '#type' => 'submit',
  314. '#value' => t('Remove'),
  315. '#name' => "property_remove-".$property->type_id.'-'.$property->rank,
  316. '#ajax' => array(
  317. 'callback' => "chado_add_node_form_properties_ajax_update",
  318. 'wrapper' => 'tripal-generic-edit-properties-table',
  319. 'effect' => 'fade',
  320. 'method' => 'replace',
  321. 'prevent' => 'click'
  322. ),
  323. // When this button is clicked, the form will be validated and submitted.
  324. // Therefore, we set custom submit and validate functions to override the
  325. // default node form submit. In the validate function we validate only the
  326. // property fields and in the submit we remove the indicated property
  327. // from the chado_properties array. In order to keep validate errors
  328. // from the node form validate and Drupal required errors for non-property fields
  329. // preventing the user from removing properties we set the #limit_validation_errors below
  330. '#validate' => array('chado_add_node_form_properties_remove_button_validate'),
  331. '#submit' => array('chado_add_node_form_properties_remove_button_submit'),
  332. // Limit the validation of the form upon clicking this button to the property_table tree
  333. // No other fields will be validated (ie: no fields from the main form or any other api
  334. // added form).
  335. '#limit_validation_errors' => array(
  336. array('property_table') // Validate all fields within $form_state['values']['property_table']
  337. )
  338. );
  339. }
  340. }
  341. // Form elements for adding a new property
  342. //---------------------------------------------
  343. $form['properties']['property_table']['new'] = array(
  344. '#type' => 'markup',
  345. '#prefix' => '<span class="addtl-properties-add-new-property">',
  346. '#suffix' => '</span>'
  347. );
  348. $form['properties']['property_table']['new']['type'] = array(
  349. '#type' => 'select',
  350. '#options' => $property_options, // Set at top of form
  351. );
  352. $form['properties']['property_table']['new']['value'] = array(
  353. '#type' => 'textarea',
  354. '#rows' => 1,
  355. );
  356. // add button
  357. $form['properties']['property_table']['new']['property_action'] = array(
  358. '#type' => 'submit',
  359. '#value' => t('Add'),
  360. '#name' => "property-add",
  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. // additional property fields and in the submit we add them to the chado_properties
  372. // array. In order to keep validate errors from the node form validate and Drupal
  373. // required errors for non-property fields preventing the user from adding properties we
  374. // set the #limit_validation_errors below
  375. '#validate' => array('chado_update_node_form_properties_add_button_validate'),
  376. '#submit' => array('chado_add_node_form_properties_add_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. * Validate the user input for creating a new property
  387. * Called by the add button in chado_add_node_form_properties
  388. *
  389. * @ingroup tripal_core
  390. */
  391. function chado_update_node_form_properties_add_button_validate($form, &$form_state) {
  392. // Ensure the type_id is supplied & Valid
  393. $cvterm = chado_select_record(
  394. 'cvterm',
  395. array('cvterm_id', 'name'),
  396. array('cvterm_id' => $form_state['values']['property_table']['new']['type'])
  397. );
  398. if (!isset($cvterm[0])) {
  399. form_set_error('property_table][new][cvterm', 'Please select a property type before attempting to add a new property.');
  400. }
  401. else {
  402. $form_state['values']['property_table']['new']['type_name'] = $cvterm[0]->name;
  403. }
  404. // Ensure value is supplied
  405. if (empty($form_state['values']['property_table']['new']['value'])) {
  406. form_set_error('property_table][new][value','You must enter the property value before attempting to add a new property.');
  407. }
  408. }
  409. /**
  410. * Called by the add button in chado_add_node_form_properties
  411. *
  412. * Create an array of properties in the form state. This array will then be
  413. * used to rebuild the form in subsequent builds
  414. *
  415. * @ingroup tripal_core
  416. */
  417. function chado_add_node_form_properties_add_button_submit(&$form, &$form_state) {
  418. $details = unserialize($form_state['values']['property_table']['details']);
  419. // if the chado_additional_properties array is not set then this is the first time modifying the
  420. // property table. this means we need to include all the properties from the db
  421. if (!isset($form_state['chado_properties'])) {
  422. chado_add_node_form_properties_create_property_formstate_array($form, $form_state);
  423. }
  424. // get details for the new property
  425. $property = array(
  426. 'type_id' => $form_state['values']['property_table']['new']['type'],
  427. 'type_name' => $form_state['values']['property_table']['new']['type_name'],
  428. 'property_id' => NULL,
  429. 'value' => $form_state['values']['property_table']['new']['value'],
  430. 'rank' => '0',
  431. );
  432. // get max rank
  433. $rank = chado_get_table_max_rank(
  434. $details['property_table'],
  435. array(
  436. $details['base_foreign_key'] => $details['base_key_value'],
  437. 'type_id' => $property['type_id']
  438. )
  439. );
  440. $property['rank'] = strval($rank + 1);
  441. $key = $property['type_id'] . '-' . $property['rank'];
  442. $form_state['chado_properties'][$key] = (object) $property;
  443. $form_state['rebuild'] = TRUE;
  444. }
  445. /**
  446. * There is no user input for the remove buttons so there is no need to validate
  447. * However, both a submit & validate need to be specified so this is just a placeholder
  448. *
  449. * Called by the many remove buttons in chado_add_node_form_properties
  450. *
  451. * @ingroup tripal_core
  452. */
  453. function chado_add_node_form_properties_remove_button_validate($form, $form_state) {
  454. // No Validation needed for remove
  455. }
  456. /**
  457. * Remove the correct property from the form
  458. * Called by the many remove buttons in chado_add_node_form_properties
  459. *
  460. * @ingroup tripal_core
  461. */
  462. function chado_add_node_form_properties_remove_button_submit(&$form, &$form_state) {
  463. // if the chado_properties array is not set then this is the first time modifying the
  464. // property table. this means we need to include all the properties from the db
  465. if (!isset($form_state['chado_properties'])) {
  466. chado_add_node_form_properties_create_property_formstate_array($form, $form_state);
  467. }
  468. // remove the specified property from the form property table
  469. if(preg_match('/property_remove-([^-]+-[^-]+)/',$form_state['triggering_element']['#name'],$match)) {
  470. $key = $match[1];
  471. if (array_key_exists($key, $form_state['chado_properties'])) {
  472. unset($form_state['chado_properties'][$key]);
  473. }
  474. }
  475. $form_state['rebuild'] = TRUE;
  476. }
  477. /**
  478. * Ajax function which returns the section of the form to be re-rendered
  479. *
  480. * @ingroup tripal_core
  481. */
  482. function chado_add_node_form_properties_ajax_update($form, $form_state) {
  483. return $form['properties']['property_table'];
  484. }
  485. /**
  486. * Creates an array in form_state containing the existing properties. This array is
  487. * then modified by the add/remove buttons and used as a source for rebuilding the form.
  488. * This function get's called at each button (add and remove) button submits the first
  489. * time one of the button's is clicked to instantiates the $form_state['chado_properties'] array
  490. *
  491. * $form_state['chado_properties'] = array(
  492. * '[type_id]-[rank]' => array(
  493. * 'type_id' => [the cvterm.cvterm_id value]
  494. * 'type_name' => [the cvterm.name value]
  495. * 'property_id' => [the property.property_id value, or NULL if it doesn't yet exist],
  496. * 'value' => [the BASEprop.value value],
  497. * 'rank' => [the BASEprop.rank value],
  498. * ),
  499. * );
  500. *
  501. * @ingroup tripal_core
  502. */
  503. function chado_add_node_form_properties_create_property_formstate_array($form, &$form_state) {
  504. $form_state['chado_properties'] = array();
  505. foreach (element_children($form['properties']['property_table']) as $type_id) {
  506. if ($type_id != 'new') {
  507. foreach (element_children($form['properties']['property_table'][$type_id]) as $rank) {
  508. $element = $form['properties']['property_table'][$type_id][$rank];
  509. $property = array(
  510. 'type_id' => $element['prop_type_id']['#value'],
  511. 'type_name' => $element['type']['#markup'],
  512. 'property_id' => $element['property_id']['#value'],
  513. 'value' => $element['value']['#markup'],
  514. 'rank' => $element['rank']['#markup']
  515. );
  516. $key = $property['type_id'] . '-' . $property['rank'];
  517. $form_state['chado_properties'][$key] = (object) $property;
  518. }
  519. }
  520. }
  521. }
  522. /**
  523. * Function to theme the add/remove properties form into a table
  524. *
  525. * @ingroup tripal_chado_node_api
  526. */
  527. function theme_chado_add_node_form_properties($variables) {
  528. $element = $variables['element'];
  529. $header = array(
  530. 'type' => t('Type'),
  531. 'value' => t('Value'),
  532. 'property_action' => t('Actions'),
  533. );
  534. $rows = array();
  535. foreach (element_children($element) as $type_id) {
  536. if ($type_id == 'new') {
  537. $row = array();
  538. $row['data'] = array();
  539. foreach ($header as $fieldname => $title) {
  540. $row['data'][] = drupal_render($element[$type_id][$fieldname]);
  541. }
  542. $rows[] = $row;
  543. }
  544. else {
  545. foreach (element_children($element[$type_id]) as $version) {
  546. $row = array();
  547. $row['data'] = array();
  548. foreach ($header as $fieldname => $title) {
  549. $row['data'][] = drupal_render($element[$type_id][$version][$fieldname]);
  550. }
  551. $rows[] = $row;
  552. }
  553. }
  554. }
  555. return theme('table', array(
  556. 'header' => $header,
  557. 'rows' => $rows
  558. ));
  559. }
  560. /**
  561. * This function is used in a hook_insert, hook_update for a node form
  562. * when the chado node properties form has been added to the form. It retrieves all of the properties
  563. * and returns them in an array of the format:
  564. *
  565. * $dbxefs[<type_id>][<rank>] = <value>
  566. *
  567. * This array can then be used for inserting or updating properties
  568. *
  569. * @param $node
  570. *
  571. * @return
  572. * A property array
  573. *
  574. * @ingroup tripal_chado_node_api
  575. */
  576. function chado_retrieve_node_form_properties($node) {
  577. $properties = array();
  578. if (isset($node->property_table)) {
  579. foreach ($node->property_table as $type_id => $elements) {
  580. if ($type_id != 'new' AND $type_id != 'details') {
  581. foreach ($elements as $rank => $element) {
  582. $properties[$type_id][$rank] = $element['prop_value'];
  583. }
  584. }
  585. }
  586. }
  587. return $properties;
  588. }
  589. /**
  590. * This function is used in hook_insert or hook_update and handles inserting of any new
  591. * properties
  592. *
  593. * @param $node
  594. * The node passed into hook_insert & hook_update
  595. * @param $details
  596. * - property_table: the name of the _property linking table (ie: feature_property)
  597. * - base_table: the name of the base table (ie: feature)
  598. * - foreignkey_name: the name of the foreign key used to link to the node content (ie: feature_id)
  599. * - foreignkey_value: the value of the foreign key (ie: 445, if there exists a feature where feature_id=445)
  600. * @param $retrieved_properties
  601. * An array of properties from chado_retrieve_node_form_properties($node). This can be used if you need
  602. * special handling for some of the properties (See FeatureMap chado_featuremap_insert for an example)
  603. *
  604. * @ingroup tripal_chado_node_api
  605. */
  606. function chado_update_node_form_properties($node, $details, $retrieved_properties = FALSE) {
  607. $details['foreignkey_value'] = (isset($details['foreignkey_value'])) ? $details['foreignkey_value'] : 0;
  608. if (isset($node->property_table) AND ($details['foreignkey_value'] > 0)) {
  609. // First remove existing property links
  610. chado_delete_record($details['property_table'], array($details['foreignkey_name'] => $details['foreignkey_value']));
  611. // Add back in property links and insert properties as needed
  612. if ($retrieved_properties) {
  613. $properties = $retrieved_properties;
  614. }
  615. else {
  616. $properties = chado_retrieve_node_form_properties($node);
  617. }
  618. foreach ($properties as $type_id => $ranks) {
  619. foreach ($ranks as $rank => $value) {
  620. $success = chado_insert_record(
  621. $details['property_table'],
  622. array(
  623. $details['foreignkey_name'] => $details['foreignkey_value'],
  624. 'type_id' => $type_id,
  625. 'value' => $value,
  626. 'rank' => $rank
  627. )
  628. );
  629. if (!$success) {
  630. tripal_report_error('tripal_' . $details['base_table'], TRIPAL_ERROR,
  631. $details['base_table'] . ' Insert: Unable to insert property type_id %cvterm with value %value.',
  632. array('%cvterm' => $type_id, '%value' => $value));
  633. }
  634. }
  635. }
  636. }
  637. }