tripal_api.chado_node.properties.inc 24 KB

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