tripal_core_properties.api.inc 41 KB

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