tripal_core_properties.api.inc 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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' in vocabulary '%cvname'.",
  113. array('%prop_name' => $property, '%cvname' => $cv_name), 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 optional 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 $include
  335. * An optional array of terms to pre-populate in the form. This argument can be used to
  336. * add a default set of pre-populated properties regardless if they exist in the database
  337. * or not. The array should be of the following form:
  338. * array(
  339. * array('cvterm' => $obj1, 'value' => $val1),
  340. * array('cvterm' => $obj2, 'value' => $val2),
  341. * ... etc
  342. * );
  343. * The 'cvterm' key should have as a value an object with these properties: 'name', 'cvterm_id', 'definition'.
  344. * @param $instructions
  345. * An optional additional set of instructions for the form properties.
  346. * @param $fset_title
  347. * A title for the property field set. The default is 'Additional Details'.
  348. * @ingroup tripal_properties_api
  349. */
  350. function tripal_core_properties_form(&$form, &$form_state, $prop_table, $id_field, $cv_name,
  351. $available_props, $id = NULL, $exclude = array(), $include = array(), $instructions = '',
  352. $fset_title = 'Additional Details') {
  353. $d_removed = array(); // lists removed properties
  354. $num_new = 0; // the number of new rows
  355. // if we are re constructing the form from a failed validation or ajax callback
  356. // then use the $form_state['values'] values
  357. if (array_key_exists('values', $form_state)) {
  358. $d_removed = $form_state['values']['removed'];
  359. $num_new = $form_state['values']['num_new'] ? $form_state['values']['num_new'] : 0;
  360. }
  361. // if we are re building the form from after submission (from ajax call) then
  362. // the values are in the $form_state['input'] array
  363. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  364. $d_removed = $form_state['input']['removed'];
  365. $num_new = $form_state['input']['num_new'] ? $form_state['input']['num_new'] : 0;
  366. }
  367. $form['properties'] = array(
  368. '#type' => 'fieldset',
  369. '#title' => t($fset_title),
  370. '#description' => t('You may add additional properties by
  371. selecting a property type from the dropdown and adding text. You may add
  372. as many properties as desired by clicking the add button on the right. To
  373. remove a property, click the remove button. ' . $instructions),
  374. );
  375. $form['properties']['table'] = array(
  376. '#type' => 'markup',
  377. '#value' => '',
  378. '#prefix' => '<div id="tripal-generic-edit-properties-table">',
  379. '#suffix' => '</div>',
  380. );
  381. // this array keeps track of all properties we have and allows the functions
  382. // below to select the next rank if a property is dupliated
  383. $ranks = array();
  384. // add in the properties from the Chado prop table (only pertains to existing analyses)
  385. if ($id) {
  386. tripal_core_properties_form_add_prop_table_props($prop_table, $id_field, $cv_name,
  387. $form, $form_state, $id, $ranks, $d_removed, $exclude, $include);
  388. }
  389. // add in any new properties that have been added by the user through an AHAH callback
  390. tripal_core_properties_form_add_new_props($form, $form_state, $ranks, $d_removed);
  391. // add an empty row of field to allow for addition of a new property
  392. tripal_core_properties_form_add_new_empty_props($form, $form_state, $available_props);
  393. $form['properties']['table']['#theme'] = 'tripal_core_properties_form';
  394. }
  395. /**
  396. * This function is responsible for adding a blank row to the properties table for
  397. * adding a new property.
  398. */
  399. function tripal_core_properties_form_add_new_empty_props(&$form, &$form_state, $properties_select) {
  400. // get the field defaults either from $form_state['values'] or $form_state['input']
  401. $description = '';
  402. $text = '';
  403. $id = 0;
  404. if (array_key_exists('values', $form_state)) {
  405. $id = $form_state['values']['new_id'];
  406. $text = $form_state['values']['new_value'];
  407. }
  408. // if we have a property ID then get it's definition to display to the user
  409. if($id) {
  410. $values = array('cvterm_id' => $id);
  411. $cvterm = tripal_core_chado_select('cvterm', array('definition'), $values);
  412. if ($cvterm[0]->definition) {
  413. $description = $cvterm[0]->definition;
  414. }
  415. }
  416. $rows = 1;
  417. // add one more blank set of property fields
  418. $form['properties']['table']['new']["new_id"] = array(
  419. '#type' => 'select',
  420. '#options' => $properties_select,
  421. '#default_value' => $id,
  422. '#ajax' => array(
  423. 'callback' => "tripal_core_props_property_ajax_get_description",
  424. 'wrapper' => 'tripal-properties-new_value',
  425. 'effect' => 'fade',
  426. 'method' => 'replace',
  427. ),
  428. );
  429. $form['properties']['table']['new']["new_value"] = array(
  430. '#type' => 'textarea',
  431. '#default_value' => $text,
  432. '#cols' => 50,
  433. '#rows' => $rows,
  434. '#prefix' => '<div id="tripal-properties-new_value">',
  435. '#description' => $description,
  436. '#suffix' => '</div>',
  437. );
  438. $form['properties']['table']['new']["add"] = array(
  439. '#type' => 'button',
  440. '#value' => t('Add'),
  441. '#name' => 'add',
  442. '#ajax' => array(
  443. 'callback' => "tripal_core_props_property_ajax_update",
  444. 'wrapper' => 'tripal-properties-edit-properties-table',
  445. 'effect' => 'fade',
  446. 'method' => 'replace',
  447. 'prevent' => 'click'
  448. ),
  449. // When this button is clicked, the form will be validated and submitted.
  450. // Therefore, we set custom submit and validate functions to override the
  451. // default form submit. In the validate function we set the form_state
  452. // to rebuild the form so the submit function never actually gets called,
  453. // but we need it or Drupal will run the default validate anyway.
  454. // we also set #limit_validation_errors to empty so fields that
  455. // are required that don't have values won't generate warnings.
  456. '#submit' => array('tripal_core_props_form_props_button_submit'),
  457. '#validate' => array('tripal_core_props_form_props_button_validate'),
  458. '#limit_validation_errors' => array(array('new_id')),
  459. );
  460. }
  461. /**
  462. * This function is used to rebuild the form if an ajax call is made vai a button.
  463. * The button causes the form to be submitted. We don't want this so we override
  464. * the validate and submit routines on the form button. Therefore, this function
  465. * only needs to tell Drupal to rebuild the form
  466. */
  467. function tripal_core_props_form_props_button_validate($form, &$form_state){
  468. if (array_key_exists('triggering_element', $form_state) and
  469. $form_state['triggering_element']['#name'] == 'add' and
  470. $form_state['input']['new_id'] == 0 ){
  471. form_set_error('new_id', "Please specify a property type");
  472. return;
  473. }
  474. $form_state['rebuild'] = TRUE;
  475. }
  476. /**
  477. * This function is just a dummy to override the default form submit on ajax calls for buttons
  478. */
  479. function tripal_core_props_form_props_button_submit($form, &$form_state){
  480. // do nothing
  481. }
  482. /**
  483. * This adds
  484. */
  485. function tripal_core_properties_form_add_new_props(&$form, &$form_state, &$ranks, &$d_removed) {
  486. // set some default values
  487. $j = 0;
  488. $num_properties = 0;
  489. $values = array();
  490. if (array_key_exists('values', $form_state)) {
  491. $values = $form_state['values'];
  492. }
  493. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  494. $values = $form_state['input'];
  495. }
  496. // first, add in all of the new properties that were added previously via this form
  497. foreach ($values as $element_name => $value) {
  498. if (preg_match('/new_value-(\d+)-(\d+)/', $element_name, $matches)) {
  499. $new_id = $matches[1];
  500. $rank = $matches[2];
  501. // skip any properties that the user requested to delete through a previous
  502. // ajax callback or through the current ajax callback
  503. if (array_key_exists("$new_id-$rank", $d_removed)) {
  504. continue;
  505. }
  506. if (array_key_exists('triggering_element', $form_state) and
  507. $form_state['triggering_element']['#name'] == 'remove-' . $new_id . '-' . $rank) {
  508. $d_removed["$new_id-$rank"] = 1;
  509. continue;
  510. }
  511. // get this new_id information
  512. $args = array('cvterm_id' => $new_id);
  513. $cvterm = tripal_core_chado_select('cvterm', array('name', 'definition'), $args);
  514. // add it to the $ranks array
  515. $ranks[$new_id][$rank]['name'] = $cvterm[0]->name;
  516. $ranks[$new_id][$rank]['id'] = $new_id;
  517. $ranks[$new_id][$rank]['value'] = $value;
  518. $ranks[$new_id][$rank]['definition'] = $cvterm[0]->definition;
  519. $num_properties++;
  520. // determine how many rows we need in the textarea
  521. $rows = 1;
  522. // add the new fields
  523. $form['properties']['table']['new'][$new_id][$rank]["new_id-$new_id-$rank"] = array(
  524. '#markup' => $cvterm[0]->name
  525. );
  526. $form['properties']['table']['new'][$new_id][$rank]["new_value-$new_id-$rank"] = array(
  527. '#type' => 'textarea',
  528. '#default_value' => $value,
  529. '#cols' => 50,
  530. '#rows' => $rows,
  531. '#description' => $cvterm[0]->definition,
  532. );
  533. $form['properties']['table']['new'][$new_id][$rank]["remove-$new_id-$rank"] = array(
  534. '#type' => 'button',
  535. '#value' => t('Remove'),
  536. '#name' => "remove-$new_id-$rank",
  537. '#ajax' => array(
  538. 'callback' => "tripal_core_props_property_ajax_update",
  539. 'wrapper' => 'tripal-properties-edit-properties-table',
  540. 'effect' => 'fade',
  541. 'event' => 'mousedown',
  542. 'method' => 'replace',
  543. 'prevent' => 'click'
  544. ),
  545. // When this button is clicked, the form will be validated and submitted.
  546. // Therefore, we set custom submit and validate functions to override the
  547. // default form submit. In the validate function we set the form_state
  548. // to rebuild the form so the submit function never actually gets called,
  549. // but we need it or Drupal will run the default validate anyway.
  550. // we also set #limit_validation_errors to empty so fields that
  551. // are required that don't have values won't generate warnings.
  552. '#submit' => array('tripal_core_props_form_props_button_submit'),
  553. '#validate' => array('tripal_core_props_form_props_button_validate'),
  554. '#limit_validation_errors' => array(),
  555. );
  556. }
  557. }
  558. // second add in any new properties added during this callback
  559. if (array_key_exists('triggering_element', $form_state) and
  560. $form_state['triggering_element']['#name'] == 'add' and
  561. $form_state['input']['new_id'] != 0) {
  562. $new_id = $form_state['input']['new_id'];
  563. $new_value = $form_state['input']['new_value'];
  564. // get the rank by counting the number of entries
  565. $rank = count($ranks[$new_id]);
  566. // get this new_id information
  567. $cvterm = tripal_core_chado_select('cvterm', array('name', 'definition'), array('cvterm_id' => $new_id));
  568. // add it to the $ranks array
  569. $ranks[$new_id][$rank]['name'] = $cvterm[0]->name;
  570. $ranks[$new_id][$rank]['id'] = $new_id;
  571. $ranks[$new_id][$rank]['value'] = $value;
  572. $ranks[$new_id][$rank]['definition'] = $cvterm[0]->definition;
  573. $num_properties++;
  574. // determine how many rows we need in the textarea
  575. $rows = 1;
  576. // add the new fields
  577. $form['properties']['table']['new'][$new_id][$rank]["new_id-$new_id-$rank"] = array(
  578. '#markup' => $cvterm[0]->name
  579. );
  580. $form['properties']['table']['new'][$new_id][$rank]["new_value-$new_id-$rank"] = array(
  581. '#type' => 'textarea',
  582. '#default_value' => $new_value,
  583. '#cols' => 50,
  584. '#rows' => $rows,
  585. '#description' => $cvterm[0]->definition,
  586. );
  587. $form['properties']['table']['new'][$new_id][$rank]["remove-$new_id-$rank"] = array(
  588. '#type' => 'button',
  589. '#value' => t('Remove'),
  590. '#name' => "remove-$new_id-$rank",
  591. '#ajax' => array(
  592. 'callback' => "tripal_core_props_property_ajax_update",
  593. 'wrapper' => 'tripal-properties-edit-properties-table',
  594. 'effect' => 'fade',
  595. 'event' => 'mousedown',
  596. 'method' => 'replace',
  597. 'prevent' => 'click'
  598. ),
  599. // When this button is clicked, the form will be validated and submitted.
  600. // Therefore, we set custom submit and validate functions to override the
  601. // default form submit. In the validate function we set the form_state
  602. // to rebuild the form so the submit function never actually gets called,
  603. // but we need it or Drupal will run the default validate anyway.
  604. // we also set #limit_validation_errors to empty so fields that
  605. // are required that don't have values won't generate warnings.
  606. '#submit' => array('tripal_core_props_form_props_button_submit'),
  607. '#validate' => array('tripal_core_props_form_props_button_validate'),
  608. '#limit_validation_errors' => array(),
  609. );
  610. }
  611. return $num_properties;
  612. }
  613. /**
  614. * This function queries the proper xxxprop table to look for existing values for the given
  615. * $id. It then adds these properties to the form for editing. It also will incorporate
  616. * extra properties that were specified manually by the caller.
  617. *
  618. */
  619. function tripal_core_properties_form_add_prop_table_props($prop_table, $id_field, $cv_name,
  620. &$form, $form_state, $id, &$ranks, &$d_removed, $exclude = array(), $include = array()) {
  621. // get the existing properties
  622. $num_properties = 0;
  623. if (!$id) {
  624. return;
  625. }
  626. // create an array of properties so we can merge those in the database with those provided by the caller
  627. $all_props = array();
  628. foreach ($include as $prop) {
  629. $all_props[] = $prop;
  630. }
  631. // now merge in properties saved in the database
  632. $sql = "
  633. SELECT CVT.cvterm_id, CVT.name, CVT.definition, PP.value, PP.rank
  634. FROM {" . $prop_table . "} PP
  635. INNER JOIN {cvterm} CVT ON CVT.cvterm_id = PP.type_id
  636. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  637. WHERE
  638. PP.$id_field = :id AND
  639. CV.name = '$cv_name'
  640. ORDER BY CVT.name, PP.rank
  641. ";
  642. $props = chado_query($sql, array(':id' => $id));
  643. while ($prop = $props->fetchObject()) {
  644. $all_props[] = array('cvterm' => $prop, 'value' => $prop->value);
  645. }
  646. // iterate through the properties
  647. foreach ($all_props as $prop) {
  648. $type_id = $prop['cvterm']->cvterm_id;
  649. $value = $prop['value'];
  650. $name = $prop['cvterm']->name;
  651. $definition = $prop['cvterm']->definition;
  652. $rank = 0;
  653. if(array_key_exists($type_id, $ranks)) {
  654. $rank = count($ranks[$type_id]);
  655. }
  656. // skip any properties that the user requested to delete through a previous
  657. // AHAH callback or through the current AHAH callback
  658. if (array_key_exists("$type_id-$rank", $d_removed)) {
  659. continue;
  660. }
  661. // skip any properties that should be excluded
  662. if (count(array_intersect(array($name), $exclude)) == 1) {
  663. continue;
  664. }
  665. if (array_key_exists('triggering_element', $form_state) and
  666. $form_state['triggering_element']['#name'] == 'remove-' . $type_id . '-' . $rank) {
  667. $d_removed["$type_id-$rank"] = 1;
  668. continue;
  669. }
  670. $ranks[$type_id][$rank]['name'] = $name;
  671. $ranks[$type_id][$rank]['id'] = $type_id;
  672. $ranks[$type_id][$rank]['value'] = $value;
  673. $num_properties++;
  674. $rows = 1;
  675. $form['properties']['table'][$type_id][$rank]["prop_id-$type_id-$rank"] = array(
  676. '#markup' => $name,
  677. );
  678. $form['properties']['table'][$type_id][$rank]["prop_value-$type_id-$rank"] = array(
  679. '#type' => 'textarea',
  680. '#default_value' => $value,
  681. '#cols' => 50,
  682. '#rows' => $rows,
  683. '#description' => $definition,
  684. );
  685. $form['properties']['table'][$type_id][$rank]["remove-$type_id-$rank"] = array(
  686. '#type' => 'button',
  687. '#value' => t('Remove'),
  688. '#name' => "remove-$type_id-$rank",
  689. '#ajax' => array(
  690. 'callback' => "tripal_core_props_property_ajax_update",
  691. 'wrapper' => 'tripal-properties-edit-properties-table',
  692. 'effect' => 'fade',
  693. 'event' => 'mousedown',
  694. 'method' => 'replace',
  695. 'prevent' => 'click'
  696. ),
  697. // When this button is clicked, the form will be validated and submitted.
  698. // Therefore, we set custom submit and validate functions to override the
  699. // default form submit. In the validate function we set the form_state
  700. // to rebuild the form so the submit function never actually gets called,
  701. // but we need it or Drupal will run the default validate anyway.
  702. // we also set #limit_validation_errors to empty so fields that
  703. // are required that don't have values won't generate warnings.
  704. '#submit' => array('tripal_core_props_form_props_button_submit'),
  705. '#validate' => array('tripal_core_props_form_props_button_validate'),
  706. '#limit_validation_errors' => array(),
  707. );
  708. }
  709. return $num_properties;
  710. }
  711. /**
  712. * Form AJAX callback for adding a blank property row
  713. *
  714. * We only want to return the properties as that's all we'll replace with this callback
  715. */
  716. function tripal_core_props_property_ajax_update($form, $form_state) {
  717. $properties_html = tripal_core_props_theme_node_form_properties($form['properties']['table']);
  718. $form['properties']['table'] = array(
  719. '#markup' => $properties_html,
  720. '#prefix' => '<div id="tripal-properties-edit-properties-table">',
  721. '#suffix' => '</div>',
  722. );
  723. return $form['properties']['table'];
  724. }
  725. /**
  726. * Form AJAX callback for updating a property description. This
  727. * function only gets called when the property drop down is changed
  728. * on the bottom (empty) row of properties
  729. */
  730. function tripal_core_props_property_ajax_get_description($form, $form_state) {
  731. return $form['properties']['table']['new']["new_value"];
  732. }
  733. /**
  734. * We need to theme the form so that the properties fields look good
  735. */
  736. function theme_tripal_core_properties_form($variables) {
  737. $form = $variables['form'];
  738. $properties_table = tripal_core_props_theme_node_form_properties($form);
  739. $markup = $properties_table;
  740. $form['properties']['table'] = array(
  741. '#markup' => $markup,
  742. '#prefix' => '<div id="tripal-properties-edit-properties-table">',
  743. '#suffix' => '</div>',
  744. );
  745. $form['buttons']['#weight'] = 50;
  746. return drupal_render($form['properties']['table']);
  747. }
  748. /**
  749. *
  750. */
  751. function tripal_core_props_theme_node_form_properties($form) {
  752. $rows = array();
  753. // first add in the properties derived from the prop table
  754. // the array tree for these properties looks like this:
  755. // $form['properties']['table'][$type_id][$rank]["prop_id-$type_id-$rank"]
  756. foreach ($form as $type_id => $elements) {
  757. // there are other fields in the properties array so we only
  758. // want the numeric ones those are our type_id
  759. if (is_numeric($type_id)) {
  760. foreach ($elements as $rank => $element) {
  761. if (is_numeric($rank)) {
  762. $rows[] = array(
  763. drupal_render($element["prop_id-$type_id-$rank"]),
  764. drupal_render($element["prop_value-$type_id-$rank"]),
  765. drupal_render($element["remove-$type_id-$rank"]),
  766. );
  767. }
  768. }
  769. }
  770. }
  771. // second, add in any new properties added by the user through AHAH callbacks
  772. // the array tree for these properties looks like this:
  773. // $form['properties']['table']['new'][$type_id][$rank]["new_id-$new_id-$rank"]
  774. foreach ($form['new'] as $type_id => $elements) {
  775. if (is_numeric($type_id)) {
  776. foreach ($elements as $rank => $element) {
  777. if (is_numeric($rank)) {
  778. $rows[] = array(
  779. drupal_render($element["new_id-$type_id-$rank"]),
  780. drupal_render($element["new_value-$type_id-$rank"]),
  781. drupal_render($element["remove-$type_id-$rank"]),
  782. );
  783. }
  784. }
  785. }
  786. }
  787. // finally add in a set of blank field for adding a new property
  788. $rows[] = array(
  789. drupal_render($form['new']['new_id']),
  790. array(
  791. 'data' => drupal_render($form['new']['new_value']),
  792. 'width' => '60%',
  793. ),
  794. drupal_render($form['new']['add']),
  795. );
  796. $headers = array('Property Type', 'Value', 'Actions');
  797. $table = array(
  798. 'header' => $headers,
  799. 'rows' => $rows,
  800. 'attributes' => array(),
  801. 'sticky' => TRUE,
  802. 'caption' => '',
  803. 'colgroups' => array(),
  804. 'empty' => '',
  805. );
  806. return theme_table($table);
  807. }
  808. /**
  809. * This function is used in a hook_insert, hook_update for a node form
  810. * when the properties form has been added to the form. It retrieves all of the properties
  811. * and returns them in an array of the format:
  812. *
  813. * $properties[<property name>][<rank>] = <value
  814. *
  815. * This array can then be used for inserting or updating properties using the API call
  816. * tripal_hook_insert_property()
  817. *
  818. * @param $node
  819. * @param $cvname
  820. * The name of the controlled vocabulary that the properties belong to
  821. *
  822. * @return
  823. * A properties array
  824. *
  825. * @ingroup tripal_properties_api
  826. */
  827. function tripal_core_properties_form_retreive($node, $cv_name) {
  828. // now add the properties
  829. $properties = array(); // stores all of the properties we need to add
  830. // get the list of properties for easy lookup (without doing lots of database queries
  831. $properties_list = array();
  832. $sql = "
  833. SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
  834. FROM {cvterm} CVT
  835. INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
  836. WHERE
  837. CV.name = '$cv_name' AND
  838. NOT CVT.is_obsolete = 1
  839. ORDER BY CVT.name ASC
  840. ";
  841. $prop_types = chado_query($sql);
  842. while ($prop = $prop_types->fetchObject()) {
  843. $properties_list[$prop->cvterm_id] = $prop->name;
  844. }
  845. // get the properties that should be added. Properties are in one of two forms:
  846. // 1) prop_value-[type id]-[index]
  847. // 2) new_value-[type id]-[index]
  848. // 3) new_id, new_value
  849. foreach ($node as $name => $value) {
  850. if (preg_match('/^new_value-(\d+)-(\d+)/', $name, $matches)) {
  851. $type_id = $matches[1];
  852. $index = $matches[2];
  853. $name = $properties_list[$type_id];
  854. $properties[$name][$index] = trim($value);
  855. }
  856. if (preg_match('/^prop_value-(\d+)-(\d+)/', $name, $matches)) {
  857. $type_id = $matches[1];
  858. $index = $matches[2];
  859. $name = $properties_list[$type_id];
  860. $properties[$name][$index] = trim($value);
  861. }
  862. }
  863. if (property_exists($node, 'new_id') and $node->new_id and property_exists($node, 'new_value') and $node->new_value) {
  864. $type_id = $node->new_id;
  865. $name = $properties_list[$type_id];
  866. $index = 0;
  867. if (array_key_exists($name, $properties)) {
  868. $index = count($properties[$name]);
  869. }
  870. $properties[$name][$index] = trim($node->new_value);
  871. }
  872. return $properties;
  873. }