tripal_core.chado_nodes.properties.api.inc 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. <?php
  2. /**
  3. * @file
  4. * API to manage the chado prop table for various Tripal Node Types
  5. *
  6. * How To Use:
  7. * @code
  8. function chado_example_form($form, $form_state) {
  9. // Default values for form elements can come in the following ways:
  10. //
  11. // 1) as elements of the $node object. This occurs when editing an existing node
  12. // 2) in the $form_state['values'] array which occurs on a failed validation or
  13. // ajax callbacks when the ajax call originates from non-submit fields other
  14. // than button
  15. // 3) in the $form_state['input'] array which occurs on ajax callbacks from submit
  16. // form elements (e.g. buttons) and the form is being rebuilt but has not yet
  17. // been validated
  18. //
  19. // The reference elements added by this function do use AJAX calls from buttons,
  20. // therefore, it is important to check for form values in the $form_state['values']
  21. // for case #2 above, and in the $form_state['input'] for case #3.
  22. // See the chado analysis node form for an example.
  23. // Next, add in all the form array definition particular to your node type
  24. // To add in the chado properties form elements, you first need to prepare the arguments
  25. // for the function call.
  26. $details = array(
  27. 'property_table' => 'example_property', // the name of the table linking additional properties to this node
  28. 'base_foreign_key' => 'example_id', // key to link to the chado content created by this node
  29. 'base_key_value' => $example_id, // the value of the above key
  30. 'cv_name' => 'example_prop_cv', // the name of the cv governing the _prop.type_id
  31. 'fieldset_title' => 'Additional References', // the non-translated title for this fieldset
  32. 'additional_instructions' => '' // a non-stranslated string providing additional instructions
  33. );
  34. // Finally, and add the additional form elements to the form
  35. chado_add_node_form_properties($form, $form_state, $details);
  36. return $form;
  37. }
  38. function chado_example_insert($node) {
  39. // if there is an example_id in the $node object then this must be a sync so
  40. // we can skip adding the chado_example as it is already there, although
  41. // we do need to proceed with the rest of the insert
  42. if (!property_exists($node, 'example_id')) {
  43. // Add record to chado example table
  44. // Add to any other tables needed
  45. // Add all properties
  46. // Existing _property links will be cleared and then re-added
  47. tripal_api_chado_node_properties_form_update_properties(
  48. $node, // the node object passed in via hook_insert()
  49. 'example_property', // the name of the _property linking table
  50. 'example', // the name of the base chado table for the node
  51. 'example_id', // key to link to the chado content created by this node
  52. $node->example_id // value of the above key
  53. );
  54. }
  55. // Add record to chado_example linking example_id to new node
  56. }
  57. function chado_example_update($node) {
  58. // Update record in chado example table
  59. // Update any other tables needed
  60. // Update all properties
  61. // Existing _property links will be cleared and then re-added
  62. tripal_api_chado_node_properties_form_update_properties(
  63. $node, // the node object passed in via hook_insert()
  64. 'example_property', // the name of the _property linking table
  65. 'example', // the name of the base chado table for the node
  66. 'example_id', // key to link to the chado content created by this node
  67. $node->example_id // value of the above key
  68. );
  69. // Don't need to update chado_example linking table since niether example_id or nid can be changed in update
  70. }
  71. * @endcode
  72. *
  73. * @ingroup tripal_chado_node_api
  74. */
  75. /**
  76. * Retrieve a property for a given base table record
  77. *
  78. * @param $basetable
  79. * The base table for which the property should be retrieved. Thus to retrieve a property
  80. * for a feature the basetable=feature and property is retrieved from featureprop
  81. * @param $record_id
  82. * The foriegn key field of the base table. This should be in integer.
  83. * @param $property
  84. * The cvterm name describing the type of properties to be retrieved
  85. * @param $cv_name
  86. * The name of the cv that the above cvterm is part of
  87. *
  88. * @return
  89. * An array in the same format as that generated by the function
  90. * chado_generate_var(). If only one record is returned it
  91. * is a single object. If more than one record is returned then it is an array
  92. * of objects
  93. *
  94. * @ingroup tripal_chado_node_api
  95. */
  96. function chado_get_property($basetable, $record_id, $property, $cv_name) {
  97. // get the foreign key for this property table
  98. $table_desc = chado_get_schema($basetable . 'prop');
  99. $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
  100. // construct the array of values to be selected
  101. $values = array(
  102. $fkcol => $record_id,
  103. 'type_id' => array(
  104. 'cv_id' => array(
  105. 'name' => $cv_name,
  106. ),
  107. 'name' => $property,
  108. 'is_obsolete' => 0
  109. ),
  110. );
  111. $results = chado_generate_var($basetable . 'prop', $values);
  112. if ($results) {
  113. $results = chado_expand_var($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 chado_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 = chado_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 chado_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 = chado_select_record('cvterm', array('cvterm_id'), $values, $options);
  184. if (!$term or count($term) == 0) {
  185. tripal_report_error('tripal_core', TRIPAL_ERROR, "Cannot find property '%prop_name' in vocabulary '%cvname'.",
  186. array('%prop_name' => $property, '%cvname' => $cv_name));
  187. return FALSE;
  188. }
  189. // get the foreign key for this property table
  190. $table_desc = chado_get_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 = chado_insert_record($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 chado_update_propertyID
  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 chado_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 = chado_get_property($basetable, $record_id, $property, $cv_name);
  242. if (count($prop)==0) {
  243. if ($insert_if_missing) {
  244. return chado_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 = chado_get_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 chado_update_record($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 chado_update_propertyID($basetable, $record_id, $property,
  294. $cv_name, $value) {
  295. // get the primary key for this property table
  296. $table_desc = chado_get_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 chado_update_record($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 chado_delete_property($basetable, $record_id, $property, $cv_name) {
  335. // get the foreign key for this property table
  336. $table_desc = chado_get_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 chado_delete_record($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 chado_delete_propertyID($basetable, $record_id) {
  365. // get the foreign key for this property table
  366. $table_desc = chado_get_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 chado_delete_record($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_add_node_form_properties(&$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_add_node_form_properties_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_add_node_form_properties_remove_button_validate'),
  592. '#submit' => array('chado_add_node_form_properties_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_add_node_form_properties_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_update_node_form_properties_add_button_validate'),
  637. '#submit' => array('chado_add_node_form_properties_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_add_node_form_properties
  649. *
  650. * @ingroup tripal_core
  651. */
  652. function chado_update_node_form_properties_add_button_validate($form, &$form_state) {
  653. // Ensure the type_id is supplied & Valid
  654. $cvterm = chado_select_record(
  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_add_node_form_properties
  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_core
  677. */
  678. function chado_add_node_form_properties_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_add_node_form_properties_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 = chado_get_table_max_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_add_node_form_properties
  711. *
  712. * @ingroup tripal_core
  713. */
  714. function chado_add_node_form_properties_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_add_node_form_properties
  720. *
  721. * @ingroup tripal_core
  722. */
  723. function chado_add_node_form_properties_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_add_node_form_properties_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. * @ingroup tripal_core
  742. */
  743. function chado_add_node_form_properties_ajax_update($form, $form_state) {
  744. return $form['properties']['property_table'];
  745. }
  746. /**
  747. * Creates an array in form_state containing the existing properties. This array is
  748. * then modified by the add/remove buttons and used as a source for rebuilding the form.
  749. * This function get's called at each button (add and remove) button submits the first
  750. * time one of the button's is clicked to instantiates the $form_state['chado_properties'] array
  751. *
  752. * $form_state['chado_properties'] = array(
  753. * '[type_id]-[rank]' => array(
  754. * 'type_id' => [the cvterm.cvterm_id value]
  755. * 'type_name' => [the cvterm.name value]
  756. * 'property_id' => [the property.property_id value, or NULL if it doesn't yet exist],
  757. * 'value' => [the BASEprop.value value],
  758. * 'rank' => [the BASEprop.rank value],
  759. * ),
  760. * );
  761. *
  762. * @ingroup tripal_core
  763. */
  764. function chado_add_node_form_properties_create_property_formstate_array($form, &$form_state) {
  765. $form_state['chado_properties'] = array();
  766. foreach (element_children($form['properties']['property_table']) as $type_id) {
  767. if ($type_id != 'new') {
  768. foreach (element_children($form['properties']['property_table'][$type_id]) as $rank) {
  769. $element = $form['properties']['property_table'][$type_id][$rank];
  770. $property = array(
  771. 'type_id' => $element['prop_type_id']['#value'],
  772. 'type_name' => $element['type']['#markup'],
  773. 'property_id' => $element['property_id']['#value'],
  774. 'value' => $element['value']['#markup'],
  775. 'rank' => $element['rank']['#markup']
  776. );
  777. $key = $property['type_id'] . '-' . $property['rank'];
  778. $form_state['chado_properties'][$key] = (object) $property;
  779. }
  780. }
  781. }
  782. }
  783. /**
  784. * Function to theme the add/remove properties form into a table
  785. *
  786. * @ingroup tripal_chado_node_api
  787. */
  788. function theme_chado_add_node_form_properties($variables) {
  789. $element = $variables['element'];
  790. $header = array(
  791. 'type' => t('Type'),
  792. 'value' => t('Value'),
  793. 'property_action' => t('Actions'),
  794. );
  795. $rows = array();
  796. foreach (element_children($element) as $type_id) {
  797. if ($type_id == 'new') {
  798. $row = array();
  799. $row['data'] = array();
  800. foreach ($header as $fieldname => $title) {
  801. $row['data'][] = drupal_render($element[$type_id][$fieldname]);
  802. }
  803. $rows[] = $row;
  804. }
  805. else {
  806. foreach (element_children($element[$type_id]) as $version) {
  807. $row = array();
  808. $row['data'] = array();
  809. foreach ($header as $fieldname => $title) {
  810. $row['data'][] = drupal_render($element[$type_id][$version][$fieldname]);
  811. }
  812. $rows[] = $row;
  813. }
  814. }
  815. }
  816. return theme('table', array(
  817. 'header' => $header,
  818. 'rows' => $rows
  819. ));
  820. }
  821. /**
  822. * This function is used in a hook_insert, hook_update for a node form
  823. * when the chado node properties form has been added to the form. It retrieves all of the properties
  824. * and returns them in an array of the format:
  825. *
  826. * $dbxefs[<type_id>][<rank>] = <value>
  827. *
  828. * This array can then be used for inserting or updating properties
  829. *
  830. * @param $node
  831. *
  832. * @return
  833. * A property array
  834. *
  835. * @ingroup tripal_chado_node_api
  836. */
  837. function chado_retrieve_node_form_properties($node) {
  838. $properties = array();
  839. if (isset($node->property_table)) {
  840. foreach ($node->property_table as $type_id => $elements) {
  841. if ($type_id != 'new' AND $type_id != 'details') {
  842. foreach ($elements as $rank => $element) {
  843. $properties[$type_id][$rank] = $element['prop_value'];
  844. }
  845. }
  846. }
  847. }
  848. return $properties;
  849. }
  850. /**
  851. * This function is used in hook_insert or hook_update and handles inserting of any new
  852. * properties
  853. *
  854. * @param $node
  855. * The node passed into hook_insert & hook_update
  856. * @param $details
  857. * - property_table: the name of the _property linking table (ie: feature_property)
  858. * - base_table: the name of the base table (ie: feature)
  859. * - foreignkey_name: the name of the foreign key used to link to the node content (ie: feature_id)
  860. * - foreignkey_value: the value of the foreign key (ie: 445, if there exists a feature where feature_id=445)
  861. * @param $retrieved_properties
  862. * An array of properties from chado_retrieve_node_form_properties($node). This can be used if you need
  863. * special handling for some of the properties (See FeatureMap chado_featuremap_insert for an example)
  864. *
  865. * @ingroup tripal_chado_node_api
  866. */
  867. function chado_update_node_form_properties($node, $details, $retrieved_properties = FALSE) {
  868. $details['foreignkey_value'] = (isset($details['foreignkey_value'])) ? $details['foreignkey_value'] : 0;
  869. if (isset($node->property_table) AND ($details['foreignkey_value'] > 0)) {
  870. // First remove existing property links
  871. chado_delete_record($details['property_table'], array($details['foreignkey_name'] => $details['foreignkey_value']));
  872. // Add back in property links and insert properties as needed
  873. if ($retrieved_properties) {
  874. $properties = $retrieved_properties;
  875. }
  876. else {
  877. $properties = chado_retrieve_node_form_properties($node);
  878. }
  879. foreach ($properties as $type_id => $ranks) {
  880. foreach ($ranks as $rank => $value) {
  881. $success = chado_insert_record(
  882. $details['property_table'],
  883. array(
  884. $details['foreignkey_name'] => $details['foreignkey_value'],
  885. 'type_id' => $type_id,
  886. 'value' => $value,
  887. 'rank' => $rank
  888. )
  889. );
  890. if (!$success) {
  891. tripal_report_error('tripal_' . $details['base_table'], TRIPAL_ERROR,
  892. $details['base_table'] . ' Insert: Unable to insert property type_id %cvterm with value %value.',
  893. array('%cvterm' => $type_id, '%value' => $value));
  894. }
  895. }
  896. }
  897. }
  898. }