tripal_core.chado_nodes.properties.api.inc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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. // Default values for form elements can come in the following ways:
  10. //
  11. // 1) as elements of the $node object. This occurs when editing an existing node
  12. // 2) in the $form_state['values'] array which occurs on a failed validation or
  13. // ajax callbacks when the ajax call originates from non-submit fields other
  14. // than button
  15. // 3) in the $form_state['input'] array which occurs on ajax callbacks from submit
  16. // form elements (e.g. buttons) and the form is being rebuilt but has not yet
  17. // been validated
  18. //
  19. // The reference elements added by this function do use AJAX calls from buttons,
  20. // therefore, it is important to check for form values in the $form_state['values']
  21. // for case #2 above, and in the $form_state['input'] for case #3.
  22. // See the chado analysis node form for an example.
  23. // Next, add in all the form array definition particular to your node type
  24. // To add in the chado properties form elements, you first need to prepare the arguments
  25. // for the function call.
  26. $details = array(
  27. 'property_table' => 'example_property', // the name of the table linking additional properties to this node
  28. 'chado_id_field' => 'example_id', // key to link to the chado content created by this node
  29. 'chado_id' => $example_id, // the value of the above key
  30. 'cv_name' => 'example_prop_cv', // the name of the cv governing the _prop.type_id
  31. 'fieldset_title' => 'Additional References', // the non-translated title for this fieldset
  32. 'additional_instructions' => '' // a non-stranslated string providing additional instructions
  33. );
  34. // Finally, and add the additional form elements to the form
  35. chado_add_node_form_properties($form, $form_state, $details);
  36. return $form;
  37. }
  38. function chado_example_insert($node) {
  39. // if there is an example_id in the $node object then this must be a sync so
  40. // we can skip adding the chado_example as it is already there, although
  41. // we do need to proceed with the rest of the insert
  42. if (!property_exists($node, 'example_id')) {
  43. // Add record to chado example table
  44. // Add to any other tables needed
  45. // Add all properties
  46. // Existing _property links will be cleared and then re-added
  47. tripal_api_chado_node_properties_form_update_properties(
  48. $node, // the node object passed in via hook_insert()
  49. 'example_property', // the name of the _property linking table
  50. 'example', // the name of the base chado table for the node
  51. 'example_id', // key to link to the chado content created by this node
  52. $node->example_id // value of the above key
  53. );
  54. }
  55. // Add record to chado_example linking example_id to new node
  56. }
  57. function chado_example_update($node) {
  58. // Update record in chado example table
  59. // Update any other tables needed
  60. // Update all properties
  61. // Existing _property links will be cleared and then re-added
  62. tripal_api_chado_node_properties_form_update_properties(
  63. $node, // the node object passed in via hook_insert()
  64. 'example_property', // the name of the _property linking table
  65. 'example', // the name of the base chado table for the node
  66. 'example_id', // key to link to the chado content created by this node
  67. $node->example_id // value of the above key
  68. );
  69. // Don't need to update chado_example linking table since niether example_id or nid can be changed in update
  70. }
  71. * @endcode
  72. *
  73. * @ingroup tripal_chado_node_api
  74. */
  75. /**
  76. * Provides a form for adding to BASEprop table
  77. *
  78. * @param $form
  79. * The Drupal form array into which the property form elements will be added
  80. * @param $form_state
  81. * The corresponding form_state array for the form
  82. * @param $details
  83. * An array defining details used by this form.
  84. * Required keys that are always required:
  85. * - property_table: the name of the property table (e.g.: featureprop, stockprop, etc.)
  86. * Required keys for forms that update a record.
  87. * - chado_id: the id of the record to which properties will be associated (e.g.: if a
  88. * feature has a feature_id of 999 and we want to associate properties for that feature
  89. * then the chado_id option should be 999)
  90. * Require ONE of the following to identify the controlled vocabulary containing the properties to use:
  91. * -cv_id: the unique key from the cv table
  92. * -cv_name: the cv.name field uniquely identifying the controlled vocabulary
  93. * Optional keys include:
  94. * - chado_id_field: the foreign key field that links properties to the
  95. * chado_id record. If this value is not specified it is determined using the
  96. * traditional Chado naming scheme for property tables.
  97. * - additional_instructions: provides additional instructions to the user
  98. * for dealing with the property elements. These instructions are appended
  99. * to the default instructions
  100. * - fieldset_title: An alternate name for the fieldset in which the properties
  101. * form is placed. By default the title is 'Properties'.
  102. * - default_properties: An array of properties used to initialize the
  103. * properties form. Each property shoudl be represented as an array with
  104. * the following keys and values:
  105. * 'cvterm': The cvterm object for the property type
  106. * 'value': The property value
  107. *
  108. * @ingroup tripal_chado_node_api
  109. */
  110. function chado_add_node_form_properties(&$form, &$form_state, $details) {
  111. // Set defaults for optional fields
  112. if (!array_key_exists('fieldset_title', $details)){
  113. $details['fieldset_title'] = 'Properties';
  114. }
  115. if (!array_key_exists('additional_instructions', $details)){
  116. $details['additional_instructions'] = '';
  117. };
  118. if (!array_key_exists('default_properties', $details)){
  119. $details['default_properties'] = array();
  120. };
  121. if (!is_array($details['default_properties'])) {
  122. drupal_set_message("The 'default_properties' option must be an array", "error");
  123. tripal_report_error('tcprops_form', TRIPAL_ERROR,
  124. "The 'default_properties' option must be an array",
  125. array());
  126. return;
  127. }
  128. // make sure the property table exists before proceeding.
  129. if (!chado_table_exists($details['property_table'])) {
  130. drupal_set_message("Cannot add property elements to the form. The property table, '" .
  131. $details['property_table'] . "', does not exists", "error");
  132. tripal_report_error('tcprops_form', TRIPAL_ERROR,
  133. "Cannot add property elements to the form. The property table, '%name', cannot be found.",
  134. array('%name' => $details['property_table']));
  135. return;
  136. }
  137. // if the chado_id_field is not specified then set it using the
  138. // typical chado naming scheme
  139. if (!array_key_exists('chado_id_field', $details)) {
  140. $chado_id_table = preg_replace('/prop$/', '', $details['property_table']);
  141. $chado_id_field = $chado_id_table . '_id';
  142. $details['chado_id_field'] = $chado_id_field;
  143. }
  144. // make sure the specified cv exists
  145. if (isset($details['cv_name'])) {
  146. // make sure the cv_name is real
  147. $result = chado_select_record('cv',array('cv_id'),array('name' => $details['cv_name']));
  148. if (count($result) == 0) {
  149. drupal_set_message("Cannot add property elements to the form. The CV name, '" .
  150. $details['cv_name'] . "', does not exists", "error");
  151. tripal_report_error('tcprops_form', TRIPAL_ERROR,
  152. "Cannot add property elements to the form. The CV named, '%name', cannot be found.",
  153. array('%name' => $details['cv_name']));
  154. return;
  155. }
  156. // add the cv_id option to the details array
  157. $details['cv_id'] = $result[0]->cv_id;
  158. }
  159. elseif (isset($details['cv_id'])) {
  160. // make sure the cv_id is real
  161. $result = chado_select_record('cv', array('name'), array('cv_id' => $details['cv_id']));
  162. if (count($result) == 0) {
  163. drupal_set_message("Cannot add property elements to the form. The CV ID, '" .
  164. $details['cv_id'] . "', does not exist", "error");
  165. tripal_report_error('tcprops_form', TRIPAL_ERROR,
  166. "Cannot add property elements to the form. The CV ID, '%id', cannot be found.",
  167. array('%id' => $details['cv_id']));
  168. return;
  169. }
  170. // add the cv_name option to the details array
  171. $details['cv_name'] = $result[0]->name;
  172. }
  173. else {
  174. drupal_set_message("Please provide either a 'cv_name' or 'cv_id' as an
  175. option for adding properties to the form", "error");
  176. tripal_report_error('tcprops_form', TRIPAL_ERROR,
  177. "Please provide either a 'cv_name' or 'cv_id' as an option for adding properties to the form",
  178. array());
  179. return;
  180. }
  181. // Get Property Types for the Select List
  182. if (isset($details['select_options'])) {
  183. $property_options = $details['select_options'];
  184. }
  185. // if the select options are not provided then try to get them on our own
  186. else {
  187. // if the vocabulary name is provided in the details then use that to
  188. // get the terms
  189. if (isset($details['cv_name'])) {
  190. $property_options = array();
  191. $property_options[] = 'Select a Property';
  192. $sql = "
  193. SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition, CV.cv_id as cv_id
  194. FROM {cvterm} CVT
  195. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  196. WHERE
  197. CV.name = :cv_name AND
  198. NOT CVT.is_obsolete = 1
  199. ORDER BY CVT.name ASC
  200. ";
  201. $prop_types = chado_query($sql, array(':cv_name' => $details['cv_name']));
  202. while ($prop = $prop_types->fetchObject()) {
  203. $property_options[$prop->cvterm_id] = $prop->name;
  204. }
  205. }
  206. // if the cv_id is set in the $details array then use that to get the terms
  207. elseif (isset($details['cv_id'])) {
  208. $property_options = array();
  209. $property_options[] = 'Select a Property';
  210. $sql = "
  211. SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition, CV.name as cv_name
  212. FROM {cvterm} CVT
  213. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  214. WHERE
  215. CV.cv_id = :cv_id AND
  216. NOT CVT.is_obsolete = 1
  217. ORDER BY CVT.name ASC
  218. ";
  219. $prop_types = chado_query($sql, array(':cv_id' => $details['cv_id']));
  220. while ($prop = $prop_types->fetchObject()) {
  221. $property_options[$prop->cvterm_id] = $prop->name;
  222. }
  223. }
  224. }
  225. // Tell tripal administrators how to add terms to the property types drop down.
  226. if (empty($property_options)) {
  227. $tripal_message = tripal_set_message(
  228. t('There are currently no proeprty types! To add additional properties to the drop
  229. down list, you need to <a href="@cvtermlink">add a controlled vocabulary term</a>
  230. to the %cv_name controlled vocabulary.',
  231. array(
  232. '%cv_name' => $details['cv_name'],
  233. '@cvtermlink' => url('admin/tripal/chado/tripal_cv/cv/' . $details['cv_id'] . '/cvterm/add')
  234. )
  235. ),
  236. TRIPAL_WARNING,
  237. array('return_html' => TRUE)
  238. );
  239. }
  240. else {
  241. $tripal_message = tripal_set_message(
  242. t('To add additional properties to the drop down list, you need to <a href="@cvtermlink">add
  243. a controlled vocabulary term</a> to the %cv_name controlled vocabulary.',
  244. array(
  245. '%cv_name' => $details['cv_name'],
  246. '@cvtermlink' => url('admin/tripal/chado/tripal_cv/cv/' . $details['cv_id'] . '/cvterm/add')
  247. )
  248. ),
  249. TRIPAL_INFO,
  250. array('return_html' => TRUE)
  251. );
  252. }
  253. // the fieldset of the property elements
  254. $form['properties'] = array(
  255. '#type' => 'fieldset',
  256. '#title' => t($details['fieldset_title']),
  257. '#description' => t('You may add additional properties by selecting a property type
  258. from the dropdown and adding text. You may add as many properties as desired by
  259. clicking the add button on the right. To remove a property, click the remove button.'
  260. . $details['additional_instructions']),
  261. '#prefix' => "<div id='properties-fieldset'>",
  262. '#suffix' => '</div>',
  263. '#weight' => 8
  264. );
  265. $form['properties']['admin_message'] = array(
  266. '#type' => 'markup',
  267. '#markup' => $tripal_message
  268. );
  269. // this form element is a tree, so that we don't puke all of the values into then node variable
  270. // it is set as a tree, and keeps them in the $form_state['values']['property_table'] heading.
  271. $form['properties']['property_table'] = array(
  272. '#type' => 'markup',
  273. '#tree' => TRUE,
  274. '#prefix' => '<div id="tripal-generic-edit-properties-table">',
  275. '#suffix' => '</div>',
  276. '#theme' => 'chado_node_properties_form_table'
  277. );
  278. // Add defaults into form_state to be used elsewhere
  279. $form['properties']['property_table']['details'] = array(
  280. '#type' => 'hidden',
  281. '#value' => serialize($details)
  282. );
  283. /* Properties can come to us in two ways:
  284. * 1) As entries in the $details['default_properties'] option
  285. *
  286. * 2) In the form state in the $form_state['chado_properties']. Data is in this field
  287. * when an AJAX call updates the form state or a validation error.
  288. *
  289. * 3) Directly from the database if the record already has properties associated. This
  290. * data is only used the first time the form is loaded. On AJAX calls or validation
  291. * errors the fields on the form are populated from the $form_state['chado_properties']
  292. * entry.
  293. */
  294. if (isset($form_state['chado_properties'])) {
  295. $existing_properties = $form_state['chado_properties'];
  296. }
  297. else {
  298. $ranks = array(); // a temporary array used for calculating rank
  299. // bulid the SQL for extracting properites already assigned to this record
  300. $sql_args = array();
  301. $sql_args[':chado_id'] = $details['chado_id'];
  302. if (array_key_exists('cv_name', $details)) {
  303. $cv_where = "CV.name = :cvname";
  304. $sql_args[':cvname'] = $details['cv_name'];
  305. }
  306. elseif (array_key_exists('cv_id', $details)) {
  307. $cv_where = "CV.cv_id = :cvid";
  308. $sql_args[':cvid'] = $details['cv_id'];
  309. }
  310. $existing_properties = chado_query(
  311. "SELECT
  312. PP.".$details['property_table']."_id property_id,
  313. CVT.cvterm_id as type_id,
  314. CVT.name as type_name,
  315. CVT.definition,
  316. PP.value,
  317. PP.rank
  318. FROM {" . $details['property_table'] . "} PP
  319. INNER JOIN {cvterm} CVT ON CVT.cvterm_id = PP.type_id
  320. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  321. WHERE
  322. PP." . $details['chado_id_field'] . " = :chado_id AND
  323. $cv_where
  324. ORDER BY CVT.name, PP.rank", $sql_args)->fetchAll();
  325. // iterate through the results and get the largest rank for each type
  326. foreach ($existing_properties as $property) {
  327. if (array_key_exists($property->type_id, $ranks)) {
  328. if($ranks[$property->type_id] < $propety->rank) {
  329. $ranks[$property->type_id] = $property->rank;
  330. }
  331. }
  332. else {
  333. $ranks[$property->type_id] = $property->rank;
  334. }
  335. }
  336. // next add in any default properties
  337. if (array_key_exists('default_properties', $details)) {
  338. // next iterate through each of the default properties and create a new
  339. // stdClass array that contains the fields needed.
  340. foreach ($details['default_properties'] as $property) {
  341. $new_prop = new stdClass();
  342. $new_prop->type_id = $property['cvterm']->cvterm_id;
  343. $new_prop->type_name = $property['cvterm']->name;
  344. $new_prop->definition = $property['cvterm']->definition;
  345. $new_prop->value = $property['value'];
  346. $new_prop->property_id = NULL;
  347. // to set the rank for this property, we need to make sure we set
  348. // it greater than any already existing rank
  349. if (array_key_exists($property['cvterm']->cvterm_id, $ranks)) {
  350. $ranks[$property['cvterm']->cvterm_id]++;
  351. }
  352. else {
  353. $ranks[$property['cvterm']->cvterm_id] = 0;
  354. }
  355. $new_prop->rank = $ranks[$property['cvterm']->cvterm_id];
  356. $existing_properties[] = $new_prop;
  357. }
  358. }
  359. }
  360. /* The format of the $existing_properties array is either:
  361. *
  362. * From the chado_properties array:
  363. * $form_state['chado_properties'] = array(
  364. * '[type_id]-[rank]' => array(
  365. * 'type_id' => [the cvterm.cvterm_id value]
  366. * 'type_name' => [the cvterm.name value]
  367. * 'property_id' => [the property.property_id value, or NULL if it doesn't yet exist],
  368. * 'value' => [the BASEprop.value value],
  369. * 'rank' => [the BASEprop.rank value],
  370. * ),
  371. * );
  372. *
  373. * OR
  374. * Populated from the database:
  375. * $existing_property = array(
  376. * 0 => array(
  377. * 'property_id' => [the property.property_id value, or NULL if it doesn't yet exist],
  378. * 'type_id' => [the cvterm.cvterm_id value]
  379. * 'type_name' => [the cvterm.name value]
  380. * 'value' => [the BASEprop.value value],
  381. * 'rank' => [the BASEprop.rank value],
  382. * ),
  383. * );
  384. *
  385. * NOTE: The main difference is the key
  386. *
  387. * Loop on the array elements of the $existing_properties array and add
  388. * an element to the form for each one as long as it's also in the
  389. * $properties_options array.
  390. */
  391. foreach ($existing_properties as $property) {
  392. if (array_key_exists($property->type_id, $property_options)) {
  393. $form['properties']['property_table'][$property->type_id]['#type'] = 'markup';
  394. $form['properties']['property_table'][$property->type_id]['#value'] = '';
  395. $form['properties']['property_table'][$property->type_id][$property->rank]['#type'] = 'markup';
  396. $form['properties']['property_table'][$property->type_id][$property->rank]['#value'] = '';
  397. $form['properties']['property_table'][$property->type_id][$property->rank]['prop_type_id'] = array(
  398. '#type' => 'hidden',
  399. '#value' => $property->type_id
  400. );
  401. $form['properties']['property_table'][$property->type_id][$property->rank]['prop_value'] = array(
  402. '#type' => 'hidden',
  403. '#value' => $property->value
  404. );
  405. $form['properties']['property_table'][$property->type_id][$property->rank]['property_id'] = array(
  406. '#type' => 'hidden',
  407. '#value' => $property->property_id
  408. );
  409. $form['properties']['property_table'][$property->type_id][$property->rank]['type'] = array(
  410. '#type' => 'markup',
  411. '#markup' => $property->type_name . '<br><i>' . $property->definition . '</i>'
  412. );
  413. $form['properties']['property_table'][$property->type_id][$property->rank]['value'] = array(
  414. '#type' => 'markup',
  415. '#markup' => $property->value,
  416. );
  417. $form['properties']['property_table'][$property->type_id][$property->rank]['rank'] = array(
  418. '#type' => 'markup',
  419. '#markup' => $property->rank
  420. );
  421. // remove button
  422. $form['properties']['property_table'][$property->type_id][$property->rank]['property_action'] = array(
  423. '#type' => 'submit',
  424. '#value' => t('Remove'),
  425. '#name' => "property_remove-".$property->type_id.'-'.$property->rank,
  426. '#ajax' => array(
  427. 'callback' => "chado_add_node_form_properties_ajax_update",
  428. 'wrapper' => 'tripal-generic-edit-properties-table',
  429. 'effect' => 'fade',
  430. 'method' => 'replace',
  431. 'prevent' => 'click'
  432. ),
  433. // When this button is clicked, the form will be validated and submitted.
  434. // Therefore, we set custom submit and validate functions to override the
  435. // default node form submit. In the validate function we validate only the
  436. // property fields and in the submit we remove the indicated property
  437. // from the chado_properties array. In order to keep validate errors
  438. // from the node form validate and Drupal required errors for non-property fields
  439. // preventing the user from removing properties we set the #limit_validation_errors below
  440. '#validate' => array('chado_add_node_form_properties_remove_button_validate'),
  441. '#submit' => array('chado_add_node_form_properties_remove_button_submit'),
  442. // Limit the validation of the form upon clicking this button to the property_table tree
  443. // No other fields will be validated (ie: no fields from the main form or any other api
  444. // added form).
  445. '#limit_validation_errors' => array(
  446. array('property_table') // Validate all fields within $form_state['values']['property_table']
  447. )
  448. );
  449. }
  450. }
  451. // Form elements for adding a new property
  452. //---------------------------------------------
  453. $form['properties']['property_table']['new'] = array(
  454. '#type' => 'markup',
  455. '#prefix' => '<span class="addtl-properties-add-new-property">',
  456. '#suffix' => '</span>'
  457. );
  458. // get the value selected (only works on AJAX call) and print the
  459. // description
  460. $type_desc = '';
  461. if (isset($form_state['input']['property_table']['new']['type'])) {
  462. $new_type_id = $form_state['values']['property_table']['new']['type'];
  463. $new_term = tripal_get_cvterm(array('cvterm_id' => $new_type_id));
  464. $type_desc = $new_term->definition;
  465. }
  466. $form['properties']['property_table']['new']['type'] = array(
  467. '#type' => 'select',
  468. '#options' => $property_options, // Set at top of form
  469. '#prefix' => '<span id="tripal-generic-edit-properties-new-desc">',
  470. '#suffix' => '<i>' . $type_desc . '</i></span>',
  471. '#ajax' => array(
  472. 'callback' => "chado_add_node_form_properties_ajax_desc",
  473. 'wrapper' => 'tripal-generic-edit-properties-new-desc',
  474. 'effect' => 'fade',
  475. 'method' => 'replace',
  476. ),
  477. );
  478. $form['properties']['property_table']['new']['value'] = array(
  479. '#type' => 'textarea',
  480. '#rows' => 2,
  481. );
  482. // add button
  483. $form['properties']['property_table']['new']['property_action'] = array(
  484. '#type' => 'submit',
  485. '#value' => t('Add'),
  486. '#name' => "property-add",
  487. '#ajax' => array(
  488. 'callback' => "chado_add_node_form_properties_ajax_update",
  489. 'wrapper' => 'tripal-generic-edit-properties-table',
  490. 'effect' => 'fade',
  491. 'method' => 'replace',
  492. 'prevent' => 'click'
  493. ),
  494. // When this button is clicked, the form will be validated and submitted.
  495. // Therefore, we set custom submit and validate functions to override the
  496. // default node form submit. In the validate function we validate only the
  497. // additional property fields and in the submit we add them to the chado_properties
  498. // array. In order to keep validate errors from the node form validate and Drupal
  499. // required errors for non-property fields preventing the user from adding properties we
  500. // set the #limit_validation_errors below
  501. '#validate' => array('chado_update_node_form_properties_add_button_validate'),
  502. '#submit' => array('chado_add_node_form_properties_add_button_submit'),
  503. // Limit the validation of the form upon clicking this button to the property_table tree
  504. // No other fields will be validated (ie: no fields from the main form or any other api
  505. // added form).
  506. '#limit_validation_errors' => array(
  507. array('property_table') // Validate all fields within $form_state['values']['property_table']
  508. )
  509. );
  510. }
  511. /**
  512. * Validate the user input for creating a new property
  513. * Called by the add button in chado_add_node_form_properties
  514. *
  515. * @ingroup tripal_core
  516. */
  517. function chado_update_node_form_properties_add_button_validate($form, &$form_state) {
  518. // Ensure the type_id is supplied & Valid
  519. $cvterm = chado_select_record(
  520. 'cvterm',
  521. array('cvterm_id', 'name', 'definition'),
  522. array('cvterm_id' => $form_state['values']['property_table']['new']['type'])
  523. );
  524. if (!isset($cvterm[0])) {
  525. form_set_error('property_table][new][cvterm', 'Please select a property type before attempting to add a new property.');
  526. }
  527. else {
  528. $form_state['values']['property_table']['new']['type_name'] = $cvterm[0]->name;
  529. $form_state['values']['property_table']['new']['definition'] = $cvterm[0]->definition;
  530. }
  531. // Ensure value is supplied
  532. if (empty($form_state['values']['property_table']['new']['value'])) {
  533. form_set_error('property_table][new][value','You must enter the property value before attempting to add a new property.');
  534. }
  535. }
  536. /**
  537. * Called by the add button in chado_add_node_form_properties
  538. *
  539. * Create an array of properties in the form state. This array will then be
  540. * used to rebuild the form in subsequent builds
  541. *
  542. * @ingroup tripal_core
  543. */
  544. function chado_add_node_form_properties_add_button_submit(&$form, &$form_state) {
  545. $details = unserialize($form_state['values']['property_table']['details']);
  546. // if the chado_additional_properties array is not set then this is the first time modifying the
  547. // property table. this means we need to include all the properties from the db
  548. if (!isset($form_state['chado_properties'])) {
  549. chado_add_node_form_properties_create_property_formstate_array($form, $form_state);
  550. }
  551. // get details for the new property
  552. $property = array(
  553. 'type_id' => $form_state['values']['property_table']['new']['type'],
  554. 'type_name' => $form_state['values']['property_table']['new']['type_name'],
  555. 'definition' => $form_state['values']['property_table']['new']['definition'],
  556. 'property_id' => NULL,
  557. 'value' => $form_state['values']['property_table']['new']['value'],
  558. 'rank' => '0',
  559. );
  560. // get max rank
  561. $rank = chado_get_table_max_rank(
  562. $details['property_table'],
  563. array(
  564. $details['chado_id_field'] => $details['chado_id'],
  565. 'type_id' => $property['type_id']
  566. )
  567. );
  568. $property['rank'] = strval($rank + 1);
  569. $key = $property['type_id'] . '-' . $property['rank'];
  570. $form_state['chado_properties'][$key] = (object) $property;
  571. // we don't want the new element to pick up the values from the previous element
  572. // so wipe them out
  573. unset($form_state['input']['property_table']['new']['type']);
  574. unset($form_state['input']['property_table']['new']['type_name']);
  575. unset($form_state['input']['property_table']['new']['definition']);
  576. unset($form_state['input']['property_table']['new']['value']);
  577. $form_state['rebuild'] = TRUE;
  578. }
  579. /**
  580. * There is no user input for the remove buttons so there is no need to validate
  581. * However, both a submit & validate need to be specified so this is just a placeholder
  582. *
  583. * Called by the many remove buttons in chado_add_node_form_properties
  584. *
  585. * @ingroup tripal_core
  586. */
  587. function chado_add_node_form_properties_remove_button_validate($form, $form_state) {
  588. // No Validation needed for remove
  589. }
  590. /**
  591. * Remove the correct property from the form
  592. * Called by the many remove buttons in chado_add_node_form_properties
  593. *
  594. * @ingroup tripal_core
  595. */
  596. function chado_add_node_form_properties_remove_button_submit(&$form, &$form_state) {
  597. // if the chado_properties array is not set then this is the first time modifying the
  598. // property table. this means we need to include all the properties from the db
  599. if (!isset($form_state['chado_properties'])) {
  600. chado_add_node_form_properties_create_property_formstate_array($form, $form_state);
  601. }
  602. // remove the specified property from the form property table
  603. if(preg_match('/property_remove-([^-]+-[^-]+)/',$form_state['triggering_element']['#name'],$match)) {
  604. $key = $match[1];
  605. if (array_key_exists($key, $form_state['chado_properties'])) {
  606. unset($form_state['chado_properties'][$key]);
  607. }
  608. }
  609. $form_state['rebuild'] = TRUE;
  610. }
  611. function chado_add_node_form_properties_ajax_desc($form, $form_state) {
  612. return $form['properties']['property_table']['new']['type'];
  613. }
  614. /**
  615. * Ajax function which returns the section of the form to be re-rendered
  616. *
  617. * @ingroup tripal_core
  618. */
  619. function chado_add_node_form_properties_ajax_update($form, $form_state) {
  620. return $form['properties']['property_table'];
  621. }
  622. /**
  623. * Creates an array in form_state containing the existing properties. This array is
  624. * then modified by the add/remove buttons and used as a source for rebuilding the form.
  625. * This function get's called at each button (add and remove) button submits the first
  626. * time one of the button's is clicked to instantiates the $form_state['chado_properties'] array
  627. *
  628. * $form_state['chado_properties'] = array(
  629. * '[type_id]-[rank]' => array(
  630. * 'type_id' => [the cvterm.cvterm_id value]
  631. * 'type_name' => [the cvterm.name value]
  632. * 'property_id' => [the property.property_id value, or NULL if it doesn't yet exist],
  633. * 'value' => [the BASEprop.value value],
  634. * 'rank' => [the BASEprop.rank value],
  635. * ),
  636. * );
  637. *
  638. * @ingroup tripal_core
  639. */
  640. function chado_add_node_form_properties_create_property_formstate_array($form, &$form_state) {
  641. $form_state['chado_properties'] = array();
  642. foreach (element_children($form['properties']['property_table']) as $type_id) {
  643. if ($type_id != 'new') {
  644. foreach (element_children($form['properties']['property_table'][$type_id]) as $rank) {
  645. $element = $form['properties']['property_table'][$type_id][$rank];
  646. $property = array(
  647. 'type_id' => $element['prop_type_id']['#value'],
  648. 'type_name' => $element['type']['#markup'],
  649. 'property_id' => $element['property_id']['#value'],
  650. 'value' => $element['value']['#markup'],
  651. 'rank' => $element['rank']['#markup']
  652. );
  653. $key = $property['type_id'] . '-' . $property['rank'];
  654. $form_state['chado_properties'][$key] = (object) $property;
  655. }
  656. }
  657. }
  658. }
  659. /**
  660. * Function to theme the add/remove properties form into a table
  661. *
  662. * @ingroup tripal_chado_node_api
  663. */
  664. function theme_chado_add_node_form_properties($variables) {
  665. $element = $variables['element'];
  666. $header = array(
  667. 'type' => array('data' => t('Type'), 'width' => '30%'),
  668. 'value' => array('data' => t('Value'), 'width' => '50%'),
  669. 'property_action' => array('data' => t('Actions'),'width' => '20%'),
  670. );
  671. $rows = array();
  672. foreach (element_children($element) as $type_id) {
  673. if ($type_id == 'new') {
  674. $row = array();
  675. $row['data'] = array();
  676. foreach ($header as $fieldname => $title) {
  677. $row['data'][] = drupal_render($element[$type_id][$fieldname]);
  678. }
  679. $rows[] = $row;
  680. }
  681. else {
  682. foreach (element_children($element[$type_id]) as $version) {
  683. $row = array();
  684. $row['data'] = array();
  685. foreach ($header as $fieldname => $title) {
  686. $row['data'][] = drupal_render($element[$type_id][$version][$fieldname]);
  687. }
  688. $rows[] = $row;
  689. }
  690. }
  691. }
  692. return theme('table', array(
  693. 'header' => $header,
  694. 'rows' => $rows
  695. ));
  696. }
  697. /**
  698. * This function is used in a hook_insert, hook_update for a node form
  699. * when the chado node properties form has been added to the form. It retrieves all of the properties
  700. * and returns them in an array of the format:
  701. *
  702. * $dbxefs[<type_id>][<rank>] = <value>
  703. *
  704. * This array can then be used for inserting or updating properties
  705. *
  706. * @param $node
  707. *
  708. * @return
  709. * A property array
  710. *
  711. * @ingroup tripal_chado_node_api
  712. */
  713. function chado_retrieve_node_form_properties($node) {
  714. $properties = array();
  715. if (isset($node->property_table)) {
  716. foreach ($node->property_table as $type_id => $elements) {
  717. if ($type_id != 'new' AND $type_id != 'details') {
  718. foreach ($elements as $rank => $element) {
  719. $properties[$type_id][$rank] = $element['prop_value'];
  720. }
  721. }
  722. }
  723. }
  724. return $properties;
  725. }
  726. /**
  727. * This function is used in hook_insert or hook_update and handles inserting of any new
  728. * properties
  729. *
  730. * @param $node
  731. * The node passed into hook_insert & hook_update
  732. * @param $details
  733. * - property_table: the name of the _property linking table (ie: feature_property)
  734. * - base_table: the name of the base table (ie: feature)
  735. * - foreignkey_name: the name of the foreign key used to link to the node content (ie: feature_id)
  736. * - foreignkey_value: the value of the foreign key (ie: 445, if there exists a feature where feature_id=445)
  737. * @param $retrieved_properties
  738. * An array of properties from chado_retrieve_node_form_properties($node). This can be used if you need
  739. * special handling for some of the properties (See FeatureMap chado_featuremap_insert for an example)
  740. *
  741. * @ingroup tripal_chado_node_api
  742. */
  743. function chado_update_node_form_properties($node, $details, $retrieved_properties = FALSE) {
  744. $details['foreignkey_value'] = (isset($details['foreignkey_value'])) ? $details['foreignkey_value'] : 0;
  745. if (isset($node->property_table) AND ($details['foreignkey_value'] > 0)) {
  746. // First remove existing property links
  747. chado_delete_record($details['property_table'], array($details['foreignkey_name'] => $details['foreignkey_value']));
  748. // Add back in property links and insert properties as needed
  749. if ($retrieved_properties) {
  750. $properties = $retrieved_properties;
  751. }
  752. else {
  753. $properties = chado_retrieve_node_form_properties($node);
  754. }
  755. foreach ($properties as $type_id => $ranks) {
  756. foreach ($ranks as $rank => $value) {
  757. $success = chado_insert_record(
  758. $details['property_table'],
  759. array(
  760. $details['foreignkey_name'] => $details['foreignkey_value'],
  761. 'type_id' => $type_id,
  762. 'value' => $value,
  763. 'rank' => $rank
  764. )
  765. );
  766. if (!$success) {
  767. tripal_report_error('tripal_' . $details['base_table'], TRIPAL_ERROR,
  768. $details['base_table'] . ' Insert: Unable to insert property type_id %cvterm with value %value.',
  769. array('%cvterm' => $type_id, '%value' => $value));
  770. }
  771. }
  772. }
  773. }
  774. }