tripal_core_properties.api.inc 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. <?php
  2. /**
  3. * Retrieve a property for a given base table record
  4. *
  5. * @param $basetable
  6. * The base table for which the property should be retrieved. Thus to retrieve a property
  7. * for a feature the basetable=feature and property is retrieved from featureprop
  8. * @param $record_id
  9. * The foriegn key field of the base table. This should be in integer.
  10. * @param $property
  11. * The cvterm name describing the type of properties to be retrieved
  12. * @param $cv_name
  13. * The name of the cv that the above cvterm is part of
  14. *
  15. * @return
  16. * An array in the same format as that generated by the function
  17. * tripal_core_generate_chado_var(). If only one record is returned it
  18. * is a single object. If more than one record is returned then it is an array
  19. * of objects
  20. *
  21. * @ingroup tripal_properties_api
  22. */
  23. function tripal_core_get_property($basetable, $record_id, $property, $cv_name) {
  24. // get the foreign key for this property table
  25. $table_desc = tripal_core_get_chado_table_schema($basetable . 'prop');
  26. $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
  27. // construct the array of values to be selected
  28. $values = array(
  29. $fkcol => $record_id,
  30. 'type_id' => array(
  31. 'cv_id' => array(
  32. 'name' => $cv_name,
  33. ),
  34. 'name' => $property,
  35. 'is_obsolete' => 0
  36. ),
  37. );
  38. $results = tripal_core_generate_chado_var($basetable . 'prop', $values);
  39. if ($results) {
  40. $results = tripal_core_expand_chado_vars($results, 'field', $basetable . 'prop.value');
  41. }
  42. return $results;
  43. }
  44. /**
  45. * Insert a property for a given base table. By default if the property already
  46. * exists a new property is added with the next available rank. If
  47. * $update_if_present argument is specified then the record will be updated if it
  48. * exists rather than adding a new property.
  49. *
  50. * @param $basetable
  51. * The base table for which the property should be inserted. Thus to insert a property
  52. * for a feature the basetable=feature and property is inserted into featureprop
  53. * @param $record_id
  54. * The foriegn key value of the base table. This should be in integer.
  55. * @param $property
  56. * The cvterm name describing the type of properties to be inserted
  57. * @param $cv_name
  58. * The name of the cv that the above cvterm is part of
  59. * @param $value
  60. * The value of the property to be inserted (can be empty)
  61. * @param $update_if_present
  62. * A boolean indicating whether an existing record should be updated. If the
  63. * property already exists and this value is not specified or is zero then
  64. * a new property will be added with the next largest rank.
  65. *
  66. * @return
  67. * Return True on Insert/Update and False otherwise
  68. *
  69. * @ingroup tripal_properties_api
  70. */
  71. function tripal_core_insert_property($basetable, $record_id, $property,
  72. $cv_name, $value, $update_if_present = 0) {
  73. // first see if the property already exists, if the user want's to update
  74. // then we can do that, but otherwise we want to increment the rank and
  75. // insert
  76. $props = tripal_core_get_property($basetable, $record_id, $property, $cv_name);
  77. if (!is_array($props) and $props) {
  78. $props = array($props);
  79. }
  80. $rank = 0;
  81. if (count($props) > 0) {
  82. if ($update_if_present) {
  83. return tripal_core_update_property($basetable, $record_id, $property, $cv_name, $value);
  84. }
  85. else {
  86. // iterate through the properties returned and check to see if the
  87. // property with this value already exists if not, get the largest rank
  88. // and insert the same property but with this new value
  89. foreach ($props as $p) {
  90. if ($p->rank > $rank) {
  91. $rank = $p->rank;
  92. }
  93. if (strcmp($p->value, $value) == 0) {
  94. return TRUE;
  95. }
  96. }
  97. // now add 1 to the rank
  98. $rank++;
  99. }
  100. }
  101. // make sure the cvterm exists. Otherwise we'll get an error with
  102. // prepared statements not matching
  103. $values = array(
  104. 'cv_id' => array(
  105. 'name' => $cv_name,
  106. ),
  107. 'name' => $property,
  108. );
  109. $options = array();
  110. $term = tripal_core_chado_select('cvterm', array('cvterm_id'), $values, $options);
  111. if (!$term or count($term) == 0) {
  112. watchdog('tripal_core', "Cannot find property '%prop_name'.",
  113. array('%prop_name' => $property), WATCHDOG_ERROR);
  114. return FALSE;
  115. }
  116. // get the foreign key for this property table
  117. $table_desc = tripal_core_get_chado_table_schema($basetable . 'prop');
  118. $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
  119. // construct the array of values to be inserted
  120. $values = array(
  121. $fkcol => $record_id,
  122. 'type_id' => array(
  123. 'cv_id' => array(
  124. 'name' => $cv_name,
  125. ),
  126. 'name' => $property,
  127. ),
  128. 'value' => $value,
  129. 'rank' => $rank,
  130. );
  131. $options = array();
  132. $result = tripal_core_chado_insert($basetable . 'prop', $values, $options);
  133. return $result;
  134. }
  135. /**
  136. * Update a property for a given base table record and property name. This
  137. * function should be used only if one record of the property will be present.
  138. * If the property name can have multiple entries (with increasing rank) then
  139. * use the function named tripal_core_update_property_by_id
  140. *
  141. * @param $basetable
  142. * The base table for which the property should be updated. The property table
  143. * is constructed using a combination of the base table name and the suffix
  144. * 'prop' (e.g. basetable = feature then property tabie is featureprop).
  145. * @param $record_id
  146. * The foreign key of the basetable to update a property for. This should be in integer.
  147. * For example, if the basetable is 'feature' then the $record_id should be the feature_id
  148. * @param $property
  149. * The cvterm name of property to be updated
  150. * @param $cv_name
  151. * The name of the cv that the above cvterm is part of
  152. * @param $value
  153. * The value of the property to be inserted (can be empty)
  154. * @param $insert_if_missing
  155. * A boolean indicating whether a record should be inserted if one doesn't exist to update
  156. *
  157. * Note: The property to be updated is select via the unique combination of $record_id and
  158. * $property and then it is updated with the supplied value
  159. *
  160. * @return
  161. * Return True on Update/Insert and False otherwise
  162. *
  163. * @ingroup tripal_properties_api
  164. */
  165. function tripal_core_update_property($basetable, $record_id, $property,
  166. $cv_name, $value, $insert_if_missing = 0) {
  167. // first see if the property is missing (we can't update a missing property
  168. $prop = tripal_core_get_property($basetable, $record_id, $property, $cv_name);
  169. if (count($prop)==0) {
  170. if ($insert_if_missing) {
  171. return tripal_core_insert_property($basetable, $record_id, $property, $cv_name, $value);
  172. }
  173. else {
  174. return FALSE;
  175. }
  176. }
  177. // get the foreign key for this property table
  178. $table_desc = tripal_core_get_chado_table_schema($basetable . 'prop');
  179. $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
  180. // construct the array that will match the exact record to update
  181. $match = array(
  182. $fkcol => $record_id,
  183. 'type_id' => array(
  184. 'cv_id' => array(
  185. 'name' => $cv_name,
  186. ),
  187. 'name' => $property,
  188. ),
  189. );
  190. // construct the array of values to be updated
  191. $values = array(
  192. 'value' => $value,
  193. );
  194. return tripal_core_chado_update($basetable . 'prop', $match, $values);
  195. }
  196. /**
  197. * Update a property for a given base table record. This function should be
  198. * used if multiple records of the same property will be present. Also, use this
  199. * function to change the property name of an existing property.
  200. *
  201. * @param $basetable
  202. * The base table for which the property should be updated. The property table
  203. * is constructed using a combination of the base table name and the suffix
  204. * 'prop' (e.g. basetable = feature then property tabie is featureprop).
  205. * @param $record_id
  206. * The primary key of the base table. This should be in integer.
  207. * For example, if the basetable is 'feature' then the $record_id should be the featureprop_id
  208. * @param $property
  209. * The cvterm name of property to be updated
  210. * @param $cv_name
  211. * The name of the cv that the above cvterm is part of
  212. * @param $value
  213. * The value of the property to be inserted (can be empty)
  214. *
  215. * @return
  216. * Return True on Update/Insert and False otherwise
  217. *
  218. * @ingroup tripal_properties_api
  219. */
  220. function tripal_core_update_property_by_id($basetable, $record_id, $property,
  221. $cv_name, $value) {
  222. // get the primary key for this property table
  223. $table_desc = tripal_core_get_chado_table_schema($basetable . 'prop');
  224. $pkcol = $table_desc['primary key'][0];
  225. // construct the array that will match the exact record to update
  226. $match = array(
  227. $pkcol => $record_id,
  228. );
  229. // construct the array of values to be updated
  230. $values = array(
  231. 'type_id' => array(
  232. 'cv_id' => array(
  233. 'name' => $cv_name,
  234. ),
  235. 'name' => $property,
  236. ),
  237. 'value' => $value,
  238. );
  239. return tripal_core_chado_update($basetable . 'prop', $match, $values);
  240. }
  241. /**
  242. * Deletes a property for a given base table record using the property name
  243. *
  244. * @param $basetable
  245. * The base table for which the property should be deleted. Thus to deleted a property
  246. * for a feature the basetable=feature and property is deleted from featureprop
  247. * @param $record_id
  248. * The primary key of the basetable to delete a property for. This should be in integer.
  249. * @param $property
  250. * The cvterm name describing the type of property to be deleted
  251. * @param $cv_name
  252. * The name of the cv that the above cvterm is part of
  253. *
  254. * Note: The property to be deleted is select via the unique combination of $record_id and $property
  255. *
  256. * @return
  257. * Return True on Delete and False otherwise
  258. *
  259. * @ingroup tripal_properties_api
  260. */
  261. function tripal_core_delete_property($basetable, $record_id, $property, $cv_name) {
  262. // get the foreign key for this property table
  263. $table_desc = tripal_core_get_chado_table_schema($basetable . 'prop');
  264. $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
  265. // construct the array that will match the exact record to update
  266. $match = array(
  267. $fkcol => $record_id,
  268. 'type_id' => array(
  269. 'cv_id' => array(
  270. 'name' => $cv_name,
  271. ),
  272. 'name' => $property,
  273. ),
  274. );
  275. return tripal_core_chado_delete($basetable . 'prop', $match);
  276. }
  277. /**
  278. * Deletes a property using the property ID
  279. *
  280. * @param $basetable
  281. * The base table for which the property should be deleted. Thus to deleted a property
  282. * for a feature the basetable=feature and property is deleted from featureprop
  283. * @param $record_id
  284. * The primary key of the basetable to delete a property for. This should be in integer.
  285. *
  286. * @return
  287. * Return True on Delete and False otherwise
  288. *
  289. * @ingroup tripal_properties_api
  290. */
  291. function tripal_core_delete_property_by_id($basetable, $record_id) {
  292. // get the foreign key for this property table
  293. $table_desc = tripal_core_get_chado_table_schema($basetable . 'prop');
  294. $pkcol = $table_desc['primary key'][0];
  295. // construct the array that will match the exact record to update
  296. $match = array(
  297. $pkcol => $record_id,
  298. );
  299. return tripal_core_chado_delete($basetable . 'prop', $match);
  300. }
  301. /**
  302. * This function is a wrapper for adding fields to an existing form for managing properties.
  303. * Many of the chado tables have a corresponding 'prop' table (e.g. analysisprop, contactprop,
  304. * organismprop, etc) and those prop tables all have the same schema. Use this function
  305. * to add all the necessary components to a form for allowing the user to add/edit properties to
  306. * a given record. To retreive properties in hook_insert or hook_update of a node form use
  307. * use the function tripal_core_properties_form_retreive().
  308. *
  309. * @param $form
  310. * The Drupal form array into which the properties elements will be added
  311. * @param $form_state
  312. * The corresponding form_state array for the form
  313. * @param $prop_table
  314. * The name of the property table (e.g. anlaysisprop, contactprop)
  315. * @param $id_field
  316. * The name of the ID field in the property table (e.g. analysis_id, contact_id)
  317. * @param $cv_name
  318. * The name of the controlled vocabulary that these properties are derived from
  319. * @param $available_props
  320. * An array of properties to inclde in the properties drop down. This array should
  321. * have cvterm_id's as the key and the cvterm name as the value
  322. * @param $id
  323. * The current base table ID. For example, if the property table is analysisprop then the
  324. * value should be that of the analysis_id. If the property table is contactprop then the
  325. * value should be that of the contact_id. This is the record from which currently assigned
  326. * properties will be retrieved.
  327. * @param $exclude
  328. * An array of cvterms to exclude when retreiving terms already saved in the database.
  329. * Use this array when properties are present but should be handled elsewhere.
  330. * For example, for contacts, the description field is stored as a property because
  331. * the actual field is only 255 characters. The 'contact_description' therefore should
  332. * not be shown in the list of properties, even if present, because it is handled by
  333. * a different form element.
  334. * @param $instructions
  335. * An additional set of instructions for the form properties.
  336. *
  337. * @ingroup tripal_properties_api
  338. */
  339. function tripal_core_properties_form(&$form, &$form_state, $prop_table, $id_field, $cv_name,
  340. $available_props, $id = NULL, $exclude = array(), $instructions = '') {
  341. $d_removed = array(); // lists removed properties
  342. $num_new = 0; // the number of new rows
  343. // if we are re constructing the form from a failed validation or ajax callback
  344. // then use the $form_state['values'] values
  345. if (array_key_exists('values', $form_state)) {
  346. $d_removed = $form_state['values']['removed'];
  347. $num_new = $form_state['values']['num_new'] ? $form_state['values']['num_new'] : 0;
  348. }
  349. // if we are re building the form from after submission (from ajax call) then
  350. // the values are in the $form_state['input'] array
  351. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  352. $d_removed = $form_state['input']['removed'];
  353. $num_new = $form_state['input']['num_new'] ? $form_state['input']['num_new'] : 0;
  354. }
  355. $form['properties'] = array(
  356. '#type' => 'fieldset',
  357. '#title' => t('Additional Details'),
  358. '#description' => t('You may add additional properties by
  359. selecting a property type from the dropdown and adding text. You may add
  360. as many properties as desired by clicking the add button on the right. To
  361. remove a property, click the remove button. ' . $instructions),
  362. );
  363. $form['properties']['table'] = array(
  364. '#type' => 'markup',
  365. '#value' => '',
  366. '#prefix' => '<div id="tripal-generic-edit-properties-table">',
  367. '#suffix' => '</div>',
  368. );
  369. // this array keeps track of all properties we have and allows the functions
  370. // below to select the next rank if a property is dupliated
  371. $ranks = array();
  372. // add in the properties from the Chado prop table (only pertains to existing analyses)
  373. if ($id) {
  374. tripal_core_properties_form_add_prop_table_props($prop_table, $id_field, $cv_name,
  375. $form, $form_state, $id, $ranks, $d_removed, $exclude);
  376. }
  377. // add in any new properties that have been added by the user through an AHAH callback
  378. tripal_core_properties_form_add_new_props($form, $form_state, $ranks, $d_removed);
  379. // add an empty row of field to allow for addition of a new property
  380. tripal_core_properties_form_add_new_empty_props($form, $form_state, $available_props);
  381. $form['properties']['table']['#theme'] = 'tripal_core_properties_form';
  382. }
  383. /**
  384. * This function is responsible for adding a blank row to the properties table for
  385. * adding a new property.
  386. */
  387. function tripal_core_properties_form_add_new_empty_props(&$form, &$form_state, $properties_select) {
  388. // get the field defaults either from $form_state['values'] or $form_state['input']
  389. $description = '';
  390. $text = '';
  391. $id = 0;
  392. if (array_key_exists('values', $form_state)) {
  393. $id = $form_state['values']['new_id'];
  394. $text = $form_state['values']['new_value'];
  395. }
  396. // if we have a property ID then get it's definition to display to the user
  397. if($id) {
  398. $values = array('cvterm_id' => $id);
  399. $cvterm = tripal_core_chado_select('cvterm', array('definition'), $values);
  400. if ($cvterm[0]->definition) {
  401. $description = $cvterm[0]->definition;
  402. }
  403. }
  404. $rows = 1;
  405. // add one more blank set of property fields
  406. $form['properties']['table']['new']["new_id"] = array(
  407. '#type' => 'select',
  408. '#options' => $properties_select,
  409. '#default_value' => $id,
  410. '#ajax' => array(
  411. 'callback' => "tripal_core_props_property_ajax_get_description",
  412. 'wrapper' => 'tripal-properties-new_value',
  413. 'effect' => 'fade',
  414. 'method' => 'replace',
  415. ),
  416. );
  417. $form['properties']['table']['new']["new_value"] = array(
  418. '#type' => 'textarea',
  419. '#default_value' => $text,
  420. '#cols' => 50,
  421. '#rows' => $rows,
  422. '#prefix' => '<div id="tripal-properties-new_value">',
  423. '#description' => $description,
  424. '#suffix' => '</div>',
  425. );
  426. $form['properties']['table']['new']["add"] = array(
  427. '#type' => 'button',
  428. '#value' => t('Add'),
  429. '#name' => 'add',
  430. '#ajax' => array(
  431. 'callback' => "tripal_core_props_property_ajax_update",
  432. 'wrapper' => 'tripal-properties-edit-properties-table',
  433. 'effect' => 'fade',
  434. 'method' => 'replace',
  435. 'prevent' => 'click'
  436. ),
  437. // When this button is clicked, the form will be validated and submitted.
  438. // Therefore, we set custom submit and validate functions to override the
  439. // default form submit. In the validate function we set the form_state
  440. // to rebuild the form so the submit function never actually gets called,
  441. // but we need it or Drupal will run the default validate anyway.
  442. // we also set #limit_validation_errors to empty so fields that
  443. // are required that don't have values won't generate warnings.
  444. '#submit' => array('tripal_core_props_form_props_button_submit'),
  445. '#validate' => array('tripal_core_props_form_props_button_validate'),
  446. '#limit_validation_errors' => array(array('new_id')),
  447. );
  448. }
  449. /**
  450. * This function is used to rebuild the form if an ajax call is made vai a button.
  451. * The button causes the form to be submitted. We don't want this so we override
  452. * the validate and submit routines on the form button. Therefore, this function
  453. * only needs to tell Drupal to rebuild the form
  454. */
  455. function tripal_core_props_form_props_button_validate($form, &$form_state){
  456. if (array_key_exists('triggering_element', $form_state) and
  457. $form_state['triggering_element']['#name'] == 'add' and
  458. $form_state['input']['new_id'] == 0 ){
  459. form_set_error('new_id', "Please specify a property type");
  460. return;
  461. }
  462. $form_state['rebuild'] = TRUE;
  463. }
  464. /**
  465. * This function is just a dummy to override the default form submit on ajax calls for buttons
  466. */
  467. function tripal_core_props_form_props_button_submit($form, &$form_state){
  468. // do nothing
  469. }
  470. /**
  471. * This adds
  472. */
  473. function tripal_core_properties_form_add_new_props(&$form, &$form_state, &$ranks, &$d_removed) {
  474. // set some default values
  475. $j = 0;
  476. $num_properties = 0;
  477. $values = array();
  478. if (array_key_exists('values', $form_state)) {
  479. $values = $form_state['values'];
  480. }
  481. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  482. $values = $form_state['input'];
  483. }
  484. // first, add in all of the new properties that were added previously via this form
  485. foreach ($values as $element_name => $value) {
  486. if (preg_match('/new_value-(\d+)-(\d+)/', $element_name, $matches)) {
  487. $new_id = $matches[1];
  488. $rank = $matches[2];
  489. // skip any properties that the user requested to delete through a previous
  490. // ajax callback or through the current ajax callback
  491. if (array_key_exists("$new_id-$rank", $d_removed)) {
  492. continue;
  493. }
  494. if (array_key_exists('triggering_element', $form_state) and
  495. $form_state['triggering_element']['#name'] == 'remove-' . $new_id . '-' . $rank) {
  496. $d_removed["$new_id-$rank"] = 1;
  497. continue;
  498. }
  499. // get this new_id information
  500. $args = array('cvterm_id' => $new_id);
  501. $cvterm = tripal_core_chado_select('cvterm', array('name', 'definition'), $args);
  502. // add it to the $ranks array
  503. $ranks[$new_id][$rank]['name'] = $cvterm[0]->name;
  504. $ranks[$new_id][$rank]['id'] = $new_id;
  505. $ranks[$new_id][$rank]['value'] = $value;
  506. $ranks[$new_id][$rank]['definition'] = $cvterm[0]->definition;
  507. $num_properties++;
  508. // determine how many rows we need in the textarea
  509. $rows = 1;
  510. // add the new fields
  511. $form['properties']['table']['new'][$new_id][$rank]["new_id-$new_id-$rank"] = array(
  512. '#markup' => $cvterm[0]->name
  513. );
  514. $form['properties']['table']['new'][$new_id][$rank]["new_value-$new_id-$rank"] = array(
  515. '#type' => 'textarea',
  516. '#default_value' => $value,
  517. '#cols' => 50,
  518. '#rows' => $rows,
  519. '#description' => $cvterm[0]->definition,
  520. );
  521. $form['properties']['table']['new'][$new_id][$rank]["remove-$new_id-$rank"] = array(
  522. '#type' => 'button',
  523. '#value' => t('Remove'),
  524. '#name' => "remove-$new_id-$rank",
  525. '#ajax' => array(
  526. 'callback' => "tripal_core_props_property_ajax_update",
  527. 'wrapper' => 'tripal-properties-edit-properties-table',
  528. 'effect' => 'fade',
  529. 'event' => 'mousedown',
  530. 'method' => 'replace',
  531. 'prevent' => 'click'
  532. ),
  533. // When this button is clicked, the form will be validated and submitted.
  534. // Therefore, we set custom submit and validate functions to override the
  535. // default form submit. In the validate function we set the form_state
  536. // to rebuild the form so the submit function never actually gets called,
  537. // but we need it or Drupal will run the default validate anyway.
  538. // we also set #limit_validation_errors to empty so fields that
  539. // are required that don't have values won't generate warnings.
  540. '#submit' => array('tripal_core_props_form_props_button_submit'),
  541. '#validate' => array('tripal_core_props_form_props_button_validate'),
  542. '#limit_validation_errors' => array(),
  543. );
  544. }
  545. }
  546. // second add in any new properties added during this callback
  547. if (array_key_exists('triggering_element', $form_state) and
  548. $form_state['triggering_element']['#name'] == 'add' and
  549. $form_state['input']['new_id'] != 0) {
  550. $new_id = $form_state['input']['new_id'];
  551. $new_value = $form_state['input']['new_value'];
  552. // get the rank by counting the number of entries
  553. $rank = count($ranks[$new_id]);
  554. // get this new_id information
  555. $cvterm = tripal_core_chado_select('cvterm', array('name', 'definition'), array('cvterm_id' => $new_id));
  556. // add it to the $ranks array
  557. $ranks[$new_id][$rank]['name'] = $cvterm[0]->name;
  558. $ranks[$new_id][$rank]['id'] = $new_id;
  559. $ranks[$new_id][$rank]['value'] = $value;
  560. $ranks[$new_id][$rank]['definition'] = $cvterm[0]->definition;
  561. $num_properties++;
  562. // determine how many rows we need in the textarea
  563. $rows = 1;
  564. // add the new fields
  565. $form['properties']['table']['new'][$new_id][$rank]["new_id-$new_id-$rank"] = array(
  566. '#markup' => $cvterm[0]->name
  567. );
  568. $form['properties']['table']['new'][$new_id][$rank]["new_value-$new_id-$rank"] = array(
  569. '#type' => 'textarea',
  570. '#default_value' => $new_value,
  571. '#cols' => 50,
  572. '#rows' => $rows,
  573. '#description' => $cvterm[0]->definition,
  574. );
  575. $form['properties']['table']['new'][$new_id][$rank]["remove-$new_id-$rank"] = array(
  576. '#type' => 'button',
  577. '#value' => t('Remove'),
  578. '#name' => "remove-$new_id-$rank",
  579. '#ajax' => array(
  580. 'callback' => "tripal_core_props_property_ajax_update",
  581. 'wrapper' => 'tripal-properties-edit-properties-table',
  582. 'effect' => 'fade',
  583. 'event' => 'mousedown',
  584. 'method' => 'replace',
  585. 'prevent' => 'click'
  586. ),
  587. // When this button is clicked, the form will be validated and submitted.
  588. // Therefore, we set custom submit and validate functions to override the
  589. // default form submit. In the validate function we set the form_state
  590. // to rebuild the form so the submit function never actually gets called,
  591. // but we need it or Drupal will run the default validate anyway.
  592. // we also set #limit_validation_errors to empty so fields that
  593. // are required that don't have values won't generate warnings.
  594. '#submit' => array('tripal_core_props_form_props_button_submit'),
  595. '#validate' => array('tripal_core_props_form_props_button_validate'),
  596. '#limit_validation_errors' => array(),
  597. );
  598. }
  599. return $num_properties;
  600. }
  601. /*
  602. *
  603. */
  604. function tripal_core_properties_form_add_prop_table_props($prop_table, $id_field, $cv_name,
  605. &$form, $form_state, $id, &$ranks, &$d_removed, $exclude = array()) {
  606. // get the existing properties
  607. $num_properties = 0;
  608. if (!$id) {
  609. return;
  610. }
  611. $sql = "
  612. SELECT CVT.cvterm_id, CVT.name, CVT.definition, PP.value, PP.rank
  613. FROM {" . $prop_table . "} PP
  614. INNER JOIN {cvterm} CVT ON CVT.cvterm_id = PP.type_id
  615. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  616. WHERE
  617. PP.$id_field = :id AND
  618. CV.name = '$cv_name'
  619. ORDER BY CVT.name, PP.rank
  620. ";
  621. $props = chado_query($sql, array(':id' => $id));
  622. while ($prop = $props->fetchObject()) {
  623. $type_id = $prop->cvterm_id;
  624. $rank = 0;
  625. if(array_key_exists($type_id, $ranks)) {
  626. $rank = count($ranks[$type_id]);
  627. }
  628. // skip any properties that the user requested to delete through a previous
  629. // AHAH callback or through the current AHAH callback
  630. if (array_key_exists("$type_id-$rank", $d_removed)) {
  631. continue;
  632. }
  633. // skip any properties that should be excluded
  634. if (count(array_intersect(array($prop->name), $exclude)) == 1) {
  635. continue;
  636. }
  637. if (array_key_exists('triggering_element', $form_state) and
  638. $form_state['triggering_element']['#name'] == 'remove-' . $type_id . '-' . $rank) {
  639. $d_removed["$type_id-$rank"] = 1;
  640. continue;
  641. }
  642. $ranks[$type_id][$rank]['name'] = $prop->name;
  643. $ranks[$type_id][$rank]['id'] = $type_id;
  644. $ranks[$type_id][$rank]['value'] = $prop->value;
  645. $ranks[$type_id][$rank]['definition'] = $prop->definition;
  646. $num_properties++;
  647. $rows = 1;
  648. $form['properties']['table'][$type_id][$rank]["prop_id-$type_id-$rank"] = array(
  649. '#markup' => $prop->name,
  650. );
  651. $form['properties']['table'][$type_id][$rank]["prop_value-$type_id-$rank"] = array(
  652. '#type' => 'textarea',
  653. '#default_value' => $prop->value,
  654. '#cols' => 50,
  655. '#rows' => $rows,
  656. '#description' => $prop->definition,
  657. );
  658. $form['properties']['table'][$type_id][$rank]["remove-$type_id-$rank"] = array(
  659. '#type' => 'button',
  660. '#value' => t('Remove'),
  661. '#name' => "remove-$type_id-$rank",
  662. '#ajax' => array(
  663. 'callback' => "tripal_core_props_property_ajax_update",
  664. 'wrapper' => 'tripal-properties-edit-properties-table',
  665. 'effect' => 'fade',
  666. 'event' => 'mousedown',
  667. 'method' => 'replace',
  668. 'prevent' => 'click'
  669. ),
  670. // When this button is clicked, the form will be validated and submitted.
  671. // Therefore, we set custom submit and validate functions to override the
  672. // default form submit. In the validate function we set the form_state
  673. // to rebuild the form so the submit function never actually gets called,
  674. // but we need it or Drupal will run the default validate anyway.
  675. // we also set #limit_validation_errors to empty so fields that
  676. // are required that don't have values won't generate warnings.
  677. '#submit' => array('tripal_core_props_form_props_button_submit'),
  678. '#validate' => array('tripal_core_props_form_props_button_validate'),
  679. '#limit_validation_errors' => array(),
  680. );
  681. }
  682. return $num_properties;
  683. }
  684. /**
  685. * Form AJAX callback for adding a blank property row
  686. *
  687. * We only want to return the properties as that's all we'll replace with this callback
  688. */
  689. function tripal_core_props_property_ajax_update($form, $form_state) {
  690. $properties_html = tripal_core_props_theme_node_form_properties($form['properties']['table']);
  691. $form['properties']['table'] = array(
  692. '#markup' => $properties_html,
  693. '#prefix' => '<div id="tripal-properties-edit-properties-table">',
  694. '#suffix' => '</div>',
  695. );
  696. return $form['properties']['table'];
  697. }
  698. /**
  699. * Form AJAX callback for updating a property description. This
  700. * function only gets called when the property drop down is changed
  701. * on the bottom (empty) row of properties
  702. */
  703. function tripal_core_props_property_ajax_get_description($form, $form_state) {
  704. return $form['properties']['table']['new']["new_value"];
  705. }
  706. /**
  707. * We need to theme the form so that the properties fields look good
  708. */
  709. function theme_tripal_core_properties_form($variables) {
  710. $form = $variables['form'];
  711. $properties_table = tripal_core_props_theme_node_form_properties($form);
  712. $markup = $properties_table;
  713. $form['properties']['table'] = array(
  714. '#markup' => $markup,
  715. '#prefix' => '<div id="tripal-properties-edit-properties-table">',
  716. '#suffix' => '</div>',
  717. );
  718. $form['buttons']['#weight'] = 50;
  719. return drupal_render($form['properties']['table']);
  720. }
  721. /**
  722. *
  723. */
  724. function tripal_core_props_theme_node_form_properties($form) {
  725. $rows = array();
  726. // first add in the properties derived from the prop table
  727. // the array tree for these properties looks like this:
  728. // $form['properties']['table'][$type_id][$rank]["prop_id-$type_id-$rank"]
  729. foreach ($form as $type_id => $elements) {
  730. // there are other fields in the properties array so we only
  731. // want the numeric ones those are our type_id
  732. if (is_numeric($type_id)) {
  733. foreach ($elements as $rank => $element) {
  734. if (is_numeric($rank)) {
  735. $rows[] = array(
  736. drupal_render($element["prop_id-$type_id-$rank"]),
  737. drupal_render($element["prop_value-$type_id-$rank"]),
  738. drupal_render($element["remove-$type_id-$rank"]),
  739. );
  740. }
  741. }
  742. }
  743. }
  744. // second, add in any new properties added by the user through AHAH callbacks
  745. // the array tree for these properties looks like this:
  746. // $form['properties']['table']['new'][$type_id][$rank]["new_id-$new_id-$rank"]
  747. foreach ($form['new'] as $type_id => $elements) {
  748. if (is_numeric($type_id)) {
  749. foreach ($elements as $rank => $element) {
  750. if (is_numeric($rank)) {
  751. $rows[] = array(
  752. drupal_render($element["new_id-$type_id-$rank"]),
  753. drupal_render($element["new_value-$type_id-$rank"]),
  754. drupal_render($element["remove-$type_id-$rank"]),
  755. );
  756. }
  757. }
  758. }
  759. }
  760. // finally add in a set of blank field for adding a new property
  761. $rows[] = array(
  762. drupal_render($form['new']['new_id']),
  763. array(
  764. 'data' => drupal_render($form['new']['new_value']),
  765. 'width' => '60%',
  766. ),
  767. drupal_render($form['new']['add']),
  768. );
  769. $headers = array('Property Type', 'Value', 'Actions');
  770. $table = array(
  771. 'header' => $headers,
  772. 'rows' => $rows,
  773. 'attributes' => array(),
  774. 'sticky' => TRUE,
  775. 'caption' => '',
  776. 'colgroups' => array(),
  777. 'empty' => '',
  778. );
  779. return theme_table($table);
  780. }
  781. /**
  782. * This function is used in a hook_insert, hook_update for a node form
  783. * when the properties form has been added to the form. It retrieves all of the properties
  784. * and returns them in an array of the format:
  785. *
  786. * $properties[<property name>][<rank>] = <value
  787. *
  788. * This array can then be used for inserting or updating properties using the API call
  789. * tripal_hook_insert_property()
  790. *
  791. * @param $node
  792. * @param $cvname
  793. * The name of the controlled vocabulary that the properties belong to
  794. *
  795. * @return
  796. * A properties array
  797. *
  798. * @ingroup tripal_properties_api
  799. */
  800. function tripal_core_properties_form_retreive($node, $cv_name) {
  801. // now add the properties
  802. $properties = array(); // stores all of the properties we need to add
  803. // get the list of properties for easy lookup (without doing lots of database queries
  804. $properties_list = array();
  805. $sql = "
  806. SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
  807. FROM {cvterm} CVT
  808. INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
  809. WHERE
  810. CV.name = '$cv_name' AND
  811. NOT CVT.is_obsolete = 1
  812. ORDER BY CVT.name ASC
  813. ";
  814. $prop_types = chado_query($sql);
  815. while ($prop = $prop_types->fetchObject()) {
  816. $properties_list[$prop->cvterm_id] = $prop->name;
  817. }
  818. // get the properties that should be added. Properties are in one of two forms:
  819. // 1) prop_value-[type id]-[index]
  820. // 2) new_value-[type id]-[index]
  821. // 3) new_id, new_value
  822. foreach ($node as $name => $value) {
  823. if (preg_match('/^new_value-(\d+)-(\d+)/', $name, $matches)) {
  824. $type_id = $matches[1];
  825. $index = $matches[2];
  826. $name = $properties_list[$type_id];
  827. $properties[$name][$index] = trim($value);
  828. }
  829. if (preg_match('/^prop_value-(\d+)-(\d+)/', $name, $matches)) {
  830. $type_id = $matches[1];
  831. $index = $matches[2];
  832. $name = $properties_list[$type_id];
  833. $properties[$name][$index] = trim($value);
  834. }
  835. }
  836. if (property_exists($node, 'new_id') and $node->new_id and property_exists($node, 'new_value') and $node->new_value) {
  837. $type_id = $node->new_id;
  838. $name = $properties_list[$type_id];
  839. $index = 0;
  840. if (array_key_exists($name, $properties)) {
  841. $index = count($properties[$name]);
  842. }
  843. $properties[$name][$index] = trim($node->new_value);
  844. }
  845. return $properties;
  846. }