tripal_core.properties.api.inc 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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_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. * - select_options: must be an array where the [key] is a valid cvterm_id and
  396. * the [value] is the human-readable name of the option. This is generated from the cv_name/id by default
  397. */
  398. function chado_node_properties_form(&$form, &$form_state, $details) {
  399. // Set Defaults for optional fields
  400. $details['fieldset_title'] = 'Properties';
  401. $details['additional_instructions'] = '';
  402. // Get Property Types for the Select List
  403. if (isset($details['select_options'])) {
  404. $property_options = $details['select_options'];
  405. }
  406. else {
  407. if (isset($details['cv_name'])) {
  408. $property_options = array();
  409. $property_options[] = 'Select a Property';
  410. $sql = "
  411. SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
  412. FROM {cvterm} CVT
  413. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  414. WHERE
  415. CV.name = :cv_name AND
  416. NOT CVT.is_obsolete = 1
  417. ORDER BY CVT.name ASC
  418. ";
  419. $prop_types = chado_query($sql, array(':cv_name' => $details['cv_name']));
  420. while ($prop = $prop_types->fetchObject()) {
  421. $property_options[$prop->cvterm_id] = $prop->name;
  422. }
  423. } elseif (isset($details['cv_id'])) {
  424. $property_options = array();
  425. $property_options[] = 'Select a Property';
  426. $sql = "
  427. SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
  428. FROM {cvterm} CVT
  429. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  430. WHERE
  431. CV.cv_id = :cv_id AND
  432. NOT CVT.is_obsolete = 1
  433. ORDER BY CVT.name ASC
  434. ";
  435. $prop_types = chado_query($sql, array(':cv_id' => $details['cv_id']));
  436. while ($prop = $prop_types->fetchObject()) {
  437. $property_options[$prop->cvterm_id] = $prop->name;
  438. }
  439. }
  440. }
  441. // the fieldset of the property elements
  442. $form['properties'] = array(
  443. '#type' => 'fieldset',
  444. '#title' => t($details['fieldset_title']),
  445. '#description' => t('You may add additional properties by selecting a property type
  446. from the dropdown and adding text. You may add as many properties as desired by
  447. clicking the add button on the right. To remove a property, click the remove button.
  448. To add additional properties to the drop down. ' . $details['additional_instructions']),
  449. '#prefix' => "<div id='properties-fieldset'>",
  450. '#suffix' => '</div>'
  451. );
  452. // this form element is a tree, so that we don't puke all of the values into then node variable
  453. // it is set as a tree, and keeps them in the $form_state['values']['property_table'] heading.
  454. $form['properties']['property_table'] = array(
  455. '#type' => 'markup',
  456. '#tree' => TRUE,
  457. '#prefix' => '<div id="tripal-generic-edit-properties-table">',
  458. '#suffix' => '</div>',
  459. '#theme' => 'chado_node_properties_form_table'
  460. );
  461. // Add defaults into form_state to be used elsewhere
  462. $form['properties']['property_table']['details'] = array(
  463. '#type' => 'hidden',
  464. '#value' => serialize($details)
  465. );
  466. /* Properties can come to us in two ways:
  467. *
  468. * 1) In the form state in the $form_state['chado_properties']. Data is in this field
  469. * when an AJAX call updates the form state or a validation error.
  470. *
  471. * 2) Directly from the database if the record already has properties associated. This
  472. * data is only used the first time the form is loaded. On AJAX calls or validation
  473. * errors the fields on the form are populated from the $form_state['chado_properties']
  474. * entry.
  475. */
  476. if (isset($form_state['chado_properties'])) {
  477. $existing_properties = $form_state['chado_properties'];
  478. }
  479. else {
  480. if (isset($details['cv_name'])) {
  481. $existing_properties = chado_query(
  482. "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
  483. FROM {" . $details['property_table'] . "} PP
  484. INNER JOIN {cvterm} CVT ON CVT.cvterm_id = PP.type_id
  485. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  486. WHERE
  487. PP." . $details['base_foreign_key'] . " = :base_key_value AND
  488. CV.name = '" .$details['cv_name']. "'
  489. ORDER BY CVT.name, PP.rank",
  490. array(':base_key_value' => $details['base_key_value'])
  491. );
  492. } elseif (isset($details['cv_id'])) {
  493. $existing_properties = chado_query(
  494. "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
  495. FROM {" . $details['property_table'] . "} PP
  496. INNER JOIN {cvterm} CVT ON CVT.cvterm_id = PP.type_id
  497. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  498. WHERE
  499. PP." . $details['base_foreign_key'] . " = :base_key_value AND
  500. CV.cv_id = '" .$details['cv_id']. "'
  501. ORDER BY CVT.name, PP.rank",
  502. array(':base_key_value' => $details['base_key_value'])
  503. );
  504. }
  505. }
  506. /* The format of the $existing_properties array is either:
  507. *
  508. * From the chado_properties array:
  509. * $form_state['chado_properties'] = array(
  510. * '[type_id]-[rank]' => array(
  511. * 'type_id' => [the cvterm.cvterm_id value]
  512. * 'type_name' => [the cvterm.name value]
  513. * 'property_id' => [the property.property_id value, or NULL if it doesn't yet exist],
  514. * 'value' => [the BASEprop.value value],
  515. * 'rank' => [the BASEprop.rank value],
  516. * ),
  517. * );
  518. *
  519. * OR
  520. * Populated from the database:
  521. * $existing_property = array(
  522. * 0 => array(
  523. * 'property_id' => [the property.property_id value, or NULL if it doesn't yet exist],
  524. * 'type_id' => [the cvterm.cvterm_id value]
  525. * 'type_name' => [the cvterm.name value]
  526. * 'value' => [the BASEprop.value value],
  527. * 'rank' => [the BASEprop.rank value],
  528. * ),
  529. * );
  530. *
  531. * NOTE: The main difference is the key
  532. *
  533. * Loop on the array elements of the $existing_properties array and add
  534. * an element to the form for each one.
  535. */
  536. foreach ($existing_properties as $property) {
  537. $form['properties']['property_table'][$property->type_id]['#type'] = 'markup';
  538. $form['properties']['property_table'][$property->type_id]['#value'] = '';
  539. $form['properties']['property_table'][$property->type_id][$property->rank]['#type'] = 'markup';
  540. $form['properties']['property_table'][$property->type_id][$property->rank]['#value'] = '';
  541. $form['properties']['property_table'][$property->type_id][$property->rank]['prop_type_id'] = array(
  542. '#type' => 'hidden',
  543. '#value' => $property->type_id
  544. );
  545. $form['properties']['property_table'][$property->type_id][$property->rank]['prop_value'] = array(
  546. '#type' => 'hidden',
  547. '#value' => $property->value
  548. );
  549. $form['properties']['property_table'][$property->type_id][$property->rank]['property_id'] = array(
  550. '#type' => 'hidden',
  551. '#value' => $property->property_id
  552. );
  553. $form['properties']['property_table'][$property->type_id][$property->rank]['type'] = array(
  554. '#type' => 'markup',
  555. '#markup' => $property->type_name
  556. );
  557. $form['properties']['property_table'][$property->type_id][$property->rank]['value'] = array(
  558. '#type' => 'markup',
  559. '#markup' => $property->value
  560. );
  561. $form['properties']['property_table'][$property->type_id][$property->rank]['rank'] = array(
  562. '#type' => 'markup',
  563. '#markup' => $property->rank
  564. );
  565. // remove button
  566. $form['properties']['property_table'][$property->type_id][$property->rank]['property_action'] = array(
  567. '#type' => 'submit',
  568. '#value' => t('Remove'),
  569. '#name' => "property_remove-".$property->type_id.'-'.$property->rank,
  570. '#ajax' => array(
  571. 'callback' => "chado_node_properties_form_ajax_update",
  572. 'wrapper' => 'tripal-generic-edit-properties-table',
  573. 'effect' => 'fade',
  574. 'method' => 'replace',
  575. 'prevent' => 'click'
  576. ),
  577. // When this button is clicked, the form will be validated and submitted.
  578. // Therefore, we set custom submit and validate functions to override the
  579. // default node form submit. In the validate function we validate only the
  580. // property fields and in the submit we remove the indicated property
  581. // from the chado_properties array. In order to keep validate errors
  582. // from the node form validate and Drupal required errors for non-property fields
  583. // preventing the user from removing properties we set the #limit_validation_errors below
  584. '#validate' => array('chado_node_properties_form_remove_button_validate'),
  585. '#submit' => array('chado_node_properties_form_remove_button_submit'),
  586. // Limit the validation of the form upon clicking this button to the property_table tree
  587. // No other fields will be validated (ie: no fields from the main form or any other api
  588. // added form).
  589. '#limit_validation_errors' => array(
  590. array('property_table') // Validate all fields within $form_state['values']['property_table']
  591. )
  592. );
  593. }
  594. // Form elements for adding a new property
  595. //---------------------------------------------
  596. $form['properties']['property_table']['new'] = array(
  597. '#type' => 'markup',
  598. '#prefix' => '<span class="addtl-properties-add-new-property">',
  599. '#suffix' => '</span>'
  600. );
  601. $form['properties']['property_table']['new']['type'] = array(
  602. '#type' => 'select',
  603. '#options' => $property_options, // Set at top of form
  604. );
  605. $form['properties']['property_table']['new']['value'] = array(
  606. '#type' => 'textfield',
  607. );
  608. // add button
  609. $form['properties']['property_table']['new']['property_action'] = array(
  610. '#type' => 'submit',
  611. '#value' => t('Add'),
  612. '#name' => "property-add",
  613. '#ajax' => array(
  614. 'callback' => "chado_node_properties_form_ajax_update",
  615. 'wrapper' => 'tripal-generic-edit-properties-table',
  616. 'effect' => 'fade',
  617. 'method' => 'replace',
  618. 'prevent' => 'click'
  619. ),
  620. // When this button is clicked, the form will be validated and submitted.
  621. // Therefore, we set custom submit and validate functions to override the
  622. // default node form submit. In the validate function we validate only the
  623. // additional property fields and in the submit we add them to the chado_properties
  624. // array. In order to keep validate errors from the node form validate and Drupal
  625. // required errors for non-property fields preventing the user from adding properties we
  626. // set the #limit_validation_errors below
  627. '#validate' => array('chado_node_properties_form_add_button_validate'),
  628. '#submit' => array('chado_node_properties_form_add_button_submit'),
  629. // Limit the validation of the form upon clicking this button to the property_table tree
  630. // No other fields will be validated (ie: no fields from the main form or any other api
  631. // added form).
  632. '#limit_validation_errors' => array(
  633. array('property_table') // Validate all fields within $form_state['values']['property_table']
  634. )
  635. );
  636. }
  637. /**
  638. * Validate the user input for creating a new property
  639. * Called by the add button in chado_node_properties_form
  640. */
  641. function chado_node_properties_form_add_button_validate($form, &$form_state) {
  642. // Ensure the type_id is supplied & Valid
  643. $cvterm = tripal_core_chado_select(
  644. 'cvterm',
  645. array('cvterm_id', 'name'),
  646. array('cvterm_id' => $form_state['values']['property_table']['new']['type'])
  647. );
  648. if (!isset($cvterm[0])) {
  649. form_set_error('property_table][new][cvterm', 'Please select a property type before attempting to add a new property.');
  650. }
  651. else {
  652. $form_state['values']['property_table']['new']['type_name'] = $cvterm[0]->name;
  653. }
  654. // Ensure value is supplied
  655. if (empty($form_state['values']['property_table']['new']['value'])) {
  656. form_set_error('property_table][new][value','You must enter the property value before attempting to add a new property.');
  657. }
  658. }
  659. /**
  660. * Called by the add button in chado_node_properties_form
  661. *
  662. * Create an array of properties in the form state. This array will then be
  663. * used to rebuild the form in subsequent builds
  664. */
  665. function chado_node_properties_form_add_button_submit(&$form, &$form_state) {
  666. $details = unserialize($form_state['values']['property_table']['details']);
  667. // if the chado_additional_properties array is not set then this is the first time modifying the
  668. // property table. this means we need to include all the properties from the db
  669. if (!isset($form_state['chado_properties'])) {
  670. chado_node_properties_form_create_property_formstate_array($form, $form_state);
  671. }
  672. // get details for the new property
  673. $property = array(
  674. 'type_id' => $form_state['values']['property_table']['new']['type'],
  675. 'type_name' => $form_state['values']['property_table']['new']['type_name'],
  676. 'property_id' => NULL,
  677. 'value' => $form_state['values']['property_table']['new']['value'],
  678. 'rank' => '0',
  679. );
  680. // get max rank
  681. $rank = tripal_core_get_max_chado_rank(
  682. $details['property_table'],
  683. array(
  684. $details['base_foreign_key'] => $details['base_key_value'],
  685. 'type_id' => $property['type_id']
  686. )
  687. );
  688. $property['rank'] = strval($rank + 1);
  689. $key = $property['type_id'] . '-' . $property['rank'];
  690. $form_state['chado_properties'][$key] = (object) $property;
  691. $form_state['rebuild'] = TRUE;
  692. }
  693. /**
  694. * There is no user input for the remove buttons so there is no need to validate
  695. * However, both a submit & validate need to be specified so this is just a placeholder
  696. *
  697. * Called by the many remove buttons in chado_node_properties_form
  698. */
  699. function chado_node_properties_form_remove_button_validate($form, $form_state) {
  700. // No Validation needed for remove
  701. }
  702. /**
  703. * Remove the correct property from the form
  704. * Called by the many remove buttons in chado_node_properties_form
  705. */
  706. function chado_node_properties_form_remove_button_submit(&$form, &$form_state) {
  707. // if the chado_properties array is not set then this is the first time modifying the
  708. // property table. this means we need to include all the properties from the db
  709. if (!isset($form_state['chado_properties'])) {
  710. chado_node_properties_form_create_property_formstate_array($form, $form_state);
  711. }
  712. // remove the specified property from the form property table
  713. if(preg_match('/property_remove-([^-]+-[^-]+)/',$form_state['triggering_element']['#name'],$match)) {
  714. $key = $match[1];
  715. if (array_key_exists($key, $form_state['chado_properties'])) {
  716. unset($form_state['chado_properties'][$key]);
  717. }
  718. }
  719. $form_state['rebuild'] = TRUE;
  720. }
  721. /**
  722. * Ajax function which returns the section of the form to be re-rendered
  723. */
  724. function chado_node_properties_form_ajax_update($form, $form_state) {
  725. return $form['properties']['property_table'];
  726. }
  727. /**
  728. * Creates an array in form_state containing the existing properties. This array is
  729. * then modified by the add/remove buttons and used as a source for rebuilding the form.
  730. * This function get's called at each button (add and remove) button submits the first
  731. * time one of the button's is clicked to instantiates the $form_state['chado_properties'] array
  732. *
  733. * $form_state['chado_properties'] = array(
  734. * '[type_id]-[rank]' => array(
  735. * 'type_id' => [the cvterm.cvterm_id value]
  736. * 'type_name' => [the cvterm.name value]
  737. * 'property_id' => [the property.property_id value, or NULL if it doesn't yet exist],
  738. * 'value' => [the BASEprop.value value],
  739. * 'rank' => [the BASEprop.rank value],
  740. * ),
  741. * );
  742. */
  743. function chado_node_properties_form_create_property_formstate_array($form, &$form_state) {
  744. $form_state['chado_properties'] = array();
  745. foreach (element_children($form['properties']['property_table']) as $type_id) {
  746. if ($type_id != 'new') {
  747. foreach (element_children($form['properties']['property_table'][$type_id]) as $rank) {
  748. $element = $form['properties']['property_table'][$type_id][$rank];
  749. $property = array(
  750. 'type_id' => $element['prop_type_id']['#value'],
  751. 'type_name' => $element['type']['#markup'],
  752. 'property_id' => $element['property_id']['#value'],
  753. 'value' => $element['value']['#markup'],
  754. 'rank' => $element['rank']['#markup']
  755. );
  756. $key = $property['type_id'] . '-' . $property['rank'];
  757. $form_state['chado_properties'][$key] = (object) $property;
  758. }
  759. }
  760. }
  761. }
  762. /**
  763. * Function to theme the add/remove properties form into a table
  764. */
  765. function theme_chado_node_properties_form_table($variables) {
  766. $element = $variables['element'];
  767. $header = array(
  768. 'type' => t('Type'),
  769. 'value' => t('Value'),
  770. 'rank' => t('Rank'),
  771. 'property_action' => t('Actions'),
  772. );
  773. $rows = array();
  774. foreach (element_children($element) as $type_id) {
  775. if ($type_id == 'new') {
  776. $row = array();
  777. $row['data'] = array();
  778. foreach ($header as $fieldname => $title) {
  779. $row['data'][] = drupal_render($element[$type_id][$fieldname]);
  780. }
  781. $rows[] = $row;
  782. }
  783. else {
  784. foreach (element_children($element[$type_id]) as $version) {
  785. $row = array();
  786. $row['data'] = array();
  787. foreach ($header as $fieldname => $title) {
  788. $row['data'][] = drupal_render($element[$type_id][$version][$fieldname]);
  789. }
  790. $rows[] = $row;
  791. }
  792. }
  793. }
  794. return theme('table', array(
  795. 'header' => $header,
  796. 'rows' => $rows
  797. ));
  798. }
  799. /**
  800. * This function is used in a hook_insert, hook_update for a node form
  801. * when the chado node properties form has been added to the form. It retrieves all of the properties
  802. * and returns them in an array of the format:
  803. *
  804. * $dbxefs[<type_id>][<rank>] = <value>
  805. *
  806. * This array can then be used for inserting or updating properties
  807. *
  808. * @param $node
  809. *
  810. * @return
  811. * A property array
  812. *
  813. * @ingroup tripal_properties_api
  814. */
  815. function chado_node_properties_form_retreive($node) {
  816. $properties = array();
  817. if (isset($node->property_table)) {
  818. foreach ($node->property_table as $type_id => $elements) {
  819. if ($type_id != 'new' AND $type_id != 'details') {
  820. foreach ($elements as $rank => $element) {
  821. $properties[$type_id][$rank] = $element['prop_value'];
  822. }
  823. }
  824. }
  825. }
  826. return $properties;
  827. }
  828. /**
  829. * This function is used in hook_insert or hook_update and handles inserting of any new
  830. * properties
  831. *
  832. * @param $node
  833. * The node passed into hook_insert & hook_update
  834. * @param $details
  835. * - property_table: the name of the _property linking table (ie: feature_property)
  836. * - base_table: the name of the base table (ie: feature)
  837. * - foreignkey_name: the name of the foreign key used to link to the node content (ie: feature_id)
  838. * - foreignkey_value: the value of the foreign key (ie: 445, if there exists a feature where feature_id=445)
  839. * @param $retrieved_properties
  840. * An array of properties from chado_node_properties_form_retreive($node). This can be used if you need
  841. * special handling for some of the properties (See FeatureMap chado_featuremap_insert for an example)
  842. */
  843. function chado_node_properties_form_update_properties($node, $details, $retrieved_properties = FALSE) {
  844. $details['foreignkey_value'] = (isset($details['foreignkey_value'])) ? $details['foreignkey_value'] : 0;
  845. if (isset($node->property_table) AND ($details['foreignkey_value'] > 0)) {
  846. // First remove existing property links
  847. tripal_core_chado_delete($details['property_table'], array($details['foreignkey_name'] => $details['foreignkey_value']));
  848. // Add back in property links and insert properties as needed
  849. if ($retrieved_properties) {
  850. $properties = $retrieved_properties;
  851. }
  852. else {
  853. $properties = chado_node_properties_form_retreive($node);
  854. }
  855. foreach ($properties as $type_id => $ranks) {
  856. foreach ($ranks as $rank => $value) {
  857. $success = tripal_core_chado_insert(
  858. $details['property_table'],
  859. array(
  860. $details['foreignkey_name'] => $details['foreignkey_value'],
  861. 'type_id' => $type_id,
  862. 'value' => $value,
  863. 'rank' => $rank
  864. )
  865. );
  866. if (!$success) {
  867. watchdog(
  868. 'tripal_' . $details['base_table'],
  869. $details['base_table'] . ' Insert: Unable to insert property type_id %cvterm with value %value.',
  870. array('%cvterm' => $type_id, '%value' => $value),
  871. WATCHDOG_ERROR
  872. );
  873. }
  874. }
  875. }
  876. }
  877. }