tripal_core.properties.api.inc 35 KB

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