tripal_core.chado_nodes.properties.api.inc 25 KB

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