tripal_core.chado_nodes.properties.api.inc 36 KB

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