tripal_core.chado_nodes.properties.api.inc 28 KB

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