tripal_core_properties.api.inc 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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. // if we are re constructing the form from a failed validation or ajax callback
  471. // then use the $form_state['values'] values
  472. if (array_key_exists('values', $form_state)) {
  473. $d_removed = $form_state['values']['removed'];
  474. }
  475. // if we are re building the form from after submission (from ajax call) then
  476. // the values are in the $form_state['input'] array
  477. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  478. $d_removed = $form_state['input']['removed'];
  479. }
  480. $form['properties'] = array(
  481. '#type' => 'fieldset',
  482. '#title' => t($fset_title),
  483. '#description' => t('You may add additional properties by
  484. selecting a property type from the dropdown and adding text. You may add
  485. as many properties as desired by clicking the add button on the right. To
  486. remove a property, click the remove button. ' . $instructions),
  487. );
  488. $form['properties']['table'] = array(
  489. '#type' => 'markup',
  490. '#value' => '',
  491. '#prefix' => '<div id="tripal-generic-edit-properties-table">',
  492. '#suffix' => '</div>',
  493. );
  494. // this array keeps track of all properties we have and allows the functions
  495. // below to select the next rank if a property is dupliated
  496. $ranks = array();
  497. // add in the properties from the Chado prop table (only pertains to existing analyses)
  498. if ($id) {
  499. tripal_core_properties_form_add_prop_table_props($prop_table, $id_field, $cv_name,
  500. $form, $form_state, $id, $ranks, $d_removed, $exclude, $include);
  501. }
  502. // add in any new properties that have been added by the user through an AHAH callback
  503. tripal_core_properties_form_add_new_props($form, $form_state, $ranks, $d_removed);
  504. // add an empty row of field to allow for addition of a new property
  505. tripal_core_properties_form_add_new_empty_props($form, $form_state, $available_props);
  506. $form['properties']['table']['#theme'] = 'tripal_core_properties_form';
  507. }
  508. /**
  509. * This function is responsible for adding a blank row to the properties table for
  510. * adding a new property.
  511. */
  512. function tripal_core_properties_form_add_new_empty_props(&$form, &$form_state, $properties_select) {
  513. // get the field defaults either from $form_state['values'] or $form_state['input']
  514. $description = '';
  515. $text = '';
  516. $id = 0;
  517. if (array_key_exists('values', $form_state)) {
  518. $id = $form_state['values']['new_id'];
  519. $text = $form_state['values']['new_value'];
  520. }
  521. // if we have a property ID then get it's definition to display to the user
  522. if($id) {
  523. $values = array('cvterm_id' => $id);
  524. $cvterm = tripal_core_chado_select('cvterm', array('definition'), $values);
  525. if ($cvterm[0]->definition) {
  526. $description = $cvterm[0]->definition;
  527. }
  528. }
  529. $rows = 1;
  530. // add one more blank set of property fields
  531. $form['properties']['table']['new']["new_id"] = array(
  532. '#type' => 'select',
  533. '#options' => $properties_select,
  534. '#default_value' => $id,
  535. '#ajax' => array(
  536. 'callback' => "tripal_core_props_property_ajax_get_description",
  537. 'wrapper' => 'tripal-properties-new_value',
  538. 'effect' => 'fade',
  539. 'method' => 'replace',
  540. ),
  541. );
  542. $form['properties']['table']['new']["new_value"] = array(
  543. '#type' => 'textarea',
  544. '#default_value' => $text,
  545. '#cols' => 50,
  546. '#rows' => $rows,
  547. '#prefix' => '<div id="tripal-properties-new_value">',
  548. '#description' => '<span style="white-space: normal">' . $description . '</span>',
  549. '#suffix' => '</div>',
  550. );
  551. $form['properties']['table']['new']["add"] = array(
  552. '#type' => 'button',
  553. '#value' => t('Add'),
  554. '#name' => 'add',
  555. '#ajax' => array(
  556. 'callback' => "tripal_core_props_property_ajax_update",
  557. 'wrapper' => 'tripal-properties-edit-properties-table',
  558. 'effect' => 'fade',
  559. 'method' => 'replace',
  560. 'prevent' => 'click'
  561. ),
  562. // When this button is clicked, the form will be validated and submitted.
  563. // Therefore, we set custom submit and validate functions to override the
  564. // default form submit. In the validate function we set the form_state
  565. // to rebuild the form so the submit function never actually gets called,
  566. // but we need it or Drupal will run the default validate anyway.
  567. // we also set #limit_validation_errors to empty so fields that
  568. // are required that don't have values won't generate warnings.
  569. '#submit' => array('tripal_core_props_form_props_button_submit'),
  570. '#validate' => array('tripal_core_props_form_props_button_validate'),
  571. '#limit_validation_errors' => array(array('new_id')),
  572. );
  573. }
  574. /**
  575. * This function is used to rebuild the form if an ajax call is made vai a button.
  576. * The button causes the form to be submitted. We don't want this so we override
  577. * the validate and submit routines on the form button. Therefore, this function
  578. * only needs to tell Drupal to rebuild the form
  579. */
  580. function tripal_core_props_form_props_button_validate($form, &$form_state){
  581. if (array_key_exists('triggering_element', $form_state) and
  582. $form_state['triggering_element']['#name'] == 'add' and
  583. $form_state['input']['new_id'] == 0 ){
  584. form_set_error('new_id', "Please specify a property type");
  585. return;
  586. }
  587. $form_state['rebuild'] = TRUE;
  588. }
  589. /**
  590. * This function is just a dummy to override the default form submit on ajax calls for buttons
  591. */
  592. function tripal_core_props_form_props_button_submit($form, &$form_state){
  593. // do nothing
  594. }
  595. /**
  596. * This adds
  597. */
  598. function tripal_core_properties_form_add_new_props(&$form, &$form_state, &$ranks, &$d_removed) {
  599. // set some default values
  600. $j = 0;
  601. $num_properties = 0;
  602. $values = array();
  603. if (array_key_exists('values', $form_state)) {
  604. $values = $form_state['values'];
  605. }
  606. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  607. $values = $form_state['input'];
  608. }
  609. // first, add in all of the new properties that were added previously via this form
  610. foreach ($values as $element_name => $value) {
  611. if (preg_match('/new_value-(\d+)-(\d+)/', $element_name, $matches)) {
  612. $new_id = $matches[1];
  613. $rank = $matches[2];
  614. // skip any properties that the user requested to delete through a previous
  615. // ajax callback or through the current ajax callback
  616. if (array_key_exists("$new_id-$rank", $d_removed)) {
  617. continue;
  618. }
  619. if (array_key_exists('triggering_element', $form_state) and
  620. $form_state['triggering_element']['#name'] == 'remove-' . $new_id . '-' . $rank) {
  621. $d_removed["$new_id-$rank"] = 1;
  622. continue;
  623. }
  624. // get this new_id information
  625. $args = array('cvterm_id' => $new_id);
  626. $cvterm = tripal_core_chado_select('cvterm', array('name', 'definition'), $args);
  627. // add it to the $ranks array
  628. $ranks[$new_id][$rank]['name'] = $cvterm[0]->name;
  629. $ranks[$new_id][$rank]['id'] = $new_id;
  630. $ranks[$new_id][$rank]['value'] = $value;
  631. $ranks[$new_id][$rank]['definition'] = $cvterm[0]->definition;
  632. $num_properties++;
  633. // determine how many rows we need in the textarea
  634. $rows = 1;
  635. $rows = strlen($value) / 80 + 1;
  636. if ($rows > 10) {
  637. $rows = 10;
  638. }
  639. // add the new fields
  640. $form['properties']['table']['new'][$new_id][$rank]["new_id-$new_id-$rank"] = array(
  641. '#markup' => $cvterm[0]->name
  642. );
  643. $form['properties']['table']['new'][$new_id][$rank]["new_value-$new_id-$rank"] = array(
  644. '#type' => 'textarea',
  645. '#default_value' => $value,
  646. '#cols' => 50,
  647. '#rows' => $rows,
  648. '#description' => '<span style="white-space: normal">' . $cvterm[0]->definition . '</span>',
  649. );
  650. $form['properties']['table']['new'][$new_id][$rank]["remove-$new_id-$rank"] = array(
  651. '#type' => 'button',
  652. '#value' => t('Remove'),
  653. '#name' => "remove-$new_id-$rank",
  654. '#ajax' => array(
  655. 'callback' => "tripal_core_props_property_ajax_update",
  656. 'wrapper' => 'tripal-properties-edit-properties-table',
  657. 'effect' => 'fade',
  658. 'event' => 'mousedown',
  659. 'method' => 'replace',
  660. 'prevent' => 'click'
  661. ),
  662. // When this button is clicked, the form will be validated and submitted.
  663. // Therefore, we set custom submit and validate functions to override the
  664. // default form submit. In the validate function we set the form_state
  665. // to rebuild the form so the submit function never actually gets called,
  666. // but we need it or Drupal will run the default validate anyway.
  667. // we also set #limit_validation_errors to empty so fields that
  668. // are required that don't have values won't generate warnings.
  669. '#submit' => array('tripal_core_props_form_props_button_submit'),
  670. '#validate' => array('tripal_core_props_form_props_button_validate'),
  671. '#limit_validation_errors' => array(),
  672. );
  673. }
  674. }
  675. // second add in any new properties added during this callback
  676. if (array_key_exists('triggering_element', $form_state) and
  677. $form_state['triggering_element']['#name'] == 'add' and
  678. $form_state['input']['new_id'] != 0) {
  679. $new_id = $form_state['input']['new_id'];
  680. $new_value = $form_state['input']['new_value'];
  681. // get the rank by counting the number of entries
  682. $rank = count($ranks[$new_id]);
  683. // get this new_id information
  684. $cvterm = tripal_core_chado_select('cvterm', array('name', 'definition'), array('cvterm_id' => $new_id));
  685. // add it to the $ranks array
  686. $ranks[$new_id][$rank]['name'] = $cvterm[0]->name;
  687. $ranks[$new_id][$rank]['id'] = $new_id;
  688. $ranks[$new_id][$rank]['value'] = $value;
  689. $ranks[$new_id][$rank]['definition'] = $cvterm[0]->definition;
  690. $num_properties++;
  691. // determine how many rows we need in the textarea
  692. $rows = 1;
  693. // add the new fields
  694. $form['properties']['table']['new'][$new_id][$rank]["new_id-$new_id-$rank"] = array(
  695. '#markup' => $cvterm[0]->name
  696. );
  697. $form['properties']['table']['new'][$new_id][$rank]["new_value-$new_id-$rank"] = array(
  698. '#type' => 'textarea',
  699. '#default_value' => $new_value,
  700. '#cols' => 50,
  701. '#rows' => $rows,
  702. '#description' => $cvterm[0]->definition,
  703. );
  704. $form['properties']['table']['new'][$new_id][$rank]["remove-$new_id-$rank"] = array(
  705. '#type' => 'button',
  706. '#value' => t('Remove'),
  707. '#name' => "remove-$new_id-$rank",
  708. '#ajax' => array(
  709. 'callback' => "tripal_core_props_property_ajax_update",
  710. 'wrapper' => 'tripal-properties-edit-properties-table',
  711. 'effect' => 'fade',
  712. 'event' => 'mousedown',
  713. 'method' => 'replace',
  714. 'prevent' => 'click'
  715. ),
  716. // When this button is clicked, the form will be validated and submitted.
  717. // Therefore, we set custom submit and validate functions to override the
  718. // default form submit. In the validate function we set the form_state
  719. // to rebuild the form so the submit function never actually gets called,
  720. // but we need it or Drupal will run the default validate anyway.
  721. // we also set #limit_validation_errors to empty so fields that
  722. // are required that don't have values won't generate warnings.
  723. '#submit' => array('tripal_core_props_form_props_button_submit'),
  724. '#validate' => array('tripal_core_props_form_props_button_validate'),
  725. '#limit_validation_errors' => array(),
  726. );
  727. }
  728. return $num_properties;
  729. }
  730. /**
  731. * This function queries the proper xxxprop table to look for existing values for the given
  732. * $id. It then adds these properties to the form for editing. It also will incorporate
  733. * extra properties that were specified manually by the caller.
  734. *
  735. */
  736. function tripal_core_properties_form_add_prop_table_props($prop_table, $id_field, $cv_name,
  737. &$form, $form_state, $id, &$ranks, &$d_removed, $exclude = array(), $include = array()) {
  738. // get the existing properties
  739. $num_properties = 0;
  740. if (!$id) {
  741. return;
  742. }
  743. // create an array of properties so we can merge those in the database with those provided by the caller
  744. $all_props = array();
  745. foreach ($include as $prop) {
  746. $all_props[] = $prop;
  747. }
  748. // now merge in properties saved in the database
  749. $sql = "
  750. SELECT CVT.cvterm_id, CVT.name, CVT.definition, PP.value, PP.rank
  751. FROM {" . $prop_table . "} PP
  752. INNER JOIN {cvterm} CVT ON CVT.cvterm_id = PP.type_id
  753. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  754. WHERE
  755. PP.$id_field = :id AND
  756. CV.name = '$cv_name'
  757. ORDER BY CVT.name, PP.rank
  758. ";
  759. $props = chado_query($sql, array(':id' => $id));
  760. while ($prop = $props->fetchObject()) {
  761. $all_props[] = array('cvterm' => $prop, 'value' => $prop->value);
  762. }
  763. // iterate through the properties
  764. foreach ($all_props as $prop) {
  765. $type_id = $prop['cvterm']->cvterm_id;
  766. $value = $prop['value'];
  767. $name = $prop['cvterm']->name;
  768. $definition = $prop['cvterm']->definition;
  769. $rank = 0;
  770. if(array_key_exists($type_id, $ranks)) {
  771. $rank = count($ranks[$type_id]);
  772. }
  773. // skip any properties that the user requested to delete through a previous
  774. // AHAH callback or through the current AHAH callback
  775. if (array_key_exists("$type_id-$rank", $d_removed)) {
  776. continue;
  777. }
  778. // skip any properties that should be excluded
  779. if (count(array_intersect(array($name), $exclude)) == 1) {
  780. continue;
  781. }
  782. if (array_key_exists('triggering_element', $form_state) and
  783. $form_state['triggering_element']['#name'] == 'remove-' . $type_id . '-' . $rank) {
  784. $d_removed["$type_id-$rank"] = 1;
  785. continue;
  786. }
  787. $ranks[$type_id][$rank]['name'] = $name;
  788. $ranks[$type_id][$rank]['id'] = $type_id;
  789. $ranks[$type_id][$rank]['value'] = $value;
  790. $num_properties++;
  791. $rows = 1;
  792. $rows = strlen($value) / 80 + 1;
  793. if ($rows > 10) {
  794. $rows = 10;
  795. }
  796. $form['properties']['table'][$type_id][$rank]["prop_id-$type_id-$rank"] = array(
  797. '#markup' => $name,
  798. );
  799. $form['properties']['table'][$type_id][$rank]["prop_value-$type_id-$rank"] = array(
  800. '#type' => 'textarea',
  801. '#default_value' => $value,
  802. '#cols' => 50,
  803. '#rows' => $rows,
  804. '#description' => '<span style="white-space: normal">' . $definition . '</span>',
  805. );
  806. $form['properties']['table'][$type_id][$rank]["remove-$type_id-$rank"] = array(
  807. '#type' => 'button',
  808. '#value' => t('Remove'),
  809. '#name' => "remove-$type_id-$rank",
  810. '#ajax' => array(
  811. 'callback' => "tripal_core_props_property_ajax_update",
  812. 'wrapper' => 'tripal-properties-edit-properties-table',
  813. 'effect' => 'fade',
  814. 'event' => 'mousedown',
  815. 'method' => 'replace',
  816. 'prevent' => 'click'
  817. ),
  818. // When this button is clicked, the form will be validated and submitted.
  819. // Therefore, we set custom submit and validate functions to override the
  820. // default form submit. In the validate function we set the form_state
  821. // to rebuild the form so the submit function never actually gets called,
  822. // but we need it or Drupal will run the default validate anyway.
  823. // we also set #limit_validation_errors to empty so fields that
  824. // are required that don't have values won't generate warnings.
  825. '#submit' => array('tripal_core_props_form_props_button_submit'),
  826. '#validate' => array('tripal_core_props_form_props_button_validate'),
  827. '#limit_validation_errors' => array(),
  828. );
  829. }
  830. return $num_properties;
  831. }
  832. /**
  833. * Form AJAX callback for adding a blank property row
  834. *
  835. * We only want to return the properties as that's all we'll replace with this callback
  836. */
  837. function tripal_core_props_property_ajax_update($form, $form_state) {
  838. $properties_html = tripal_core_props_theme_node_form_properties($form['properties']['table']);
  839. $form['properties']['table'] = array(
  840. '#markup' => $properties_html,
  841. '#prefix' => '<div id="tripal-properties-edit-properties-table">',
  842. '#suffix' => '</div>',
  843. );
  844. return $form['properties']['table'];
  845. }
  846. /**
  847. * Form AJAX callback for updating a property description. This
  848. * function only gets called when the property drop down is changed
  849. * on the bottom (empty) row of properties
  850. */
  851. function tripal_core_props_property_ajax_get_description($form, $form_state) {
  852. return $form['properties']['table']['new']["new_value"];
  853. }
  854. /**
  855. * We need to theme the form so that the properties fields look good
  856. */
  857. function theme_tripal_core_properties_form($variables) {
  858. $form = $variables['form'];
  859. $properties_table = tripal_core_props_theme_node_form_properties($form);
  860. $markup = $properties_table;
  861. $form['properties']['table'] = array(
  862. '#markup' => $markup,
  863. '#prefix' => '<div id="tripal-properties-edit-properties-table">',
  864. '#suffix' => '</div>',
  865. );
  866. $form['buttons']['#weight'] = 50;
  867. return drupal_render($form['properties']['table']);
  868. }
  869. /**
  870. *
  871. */
  872. function tripal_core_props_theme_node_form_properties($form) {
  873. $rows = array();
  874. // first add in the properties derived from the prop table
  875. // the array tree for these properties looks like this:
  876. // $form['properties']['table'][$type_id][$rank]["prop_id-$type_id-$rank"]
  877. foreach ($form as $type_id => $elements) {
  878. // there are other fields in the properties array so we only
  879. // want the numeric ones those are our type_id
  880. if (is_numeric($type_id)) {
  881. foreach ($elements as $rank => $element) {
  882. if (is_numeric($rank)) {
  883. $rows[] = array(
  884. drupal_render($element["prop_id-$type_id-$rank"]),
  885. drupal_render($element["prop_value-$type_id-$rank"]),
  886. drupal_render($element["remove-$type_id-$rank"]),
  887. );
  888. }
  889. }
  890. }
  891. }
  892. // second, add in any new properties added by the user through AHAH callbacks
  893. // the array tree for these properties looks like this:
  894. // $form['properties']['table']['new'][$type_id][$rank]["new_id-$new_id-$rank"]
  895. foreach ($form['new'] as $type_id => $elements) {
  896. if (is_numeric($type_id)) {
  897. foreach ($elements as $rank => $element) {
  898. if (is_numeric($rank)) {
  899. $rows[] = array(
  900. drupal_render($element["new_id-$type_id-$rank"]),
  901. drupal_render($element["new_value-$type_id-$rank"]),
  902. drupal_render($element["remove-$type_id-$rank"]),
  903. );
  904. }
  905. }
  906. }
  907. }
  908. // finally add in a set of blank field for adding a new property
  909. $rows[] = array(
  910. drupal_render($form['new']['new_id']),
  911. array(
  912. 'data' => drupal_render($form['new']['new_value']),
  913. 'width' => '60%',
  914. ),
  915. drupal_render($form['new']['add']),
  916. );
  917. $headers = array('Property Type', 'Value', 'Actions');
  918. $table = array(
  919. 'header' => $headers,
  920. 'rows' => $rows,
  921. 'attributes' => array(),
  922. 'sticky' => TRUE,
  923. 'caption' => '',
  924. 'colgroups' => array(),
  925. 'empty' => '',
  926. );
  927. return theme_table($table);
  928. }
  929. /**
  930. * This function is used in a hook_insert, hook_update for a node form
  931. * when the properties form has been added to the form. It retrieves all of the properties
  932. * and returns them in an array of the format:
  933. *
  934. * $properties[<property name>][<rank>] = <value
  935. *
  936. * This array can then be used for inserting or updating properties using the API call
  937. * tripal_hook_insert_property()
  938. *
  939. * @param $node
  940. * @param $cvname
  941. * The name of the controlled vocabulary that the properties belong to
  942. *
  943. * @return
  944. * A properties array
  945. *
  946. * @ingroup tripal_properties_api
  947. */
  948. function tripal_core_properties_form_retreive($node, $cv_name) {
  949. // now add the properties
  950. $properties = array(); // stores all of the properties we need to add
  951. // get the list of properties for easy lookup (without doing lots of database queries
  952. $properties_list = array();
  953. $sql = "
  954. SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
  955. FROM {cvterm} CVT
  956. INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
  957. WHERE
  958. CV.name = '$cv_name' AND
  959. NOT CVT.is_obsolete = 1
  960. ORDER BY CVT.name ASC
  961. ";
  962. $prop_types = chado_query($sql);
  963. while ($prop = $prop_types->fetchObject()) {
  964. $properties_list[$prop->cvterm_id] = $prop->name;
  965. }
  966. // get the properties that should be added. Properties are in one of two forms:
  967. // 1) prop_value-[type id]-[index]
  968. // 2) new_value-[type id]-[index]
  969. // 3) new_id, new_value
  970. foreach ($node as $name => $value) {
  971. if (preg_match('/^new_value-(\d+)-(\d+)/', $name, $matches)) {
  972. $type_id = $matches[1];
  973. $index = $matches[2];
  974. $name = $properties_list[$type_id];
  975. $properties[$name][$index] = trim($value);
  976. }
  977. if (preg_match('/^prop_value-(\d+)-(\d+)/', $name, $matches)) {
  978. $type_id = $matches[1];
  979. $index = $matches[2];
  980. $name = $properties_list[$type_id];
  981. $properties[$name][$index] = trim($value);
  982. }
  983. }
  984. if (property_exists($node, 'new_id') and $node->new_id and property_exists($node, 'new_value') and $node->new_value) {
  985. $type_id = $node->new_id;
  986. $name = $properties_list[$type_id];
  987. $index = 0;
  988. if (array_key_exists($name, $properties)) {
  989. $index = count($properties[$name]);
  990. }
  991. $properties[$name][$index] = trim($node->new_value);
  992. }
  993. return $properties;
  994. }