tripal_core.chado_nodes.properties.api.inc 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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. * Retrieve a property for a given base table record
  77. *
  78. * @param $basetable
  79. * The base table for which the property should be retrieved. Thus to retrieve a property
  80. * for a feature the basetable=feature and property is retrieved from featureprop
  81. * @param $record_id
  82. * The foriegn key field of the base table. This should be in integer.
  83. * @param $property
  84. * The cvterm name describing the type of properties to be retrieved
  85. * @param $cv_name
  86. * The name of the cv that the above cvterm is part of
  87. *
  88. * @return
  89. * An array in the same format as that generated by the function
  90. * chado_generate_var(). If only one record is returned it
  91. * is a single object. If more than one record is returned then it is an array
  92. * of objects
  93. *
  94. * @ingroup tripal_chado_node_api
  95. */
  96. function chado_get_property($basetable, $record_id, $property, $cv_name, $property_id = FALSE) {
  97. // get the foreign key for this property table
  98. $table_desc = chado_get_schema($basetable . 'prop');
  99. $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
  100. // construct the array of values to be selected
  101. $values = array(
  102. $fkcol => $record_id,
  103. 'type_id' => array(
  104. 'cv_id' => array(
  105. 'name' => $cv_name,
  106. ),
  107. 'name' => $property,
  108. 'is_obsolete' => 0
  109. ),
  110. );
  111. // if we have the unique property_id make sure to add that to the values
  112. if ($property_id) {
  113. $property_pkey = $table_desc['primary key'][0];
  114. $values[$property_pkey] = $property_id;
  115. }
  116. $results = chado_generate_var($basetable . 'prop', $values);
  117. if ($results) {
  118. $results = chado_expand_var($results, 'field', $basetable . 'prop.value');
  119. }
  120. return $results;
  121. }
  122. /**
  123. * Insert a property for a given base table. By default if the property already
  124. * exists a new property is added with the next available rank. If
  125. * $update_if_present argument is specified then the record will be updated if it
  126. * exists rather than adding a new property.
  127. *
  128. * @param $basetable
  129. * The base table for which the property should be inserted. Thus to insert a property
  130. * for a feature the basetable=feature and property is inserted into featureprop
  131. * @param $record_id
  132. * The foriegn key value of the base table. This should be in integer.
  133. * @param $property
  134. * The cvterm name describing the type of properties to be inserted
  135. * @param $cv_name
  136. * The name of the cv that the above cvterm is part of
  137. * @param $value
  138. * The value of the property to be inserted (can be empty)
  139. * @param $update_if_present
  140. * A boolean indicating whether an existing record should be updated. If the
  141. * property already exists and this value is not specified or is zero then
  142. * a new property will be added with the next largest rank.
  143. *
  144. * @return
  145. * Return True on Insert/Update and False otherwise
  146. *
  147. * @ingroup tripal_chado_node_api
  148. */
  149. function chado_insert_property($basetable, $record_id, $property,
  150. $cv_name, $value, $update_if_present = 0) {
  151. // first see if the property already exists, if the user want's to update
  152. // then we can do that, but otherwise we want to increment the rank and
  153. // insert
  154. $props = chado_get_property($basetable, $record_id, $property, $cv_name);
  155. if (!is_array($props) and $props) {
  156. $props = array($props);
  157. }
  158. $rank = 0;
  159. if (count($props) > 0) {
  160. if ($update_if_present) {
  161. return chado_update_property($basetable, $record_id, $property, $cv_name, $value);
  162. }
  163. else {
  164. // iterate through the properties returned and check to see if the
  165. // property with this value already exists if not, get the largest rank
  166. // and insert the same property but with this new value
  167. foreach ($props as $p) {
  168. if ($p->rank > $rank) {
  169. $rank = $p->rank;
  170. }
  171. if (strcmp($p->value, $value) == 0) {
  172. return TRUE;
  173. }
  174. }
  175. // now add 1 to the rank
  176. $rank++;
  177. }
  178. }
  179. // make sure the cvterm exists. Otherwise we'll get an error with
  180. // prepared statements not matching
  181. $values = array(
  182. 'cv_id' => array(
  183. 'name' => $cv_name,
  184. ),
  185. 'name' => $property,
  186. );
  187. $options = array();
  188. $term = chado_select_record('cvterm', array('cvterm_id'), $values, $options);
  189. if (!$term or count($term) == 0) {
  190. tripal_report_error('tripal_core', TRIPAL_ERROR, "Cannot find property '%prop_name' in vocabulary '%cvname'.",
  191. array('%prop_name' => $property, '%cvname' => $cv_name));
  192. return FALSE;
  193. }
  194. // get the foreign key for this property table
  195. $table_desc = chado_get_schema($basetable . 'prop');
  196. $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
  197. // construct the array of values to be inserted
  198. $values = array(
  199. $fkcol => $record_id,
  200. 'type_id' => array(
  201. 'cv_id' => array(
  202. 'name' => $cv_name,
  203. ),
  204. 'name' => $property,
  205. ),
  206. 'value' => $value,
  207. 'rank' => $rank,
  208. );
  209. $options = array();
  210. $result = chado_insert_record($basetable . 'prop', $values, $options);
  211. return $result;
  212. }
  213. /**
  214. * Update a property for a given base table record and property name. This
  215. * function should be used only if one record of the property will be present.
  216. * If the property name can have multiple entries (with increasing rank) then
  217. * use the function named chado_update_propertyID
  218. *
  219. * @param $basetable
  220. * The base table for which the property should be updated. The property table
  221. * is constructed using a combination of the base table name and the suffix
  222. * 'prop' (e.g. basetable = feature then property tabie is featureprop).
  223. * @param $record_id
  224. * The foreign key of the basetable to update a property for. This should be in integer.
  225. * For example, if the basetable is 'feature' then the $record_id should be the feature_id
  226. * @param $property
  227. * The cvterm name of property to be updated
  228. * @param $cv_name
  229. * The name of the cv that the above cvterm is part of
  230. * @param $value
  231. * The value of the property to be inserted (can be empty)
  232. * @param $insert_if_missing
  233. * A boolean indicating whether a record should be inserted if one doesn't exist to update
  234. *
  235. * Note: The property to be updated is select via the unique combination of $record_id and
  236. * $property and then it is updated with the supplied value
  237. *
  238. * @return
  239. * Return True on Update/Insert and False otherwise
  240. *
  241. * @ingroup tripal_chado_node_api
  242. */
  243. function chado_update_property($basetable, $record_id, $property,
  244. $cv_name, $value, $insert_if_missing = FALSE, $property_id = FALSE) {
  245. // first see if the property is missing (we can't update a missing property
  246. $prop = chado_get_property($basetable, $record_id, $property, $cv_name, $property_id);
  247. if (count($prop)==0) {
  248. if ($insert_if_missing) {
  249. return chado_insert_property($basetable, $record_id, $property, $cv_name, $value);
  250. }
  251. else {
  252. return FALSE;
  253. }
  254. }
  255. // get the foreign key for this property table
  256. $table_desc = chado_get_schema($basetable . 'prop');
  257. $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
  258. // construct the array that will match the exact record to update
  259. $match = array(
  260. $fkcol => $record_id,
  261. 'type_id' => array(
  262. 'cv_id' => array(
  263. 'name' => $cv_name,
  264. ),
  265. 'name' => $property,
  266. ),
  267. );
  268. // If we have the unique property_id, make sure to use it in the match to ensure
  269. // we get the exact record. Doesn't rely on there only being one property of that type
  270. if ($property_id) {
  271. $property_pkey = $table_desc['primary key'][0];
  272. $match = array(
  273. $property_pkey => $property_id
  274. );
  275. }
  276. // construct the array of values to be updated
  277. $values = array(
  278. 'value' => $value,
  279. );
  280. // If we have the unique property_id then we can also update the type
  281. // thus add it to the values to be updated
  282. if ($property_id) {
  283. $values['type_id'] = array(
  284. 'cv_id' => array(
  285. 'name' => $cv_name,
  286. ),
  287. 'name' => $property,
  288. );
  289. }
  290. return chado_update_record($basetable . 'prop', $match, $values);
  291. }
  292. /**
  293. * Deletes a property for a given base table record using the property name
  294. *
  295. * @param $basetable
  296. * The base table for which the property should be deleted. Thus to deleted a property
  297. * for a feature the basetable=feature and property is deleted from featureprop
  298. * @param $record_id
  299. * The primary key of the basetable to delete a property for. This should be in integer.
  300. * @param $property
  301. * The cvterm name describing the type of property to be deleted
  302. * @param $cv_name
  303. * The name of the cv that the above cvterm is part of
  304. *
  305. * Note: The property to be deleted is select via the unique combination of $record_id and $property
  306. *
  307. * @return
  308. * Return True on Delete and False otherwise
  309. *
  310. * @ingroup tripal_chado_node_api
  311. */
  312. function chado_delete_property($basetable, $record_id, $property, $cv_name) {
  313. // get the foreign key for this property table
  314. $table_desc = chado_get_schema($basetable . 'prop');
  315. $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
  316. // construct the array that will match the exact record to update
  317. $match = array(
  318. $fkcol => $record_id,
  319. 'type_id' => array(
  320. 'cv_id' => array(
  321. 'name' => $cv_name,
  322. ),
  323. 'name' => $property,
  324. ),
  325. );
  326. return chado_delete_record($basetable . 'prop', $match);
  327. }
  328. /**
  329. * @section
  330. * Properties Form to be added to node forms
  331. */
  332. /**
  333. * Provides a form for adding to BASEprop table
  334. *
  335. * @param $form
  336. * The Drupal form array into which the property form elements will be added
  337. * @param $form_state
  338. * The corresponding form_state array for the form
  339. * @param $details
  340. * An array defining details needed by this form. Required Keys are:
  341. * - property_table: the name of the property linking table (ie: featureprop)
  342. * - base_foreign_key: the name of the foreign key linking this table to the non-property table (ie: feature_id)
  343. * - base_key_value: the value of the base_foreign_key for the current form (ie: 999 if the feature_id=999)
  344. * Require ONE of the following:
  345. * The controlled vocabulary governing the property types
  346. * -cv_id: the unique key from the cv table
  347. * -cv_name: the cv.name field uniquely identifying the controlled vocab
  348. * Optional keys include:
  349. * - fieldset_title: the non-translated title for this fieldset
  350. * - additional_instructions: a non-translated string providing additional instructions
  351. * - select_options: must be an array where the [key] is a valid cvterm_id and
  352. * the [value] is the human-readable name of the option. This is generated from the cv_name/id by default
  353. *
  354. * @ingroup tripal_chado_node_api
  355. */
  356. function chado_add_node_form_properties(&$form, &$form_state, $details) {
  357. // Set Defaults for optional fields
  358. $details['fieldset_title'] = 'Properties';
  359. $details['additional_instructions'] = '';
  360. // Get Property Types for the Select List
  361. if (isset($details['select_options'])) {
  362. $property_options = $details['select_options'];
  363. }
  364. else {
  365. if (isset($details['cv_name'])) {
  366. $property_options = array();
  367. $property_options[] = 'Select a Property';
  368. $sql = "
  369. SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
  370. FROM {cvterm} CVT
  371. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  372. WHERE
  373. CV.name = :cv_name AND
  374. NOT CVT.is_obsolete = 1
  375. ORDER BY CVT.name ASC
  376. ";
  377. $prop_types = chado_query($sql, array(':cv_name' => $details['cv_name']));
  378. while ($prop = $prop_types->fetchObject()) {
  379. $property_options[$prop->cvterm_id] = $prop->name;
  380. }
  381. } elseif (isset($details['cv_id'])) {
  382. $property_options = array();
  383. $property_options[] = 'Select a Property';
  384. $sql = "
  385. SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
  386. FROM {cvterm} CVT
  387. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  388. WHERE
  389. CV.cv_id = :cv_id AND
  390. NOT CVT.is_obsolete = 1
  391. ORDER BY CVT.name ASC
  392. ";
  393. $prop_types = chado_query($sql, array(':cv_id' => $details['cv_id']));
  394. while ($prop = $prop_types->fetchObject()) {
  395. $property_options[$prop->cvterm_id] = $prop->name;
  396. }
  397. }
  398. }
  399. // the fieldset of the property elements
  400. $form['properties'] = array(
  401. '#type' => 'fieldset',
  402. '#title' => t($details['fieldset_title']),
  403. '#description' => t('You may add additional properties by selecting a property type
  404. from the dropdown and adding text. You may add as many properties as desired by
  405. clicking the add button on the right. To remove a property, click the remove button.
  406. To add additional properties to the drop down. ' . $details['additional_instructions']),
  407. '#prefix' => "<div id='properties-fieldset'>",
  408. '#suffix' => '</div>',
  409. '#weight' => 8
  410. );
  411. // this form element is a tree, so that we don't puke all of the values into then node variable
  412. // it is set as a tree, and keeps them in the $form_state['values']['property_table'] heading.
  413. $form['properties']['property_table'] = array(
  414. '#type' => 'markup',
  415. '#tree' => TRUE,
  416. '#prefix' => '<div id="tripal-generic-edit-properties-table">',
  417. '#suffix' => '</div>',
  418. '#theme' => 'chado_node_properties_form_table'
  419. );
  420. // Add defaults into form_state to be used elsewhere
  421. $form['properties']['property_table']['details'] = array(
  422. '#type' => 'hidden',
  423. '#value' => serialize($details)
  424. );
  425. /* Properties can come to us in two ways:
  426. *
  427. * 1) In the form state in the $form_state['chado_properties']. Data is in this field
  428. * when an AJAX call updates the form state or a validation error.
  429. *
  430. * 2) Directly from the database if the record already has properties associated. This
  431. * data is only used the first time the form is loaded. On AJAX calls or validation
  432. * errors the fields on the form are populated from the $form_state['chado_properties']
  433. * entry.
  434. */
  435. if (isset($form_state['chado_properties'])) {
  436. $existing_properties = $form_state['chado_properties'];
  437. }
  438. else {
  439. if (isset($details['cv_name'])) {
  440. $existing_properties = chado_query(
  441. "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
  442. FROM {" . $details['property_table'] . "} PP
  443. INNER JOIN {cvterm} CVT ON CVT.cvterm_id = PP.type_id
  444. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  445. WHERE
  446. PP." . $details['base_foreign_key'] . " = :base_key_value AND
  447. CV.name = '" .$details['cv_name']. "'
  448. ORDER BY CVT.name, PP.rank",
  449. array(':base_key_value' => $details['base_key_value'])
  450. );
  451. } elseif (isset($details['cv_id'])) {
  452. $existing_properties = chado_query(
  453. "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
  454. FROM {" . $details['property_table'] . "} PP
  455. INNER JOIN {cvterm} CVT ON CVT.cvterm_id = PP.type_id
  456. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  457. WHERE
  458. PP." . $details['base_foreign_key'] . " = :base_key_value AND
  459. CV.cv_id = '" .$details['cv_id']. "'
  460. ORDER BY CVT.name, PP.rank",
  461. array(':base_key_value' => $details['base_key_value'])
  462. );
  463. }
  464. }
  465. /* The format of the $existing_properties array is either:
  466. *
  467. * From the chado_properties array:
  468. * $form_state['chado_properties'] = array(
  469. * '[type_id]-[rank]' => array(
  470. * 'type_id' => [the cvterm.cvterm_id value]
  471. * 'type_name' => [the cvterm.name value]
  472. * 'property_id' => [the property.property_id value, or NULL if it doesn't yet exist],
  473. * 'value' => [the BASEprop.value value],
  474. * 'rank' => [the BASEprop.rank value],
  475. * ),
  476. * );
  477. *
  478. * OR
  479. * Populated from the database:
  480. * $existing_property = array(
  481. * 0 => array(
  482. * 'property_id' => [the property.property_id value, or NULL if it doesn't yet exist],
  483. * 'type_id' => [the cvterm.cvterm_id value]
  484. * 'type_name' => [the cvterm.name value]
  485. * 'value' => [the BASEprop.value value],
  486. * 'rank' => [the BASEprop.rank value],
  487. * ),
  488. * );
  489. *
  490. * NOTE: The main difference is the key
  491. *
  492. * Loop on the array elements of the $existing_properties array and add
  493. * an element to the form for each one as long as it's also in the
  494. * $properties_options array.
  495. */
  496. foreach ($existing_properties as $property) {
  497. if (array_key_exists($property->type_id, $property_options)) {
  498. $form['properties']['property_table'][$property->type_id]['#type'] = 'markup';
  499. $form['properties']['property_table'][$property->type_id]['#value'] = '';
  500. $form['properties']['property_table'][$property->type_id][$property->rank]['#type'] = 'markup';
  501. $form['properties']['property_table'][$property->type_id][$property->rank]['#value'] = '';
  502. $form['properties']['property_table'][$property->type_id][$property->rank]['prop_type_id'] = array(
  503. '#type' => 'hidden',
  504. '#value' => $property->type_id
  505. );
  506. $form['properties']['property_table'][$property->type_id][$property->rank]['prop_value'] = array(
  507. '#type' => 'hidden',
  508. '#value' => $property->value
  509. );
  510. $form['properties']['property_table'][$property->type_id][$property->rank]['property_id'] = array(
  511. '#type' => 'hidden',
  512. '#value' => $property->property_id
  513. );
  514. $form['properties']['property_table'][$property->type_id][$property->rank]['type'] = array(
  515. '#type' => 'markup',
  516. '#markup' => $property->type_name
  517. );
  518. $form['properties']['property_table'][$property->type_id][$property->rank]['value'] = array(
  519. '#type' => 'markup',
  520. '#markup' => $property->value
  521. );
  522. $form['properties']['property_table'][$property->type_id][$property->rank]['rank'] = array(
  523. '#type' => 'markup',
  524. '#markup' => $property->rank
  525. );
  526. // remove button
  527. $form['properties']['property_table'][$property->type_id][$property->rank]['property_action'] = array(
  528. '#type' => 'submit',
  529. '#value' => t('Remove'),
  530. '#name' => "property_remove-".$property->type_id.'-'.$property->rank,
  531. '#ajax' => array(
  532. 'callback' => "chado_add_node_form_properties_ajax_update",
  533. 'wrapper' => 'tripal-generic-edit-properties-table',
  534. 'effect' => 'fade',
  535. 'method' => 'replace',
  536. 'prevent' => 'click'
  537. ),
  538. // When this button is clicked, the form will be validated and submitted.
  539. // Therefore, we set custom submit and validate functions to override the
  540. // default node form submit. In the validate function we validate only the
  541. // property fields and in the submit we remove the indicated property
  542. // from the chado_properties array. In order to keep validate errors
  543. // from the node form validate and Drupal required errors for non-property fields
  544. // preventing the user from removing properties we set the #limit_validation_errors below
  545. '#validate' => array('chado_add_node_form_properties_remove_button_validate'),
  546. '#submit' => array('chado_add_node_form_properties_remove_button_submit'),
  547. // Limit the validation of the form upon clicking this button to the property_table tree
  548. // No other fields will be validated (ie: no fields from the main form or any other api
  549. // added form).
  550. '#limit_validation_errors' => array(
  551. array('property_table') // Validate all fields within $form_state['values']['property_table']
  552. )
  553. );
  554. }
  555. }
  556. // Form elements for adding a new property
  557. //---------------------------------------------
  558. $form['properties']['property_table']['new'] = array(
  559. '#type' => 'markup',
  560. '#prefix' => '<span class="addtl-properties-add-new-property">',
  561. '#suffix' => '</span>'
  562. );
  563. $form['properties']['property_table']['new']['type'] = array(
  564. '#type' => 'select',
  565. '#options' => $property_options, // Set at top of form
  566. );
  567. $form['properties']['property_table']['new']['value'] = array(
  568. '#type' => 'textarea',
  569. '#rows' => 1,
  570. );
  571. // add button
  572. $form['properties']['property_table']['new']['property_action'] = array(
  573. '#type' => 'submit',
  574. '#value' => t('Add'),
  575. '#name' => "property-add",
  576. '#ajax' => array(
  577. 'callback' => "chado_add_node_form_properties_ajax_update",
  578. 'wrapper' => 'tripal-generic-edit-properties-table',
  579. 'effect' => 'fade',
  580. 'method' => 'replace',
  581. 'prevent' => 'click'
  582. ),
  583. // When this button is clicked, the form will be validated and submitted.
  584. // Therefore, we set custom submit and validate functions to override the
  585. // default node form submit. In the validate function we validate only the
  586. // additional property fields and in the submit we add them to the chado_properties
  587. // array. In order to keep validate errors from the node form validate and Drupal
  588. // required errors for non-property fields preventing the user from adding properties we
  589. // set the #limit_validation_errors below
  590. '#validate' => array('chado_update_node_form_properties_add_button_validate'),
  591. '#submit' => array('chado_add_node_form_properties_add_button_submit'),
  592. // Limit the validation of the form upon clicking this button to the property_table tree
  593. // No other fields will be validated (ie: no fields from the main form or any other api
  594. // added form).
  595. '#limit_validation_errors' => array(
  596. array('property_table') // Validate all fields within $form_state['values']['property_table']
  597. )
  598. );
  599. }
  600. /**
  601. * Validate the user input for creating a new property
  602. * Called by the add button in chado_add_node_form_properties
  603. *
  604. * @ingroup tripal_core
  605. */
  606. function chado_update_node_form_properties_add_button_validate($form, &$form_state) {
  607. // Ensure the type_id is supplied & Valid
  608. $cvterm = chado_select_record(
  609. 'cvterm',
  610. array('cvterm_id', 'name'),
  611. array('cvterm_id' => $form_state['values']['property_table']['new']['type'])
  612. );
  613. if (!isset($cvterm[0])) {
  614. form_set_error('property_table][new][cvterm', 'Please select a property type before attempting to add a new property.');
  615. }
  616. else {
  617. $form_state['values']['property_table']['new']['type_name'] = $cvterm[0]->name;
  618. }
  619. // Ensure value is supplied
  620. if (empty($form_state['values']['property_table']['new']['value'])) {
  621. form_set_error('property_table][new][value','You must enter the property value before attempting to add a new property.');
  622. }
  623. }
  624. /**
  625. * Called by the add button in chado_add_node_form_properties
  626. *
  627. * Create an array of properties in the form state. This array will then be
  628. * used to rebuild the form in subsequent builds
  629. *
  630. * @ingroup tripal_core
  631. */
  632. function chado_add_node_form_properties_add_button_submit(&$form, &$form_state) {
  633. $details = unserialize($form_state['values']['property_table']['details']);
  634. // if the chado_additional_properties array is not set then this is the first time modifying the
  635. // property table. this means we need to include all the properties from the db
  636. if (!isset($form_state['chado_properties'])) {
  637. chado_add_node_form_properties_create_property_formstate_array($form, $form_state);
  638. }
  639. // get details for the new property
  640. $property = array(
  641. 'type_id' => $form_state['values']['property_table']['new']['type'],
  642. 'type_name' => $form_state['values']['property_table']['new']['type_name'],
  643. 'property_id' => NULL,
  644. 'value' => $form_state['values']['property_table']['new']['value'],
  645. 'rank' => '0',
  646. );
  647. // get max rank
  648. $rank = chado_get_table_max_rank(
  649. $details['property_table'],
  650. array(
  651. $details['base_foreign_key'] => $details['base_key_value'],
  652. 'type_id' => $property['type_id']
  653. )
  654. );
  655. $property['rank'] = strval($rank + 1);
  656. $key = $property['type_id'] . '-' . $property['rank'];
  657. $form_state['chado_properties'][$key] = (object) $property;
  658. $form_state['rebuild'] = TRUE;
  659. }
  660. /**
  661. * There is no user input for the remove buttons so there is no need to validate
  662. * However, both a submit & validate need to be specified so this is just a placeholder
  663. *
  664. * Called by the many remove buttons in chado_add_node_form_properties
  665. *
  666. * @ingroup tripal_core
  667. */
  668. function chado_add_node_form_properties_remove_button_validate($form, $form_state) {
  669. // No Validation needed for remove
  670. }
  671. /**
  672. * Remove the correct property from the form
  673. * Called by the many remove buttons in chado_add_node_form_properties
  674. *
  675. * @ingroup tripal_core
  676. */
  677. function chado_add_node_form_properties_remove_button_submit(&$form, &$form_state) {
  678. // if the chado_properties array is not set then this is the first time modifying the
  679. // property table. this means we need to include all the properties from the db
  680. if (!isset($form_state['chado_properties'])) {
  681. chado_add_node_form_properties_create_property_formstate_array($form, $form_state);
  682. }
  683. // remove the specified property from the form property table
  684. if(preg_match('/property_remove-([^-]+-[^-]+)/',$form_state['triggering_element']['#name'],$match)) {
  685. $key = $match[1];
  686. if (array_key_exists($key, $form_state['chado_properties'])) {
  687. unset($form_state['chado_properties'][$key]);
  688. }
  689. }
  690. $form_state['rebuild'] = TRUE;
  691. }
  692. /**
  693. * Ajax function which returns the section of the form to be re-rendered
  694. *
  695. * @ingroup tripal_core
  696. */
  697. function chado_add_node_form_properties_ajax_update($form, $form_state) {
  698. return $form['properties']['property_table'];
  699. }
  700. /**
  701. * Creates an array in form_state containing the existing properties. This array is
  702. * then modified by the add/remove buttons and used as a source for rebuilding the form.
  703. * This function get's called at each button (add and remove) button submits the first
  704. * time one of the button's is clicked to instantiates the $form_state['chado_properties'] array
  705. *
  706. * $form_state['chado_properties'] = array(
  707. * '[type_id]-[rank]' => array(
  708. * 'type_id' => [the cvterm.cvterm_id value]
  709. * 'type_name' => [the cvterm.name value]
  710. * 'property_id' => [the property.property_id value, or NULL if it doesn't yet exist],
  711. * 'value' => [the BASEprop.value value],
  712. * 'rank' => [the BASEprop.rank value],
  713. * ),
  714. * );
  715. *
  716. * @ingroup tripal_core
  717. */
  718. function chado_add_node_form_properties_create_property_formstate_array($form, &$form_state) {
  719. $form_state['chado_properties'] = array();
  720. foreach (element_children($form['properties']['property_table']) as $type_id) {
  721. if ($type_id != 'new') {
  722. foreach (element_children($form['properties']['property_table'][$type_id]) as $rank) {
  723. $element = $form['properties']['property_table'][$type_id][$rank];
  724. $property = array(
  725. 'type_id' => $element['prop_type_id']['#value'],
  726. 'type_name' => $element['type']['#markup'],
  727. 'property_id' => $element['property_id']['#value'],
  728. 'value' => $element['value']['#markup'],
  729. 'rank' => $element['rank']['#markup']
  730. );
  731. $key = $property['type_id'] . '-' . $property['rank'];
  732. $form_state['chado_properties'][$key] = (object) $property;
  733. }
  734. }
  735. }
  736. }
  737. /**
  738. * Function to theme the add/remove properties form into a table
  739. *
  740. * @ingroup tripal_chado_node_api
  741. */
  742. function theme_chado_add_node_form_properties($variables) {
  743. $element = $variables['element'];
  744. $header = array(
  745. 'type' => t('Type'),
  746. 'value' => t('Value'),
  747. 'property_action' => t('Actions'),
  748. );
  749. $rows = array();
  750. foreach (element_children($element) as $type_id) {
  751. if ($type_id == 'new') {
  752. $row = array();
  753. $row['data'] = array();
  754. foreach ($header as $fieldname => $title) {
  755. $row['data'][] = drupal_render($element[$type_id][$fieldname]);
  756. }
  757. $rows[] = $row;
  758. }
  759. else {
  760. foreach (element_children($element[$type_id]) as $version) {
  761. $row = array();
  762. $row['data'] = array();
  763. foreach ($header as $fieldname => $title) {
  764. $row['data'][] = drupal_render($element[$type_id][$version][$fieldname]);
  765. }
  766. $rows[] = $row;
  767. }
  768. }
  769. }
  770. return theme('table', array(
  771. 'header' => $header,
  772. 'rows' => $rows
  773. ));
  774. }
  775. /**
  776. * This function is used in a hook_insert, hook_update for a node form
  777. * when the chado node properties form has been added to the form. It retrieves all of the properties
  778. * and returns them in an array of the format:
  779. *
  780. * $dbxefs[<type_id>][<rank>] = <value>
  781. *
  782. * This array can then be used for inserting or updating properties
  783. *
  784. * @param $node
  785. *
  786. * @return
  787. * A property array
  788. *
  789. * @ingroup tripal_chado_node_api
  790. */
  791. function chado_retrieve_node_form_properties($node) {
  792. $properties = array();
  793. if (isset($node->property_table)) {
  794. foreach ($node->property_table as $type_id => $elements) {
  795. if ($type_id != 'new' AND $type_id != 'details') {
  796. foreach ($elements as $rank => $element) {
  797. $properties[$type_id][$rank] = $element['prop_value'];
  798. }
  799. }
  800. }
  801. }
  802. return $properties;
  803. }
  804. /**
  805. * This function is used in hook_insert or hook_update and handles inserting of any new
  806. * properties
  807. *
  808. * @param $node
  809. * The node passed into hook_insert & hook_update
  810. * @param $details
  811. * - property_table: the name of the _property linking table (ie: feature_property)
  812. * - base_table: the name of the base table (ie: feature)
  813. * - foreignkey_name: the name of the foreign key used to link to the node content (ie: feature_id)
  814. * - foreignkey_value: the value of the foreign key (ie: 445, if there exists a feature where feature_id=445)
  815. * @param $retrieved_properties
  816. * An array of properties from chado_retrieve_node_form_properties($node). This can be used if you need
  817. * special handling for some of the properties (See FeatureMap chado_featuremap_insert for an example)
  818. *
  819. * @ingroup tripal_chado_node_api
  820. */
  821. function chado_update_node_form_properties($node, $details, $retrieved_properties = FALSE) {
  822. $details['foreignkey_value'] = (isset($details['foreignkey_value'])) ? $details['foreignkey_value'] : 0;
  823. if (isset($node->property_table) AND ($details['foreignkey_value'] > 0)) {
  824. // First remove existing property links
  825. chado_delete_record($details['property_table'], array($details['foreignkey_name'] => $details['foreignkey_value']));
  826. // Add back in property links and insert properties as needed
  827. if ($retrieved_properties) {
  828. $properties = $retrieved_properties;
  829. }
  830. else {
  831. $properties = chado_retrieve_node_form_properties($node);
  832. }
  833. foreach ($properties as $type_id => $ranks) {
  834. foreach ($ranks as $rank => $value) {
  835. $success = chado_insert_record(
  836. $details['property_table'],
  837. array(
  838. $details['foreignkey_name'] => $details['foreignkey_value'],
  839. 'type_id' => $type_id,
  840. 'value' => $value,
  841. 'rank' => $rank
  842. )
  843. );
  844. if (!$success) {
  845. tripal_report_error('tripal_' . $details['base_table'], TRIPAL_ERROR,
  846. $details['base_table'] . ' Insert: Unable to insert property type_id %cvterm with value %value.',
  847. array('%cvterm' => $type_id, '%value' => $value));
  848. }
  849. }
  850. }
  851. }
  852. }