tripal_core_properties.api.inc 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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' => '<span style="white-space: normal">' . $description . '</span>',
  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. $rows = strlen($value) / 80 + 1;
  523. if ($rows > 10) {
  524. $rows = 10;
  525. }
  526. // add the new fields
  527. $form['properties']['table']['new'][$new_id][$rank]["new_id-$new_id-$rank"] = array(
  528. '#markup' => $cvterm[0]->name
  529. );
  530. $form['properties']['table']['new'][$new_id][$rank]["new_value-$new_id-$rank"] = array(
  531. '#type' => 'textarea',
  532. '#default_value' => $value,
  533. '#cols' => 50,
  534. '#rows' => $rows,
  535. '#description' => '<span style="white-space: normal">' . $cvterm[0]->definition . '</span>',
  536. );
  537. $form['properties']['table']['new'][$new_id][$rank]["remove-$new_id-$rank"] = array(
  538. '#type' => 'button',
  539. '#value' => t('Remove'),
  540. '#name' => "remove-$new_id-$rank",
  541. '#ajax' => array(
  542. 'callback' => "tripal_core_props_property_ajax_update",
  543. 'wrapper' => 'tripal-properties-edit-properties-table',
  544. 'effect' => 'fade',
  545. 'event' => 'mousedown',
  546. 'method' => 'replace',
  547. 'prevent' => 'click'
  548. ),
  549. // When this button is clicked, the form will be validated and submitted.
  550. // Therefore, we set custom submit and validate functions to override the
  551. // default form submit. In the validate function we set the form_state
  552. // to rebuild the form so the submit function never actually gets called,
  553. // but we need it or Drupal will run the default validate anyway.
  554. // we also set #limit_validation_errors to empty so fields that
  555. // are required that don't have values won't generate warnings.
  556. '#submit' => array('tripal_core_props_form_props_button_submit'),
  557. '#validate' => array('tripal_core_props_form_props_button_validate'),
  558. '#limit_validation_errors' => array(),
  559. );
  560. }
  561. }
  562. // second add in any new properties added during this callback
  563. if (array_key_exists('triggering_element', $form_state) and
  564. $form_state['triggering_element']['#name'] == 'add' and
  565. $form_state['input']['new_id'] != 0) {
  566. $new_id = $form_state['input']['new_id'];
  567. $new_value = $form_state['input']['new_value'];
  568. // get the rank by counting the number of entries
  569. $rank = count($ranks[$new_id]);
  570. // get this new_id information
  571. $cvterm = tripal_core_chado_select('cvterm', array('name', 'definition'), array('cvterm_id' => $new_id));
  572. // add it to the $ranks array
  573. $ranks[$new_id][$rank]['name'] = $cvterm[0]->name;
  574. $ranks[$new_id][$rank]['id'] = $new_id;
  575. $ranks[$new_id][$rank]['value'] = $value;
  576. $ranks[$new_id][$rank]['definition'] = $cvterm[0]->definition;
  577. $num_properties++;
  578. // determine how many rows we need in the textarea
  579. $rows = 1;
  580. // add the new fields
  581. $form['properties']['table']['new'][$new_id][$rank]["new_id-$new_id-$rank"] = array(
  582. '#markup' => $cvterm[0]->name
  583. );
  584. $form['properties']['table']['new'][$new_id][$rank]["new_value-$new_id-$rank"] = array(
  585. '#type' => 'textarea',
  586. '#default_value' => $new_value,
  587. '#cols' => 50,
  588. '#rows' => $rows,
  589. '#description' => $cvterm[0]->definition,
  590. );
  591. $form['properties']['table']['new'][$new_id][$rank]["remove-$new_id-$rank"] = array(
  592. '#type' => 'button',
  593. '#value' => t('Remove'),
  594. '#name' => "remove-$new_id-$rank",
  595. '#ajax' => array(
  596. 'callback' => "tripal_core_props_property_ajax_update",
  597. 'wrapper' => 'tripal-properties-edit-properties-table',
  598. 'effect' => 'fade',
  599. 'event' => 'mousedown',
  600. 'method' => 'replace',
  601. 'prevent' => 'click'
  602. ),
  603. // When this button is clicked, the form will be validated and submitted.
  604. // Therefore, we set custom submit and validate functions to override the
  605. // default form submit. In the validate function we set the form_state
  606. // to rebuild the form so the submit function never actually gets called,
  607. // but we need it or Drupal will run the default validate anyway.
  608. // we also set #limit_validation_errors to empty so fields that
  609. // are required that don't have values won't generate warnings.
  610. '#submit' => array('tripal_core_props_form_props_button_submit'),
  611. '#validate' => array('tripal_core_props_form_props_button_validate'),
  612. '#limit_validation_errors' => array(),
  613. );
  614. }
  615. return $num_properties;
  616. }
  617. /**
  618. * This function queries the proper xxxprop table to look for existing values for the given
  619. * $id. It then adds these properties to the form for editing. It also will incorporate
  620. * extra properties that were specified manually by the caller.
  621. *
  622. */
  623. function tripal_core_properties_form_add_prop_table_props($prop_table, $id_field, $cv_name,
  624. &$form, $form_state, $id, &$ranks, &$d_removed, $exclude = array(), $include = array()) {
  625. // get the existing properties
  626. $num_properties = 0;
  627. if (!$id) {
  628. return;
  629. }
  630. // create an array of properties so we can merge those in the database with those provided by the caller
  631. $all_props = array();
  632. foreach ($include as $prop) {
  633. $all_props[] = $prop;
  634. }
  635. // now merge in properties saved in the database
  636. $sql = "
  637. SELECT CVT.cvterm_id, CVT.name, CVT.definition, PP.value, PP.rank
  638. FROM {" . $prop_table . "} PP
  639. INNER JOIN {cvterm} CVT ON CVT.cvterm_id = PP.type_id
  640. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  641. WHERE
  642. PP.$id_field = :id AND
  643. CV.name = '$cv_name'
  644. ORDER BY CVT.name, PP.rank
  645. ";
  646. $props = chado_query($sql, array(':id' => $id));
  647. while ($prop = $props->fetchObject()) {
  648. $all_props[] = array('cvterm' => $prop, 'value' => $prop->value);
  649. }
  650. // iterate through the properties
  651. foreach ($all_props as $prop) {
  652. $type_id = $prop['cvterm']->cvterm_id;
  653. $value = $prop['value'];
  654. $name = $prop['cvterm']->name;
  655. $definition = $prop['cvterm']->definition;
  656. $rank = 0;
  657. if(array_key_exists($type_id, $ranks)) {
  658. $rank = count($ranks[$type_id]);
  659. }
  660. // skip any properties that the user requested to delete through a previous
  661. // AHAH callback or through the current AHAH callback
  662. if (array_key_exists("$type_id-$rank", $d_removed)) {
  663. continue;
  664. }
  665. // skip any properties that should be excluded
  666. if (count(array_intersect(array($name), $exclude)) == 1) {
  667. continue;
  668. }
  669. if (array_key_exists('triggering_element', $form_state) and
  670. $form_state['triggering_element']['#name'] == 'remove-' . $type_id . '-' . $rank) {
  671. $d_removed["$type_id-$rank"] = 1;
  672. continue;
  673. }
  674. $ranks[$type_id][$rank]['name'] = $name;
  675. $ranks[$type_id][$rank]['id'] = $type_id;
  676. $ranks[$type_id][$rank]['value'] = $value;
  677. $num_properties++;
  678. $rows = 1;
  679. $rows = strlen($value) / 80 + 1;
  680. if ($rows > 10) {
  681. $rows = 10;
  682. }
  683. $form['properties']['table'][$type_id][$rank]["prop_id-$type_id-$rank"] = array(
  684. '#markup' => $name,
  685. );
  686. $form['properties']['table'][$type_id][$rank]["prop_value-$type_id-$rank"] = array(
  687. '#type' => 'textarea',
  688. '#default_value' => $value,
  689. '#cols' => 50,
  690. '#rows' => $rows,
  691. '#description' => '<span style="white-space: normal">' . $definition . '</span>',
  692. );
  693. $form['properties']['table'][$type_id][$rank]["remove-$type_id-$rank"] = array(
  694. '#type' => 'button',
  695. '#value' => t('Remove'),
  696. '#name' => "remove-$type_id-$rank",
  697. '#ajax' => array(
  698. 'callback' => "tripal_core_props_property_ajax_update",
  699. 'wrapper' => 'tripal-properties-edit-properties-table',
  700. 'effect' => 'fade',
  701. 'event' => 'mousedown',
  702. 'method' => 'replace',
  703. 'prevent' => 'click'
  704. ),
  705. // When this button is clicked, the form will be validated and submitted.
  706. // Therefore, we set custom submit and validate functions to override the
  707. // default form submit. In the validate function we set the form_state
  708. // to rebuild the form so the submit function never actually gets called,
  709. // but we need it or Drupal will run the default validate anyway.
  710. // we also set #limit_validation_errors to empty so fields that
  711. // are required that don't have values won't generate warnings.
  712. '#submit' => array('tripal_core_props_form_props_button_submit'),
  713. '#validate' => array('tripal_core_props_form_props_button_validate'),
  714. '#limit_validation_errors' => array(),
  715. );
  716. }
  717. return $num_properties;
  718. }
  719. /**
  720. * Form AJAX callback for adding a blank property row
  721. *
  722. * We only want to return the properties as that's all we'll replace with this callback
  723. */
  724. function tripal_core_props_property_ajax_update($form, $form_state) {
  725. $properties_html = tripal_core_props_theme_node_form_properties($form['properties']['table']);
  726. $form['properties']['table'] = array(
  727. '#markup' => $properties_html,
  728. '#prefix' => '<div id="tripal-properties-edit-properties-table">',
  729. '#suffix' => '</div>',
  730. );
  731. return $form['properties']['table'];
  732. }
  733. /**
  734. * Form AJAX callback for updating a property description. This
  735. * function only gets called when the property drop down is changed
  736. * on the bottom (empty) row of properties
  737. */
  738. function tripal_core_props_property_ajax_get_description($form, $form_state) {
  739. return $form['properties']['table']['new']["new_value"];
  740. }
  741. /**
  742. * We need to theme the form so that the properties fields look good
  743. */
  744. function theme_tripal_core_properties_form($variables) {
  745. $form = $variables['form'];
  746. $properties_table = tripal_core_props_theme_node_form_properties($form);
  747. $markup = $properties_table;
  748. $form['properties']['table'] = array(
  749. '#markup' => $markup,
  750. '#prefix' => '<div id="tripal-properties-edit-properties-table">',
  751. '#suffix' => '</div>',
  752. );
  753. $form['buttons']['#weight'] = 50;
  754. return drupal_render($form['properties']['table']);
  755. }
  756. /**
  757. *
  758. */
  759. function tripal_core_props_theme_node_form_properties($form) {
  760. $rows = array();
  761. // first add in the properties derived from the prop table
  762. // the array tree for these properties looks like this:
  763. // $form['properties']['table'][$type_id][$rank]["prop_id-$type_id-$rank"]
  764. foreach ($form as $type_id => $elements) {
  765. // there are other fields in the properties array so we only
  766. // want the numeric ones those are our type_id
  767. if (is_numeric($type_id)) {
  768. foreach ($elements as $rank => $element) {
  769. if (is_numeric($rank)) {
  770. $rows[] = array(
  771. drupal_render($element["prop_id-$type_id-$rank"]),
  772. drupal_render($element["prop_value-$type_id-$rank"]),
  773. drupal_render($element["remove-$type_id-$rank"]),
  774. );
  775. }
  776. }
  777. }
  778. }
  779. // second, add in any new properties added by the user through AHAH callbacks
  780. // the array tree for these properties looks like this:
  781. // $form['properties']['table']['new'][$type_id][$rank]["new_id-$new_id-$rank"]
  782. foreach ($form['new'] as $type_id => $elements) {
  783. if (is_numeric($type_id)) {
  784. foreach ($elements as $rank => $element) {
  785. if (is_numeric($rank)) {
  786. $rows[] = array(
  787. drupal_render($element["new_id-$type_id-$rank"]),
  788. drupal_render($element["new_value-$type_id-$rank"]),
  789. drupal_render($element["remove-$type_id-$rank"]),
  790. );
  791. }
  792. }
  793. }
  794. }
  795. // finally add in a set of blank field for adding a new property
  796. $rows[] = array(
  797. drupal_render($form['new']['new_id']),
  798. array(
  799. 'data' => drupal_render($form['new']['new_value']),
  800. 'width' => '60%',
  801. ),
  802. drupal_render($form['new']['add']),
  803. );
  804. $headers = array('Property Type', 'Value', 'Actions');
  805. $table = array(
  806. 'header' => $headers,
  807. 'rows' => $rows,
  808. 'attributes' => array(),
  809. 'sticky' => TRUE,
  810. 'caption' => '',
  811. 'colgroups' => array(),
  812. 'empty' => '',
  813. );
  814. return theme_table($table);
  815. }
  816. /**
  817. * This function is used in a hook_insert, hook_update for a node form
  818. * when the properties form has been added to the form. It retrieves all of the properties
  819. * and returns them in an array of the format:
  820. *
  821. * $properties[<property name>][<rank>] = <value
  822. *
  823. * This array can then be used for inserting or updating properties using the API call
  824. * tripal_hook_insert_property()
  825. *
  826. * @param $node
  827. * @param $cvname
  828. * The name of the controlled vocabulary that the properties belong to
  829. *
  830. * @return
  831. * A properties array
  832. *
  833. * @ingroup tripal_properties_api
  834. */
  835. function tripal_core_properties_form_retreive($node, $cv_name) {
  836. // now add the properties
  837. $properties = array(); // stores all of the properties we need to add
  838. // get the list of properties for easy lookup (without doing lots of database queries
  839. $properties_list = array();
  840. $sql = "
  841. SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
  842. FROM {cvterm} CVT
  843. INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
  844. WHERE
  845. CV.name = '$cv_name' AND
  846. NOT CVT.is_obsolete = 1
  847. ORDER BY CVT.name ASC
  848. ";
  849. $prop_types = chado_query($sql);
  850. while ($prop = $prop_types->fetchObject()) {
  851. $properties_list[$prop->cvterm_id] = $prop->name;
  852. }
  853. // get the properties that should be added. Properties are in one of two forms:
  854. // 1) prop_value-[type id]-[index]
  855. // 2) new_value-[type id]-[index]
  856. // 3) new_id, new_value
  857. foreach ($node as $name => $value) {
  858. if (preg_match('/^new_value-(\d+)-(\d+)/', $name, $matches)) {
  859. $type_id = $matches[1];
  860. $index = $matches[2];
  861. $name = $properties_list[$type_id];
  862. $properties[$name][$index] = trim($value);
  863. }
  864. if (preg_match('/^prop_value-(\d+)-(\d+)/', $name, $matches)) {
  865. $type_id = $matches[1];
  866. $index = $matches[2];
  867. $name = $properties_list[$type_id];
  868. $properties[$name][$index] = trim($value);
  869. }
  870. }
  871. if (property_exists($node, 'new_id') and $node->new_id and property_exists($node, 'new_value') and $node->new_value) {
  872. $type_id = $node->new_id;
  873. $name = $properties_list[$type_id];
  874. $index = 0;
  875. if (array_key_exists($name, $properties)) {
  876. $index = count($properties[$name]);
  877. }
  878. $properties[$name][$index] = trim($node->new_value);
  879. }
  880. return $properties;
  881. }