tripal_core.chado_nodes.properties.api.inc 36 KB

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