tripal_stock-properties.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. /**
  7. *
  8. *
  9. * @ingroup tripal_stock
  10. */
  11. function tripal_stock_add_ALL_property_page($node) {
  12. $output = '';
  13. $output .= tripal_stock_add_chado_properties_progress('properties') . '<br />';
  14. $output .= '<b>All Properties should strictly pertain to THE CURRENT Individual</b><br />';
  15. $output .= '<br />';
  16. $output .= theme('tripal_stock_properties', $node);
  17. $output .= '<br /><br />';
  18. $output .= drupal_get_form('tripal_stock_add_ONE_property_form', $node);
  19. $output .= '<br />';
  20. $output .= drupal_get_form('tripal_stock_add_chado_properties_navigate', 'properties', $node->nid);
  21. return $output;
  22. }
  23. /**
  24. *
  25. *
  26. * @ingroup tripal_stock
  27. */
  28. function tripal_stock_add_ONE_property_form($form_state, $node) {
  29. $form = array();
  30. $stock_id = $node->stock->stock_id;
  31. $form['add_properties'] = array(
  32. '#type' => 'fieldset',
  33. '#title' => t('Add Property') . '<span class="form-optional" title="This field is optional"> (optional)</span>',
  34. );
  35. $form['prop_nid'] = array(
  36. '#type' => 'hidden',
  37. '#value' => $node->nid
  38. );
  39. $tmp_obj = tripal_cv_get_cvterm_by_name('synonym', variable_get('chado_stock_prop_types_cv', 'null'));
  40. $synonym_id = $tmp_obj->cvterm_id;
  41. $prop_type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_prop_types_cv', 'null') );
  42. $prop_type_options[0] = 'Select a Type';
  43. ksort($prop_type_options);
  44. $form['add_properties']['prop_type_id'] = array(
  45. '#type' => 'select',
  46. '#title' => t('Type of Property'),
  47. '#options' => $prop_type_options,
  48. );
  49. $form['add_properties']['prop_value'] = array(
  50. '#type' => 'textfield',
  51. '#title' => t('Value') . '<span class="form-optional" title="This field is optional">+</span>',
  52. );
  53. $form['add_properties']['preferred_synonym'] = array(
  54. '#type' => 'checkbox',
  55. '#title' => t('Preferred Synonym (only applicable if type is synonym)'),
  56. );
  57. $form['add_properties']['prop_stock_id'] = array(
  58. '#type' => 'value',
  59. '#value' => $stock_id,
  60. '#required' => TRUE
  61. );
  62. $form['add_properties']['submit-add'] = array(
  63. '#type' => 'submit',
  64. '#value' => t('Add Property')
  65. );
  66. return $form;
  67. }
  68. /**
  69. *
  70. *
  71. * @ingroup tripal_stock
  72. */
  73. function tripal_stock_add_ONE_property_form_validate($form, &$form_state) {
  74. // Only Require if Adding Property
  75. if ($form_state['clicked_button']['#value'] == t('Add Property') ) {
  76. // Check that there is a stock
  77. if ( $form_state['values']['prop_stock_id'] <= 0 ) {
  78. form_set_error('prop_stock_id', 'There is no associated stock.');
  79. }
  80. // Check that Selected a type
  81. if ( $form_state['values']['prop_type_id'] == 0) {
  82. form_set_error('prop_type_id', 'Please select a type of property.');
  83. }
  84. else {
  85. // Check that type is in chado
  86. $previous_db = tripal_db_set_active('chado');
  87. $num_rows = db_fetch_object(db_query("SELECT count(*) as count FROM {cvterm} WHERE cvterm_id=%d", $form_state['values']['prop_type_id']));
  88. tripal_db_set_active($previous_db);
  89. if ( $num_rows->count != 1) {
  90. form_set_error('prop_type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)");
  91. } // end of if more or less than 1 row
  92. } // if no prop type
  93. // only check preferred synonym if type is synonym
  94. if ($form_state['values']['preferred_synonym'] == 1) {
  95. $tmp_obj = tripal_cv_get_cvterm_by_name('synonym', variable_get('chado_stock_prop_types_cv', 'null'));
  96. if ($form_state['values']['prop_type_id'] != $tmp_obj->cvterm_id) {
  97. form_set_error('preferred_synonym', 'Preferred Synonym Checkbox Only Applicable if Type of Property is Synonym');
  98. }
  99. }
  100. } // if add Property
  101. }
  102. /**
  103. *
  104. *
  105. * @ingroup tripal_stock
  106. */
  107. function tripal_stock_add_ONE_property_form_submit($form, &$form_state) {
  108. // if there is a property add it (only won't be a property if clicked next step w/ no property)
  109. if ($form_state['values']['prop_type_id'] != 0) {
  110. //determine the rank for this property
  111. $max_rank = get_max_chado_rank('stockprop',
  112. array('stock_id' => array('type' => 'INT', 'value' => $form_state['values']['prop_stock_id']),
  113. 'type_id' => array('type' => 'INT', 'value' => $form_state['values']['prop_type_id']) ));
  114. if ($max_rank == -1) {
  115. $rank = 0;
  116. }
  117. else { $rank = $max_rank+1; }
  118. $previous_db = tripal_db_set_active('chado');
  119. db_query(
  120. "INSERT INTO {stockprop} (stock_id, type_id, value, rank) VALUES (%d, %d, '%s', %d)",
  121. $form_state['values']['prop_stock_id'],
  122. $form_state['values']['prop_type_id'],
  123. $form_state['values']['prop_value'],
  124. $rank
  125. );
  126. tripal_db_set_active($previous_db);
  127. drupal_set_message(t("Successfully Added Property"));
  128. // Set Preferred Synonym
  129. if ($form_state['values']['preferred_synonym'] == 1) {
  130. //use update node form so that both title and name get set
  131. $node = node_load($form_state['values']['prop_nid']);
  132. $node->title = $form_state['values']['prop_value'];
  133. $node_form_state = array(
  134. 'values' => array(
  135. 'title' => $form_state['values']['prop_value'],
  136. 'op' => 'Save'
  137. )
  138. );
  139. module_load_include('inc', 'node', 'node.pages');
  140. drupal_execute('chado_stock_node_form', $node_form_state, $node);
  141. }
  142. } //end of if property to add
  143. }
  144. /**
  145. *
  146. *
  147. * @ingroup tripal_stock
  148. */
  149. function tripal_stock_edit_ALL_properties_page($node) {
  150. $output = '';
  151. $output .= drupal_get_form('tripal_stock_edit_ALL_properties_form', $node);
  152. $output .= '<br />';
  153. $output .= drupal_get_form('tripal_stock_add_ONE_property_form', $node);
  154. $output .= '<br />';
  155. $output .= drupal_get_form('tripal_stock_back_to_stock_button', $node->nid);
  156. return $output;
  157. }
  158. /**
  159. * Implements Hook_form(): Handles adding of Properties & Synonyms to Stocks
  160. *
  161. * @ingroup tripal_stock
  162. */
  163. function tripal_stock_edit_ALL_properties_form($form_state, $node) {
  164. $form = array();
  165. // Add properties and synonyms
  166. $node = tripal_core_expand_chado_vars($node, 'table', 'stockprop');
  167. $form['nid'] = array(
  168. '#type' => 'hidden',
  169. '#value' => $node->nid
  170. );
  171. $i=0;
  172. if (!$node->stock->stockprop) {
  173. $node->stock->stockprop = array();
  174. }
  175. elseif (!is_array($node->stock->stockprop)) {
  176. $node->stock->stockprop = array($node->stock->stockprop);
  177. }
  178. if (sizeof($node->stock->stockprop) != 0) {
  179. foreach ($node->stock->stockprop as $property) {
  180. $i++;
  181. $form["num-$i"] = array(
  182. '#type' => 'item',
  183. '#value' => $i . '.'
  184. );
  185. $form["id-$i"] = array(
  186. '#type' => 'hidden',
  187. '#value' => $property->stockprop_id
  188. );
  189. $prop_type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_prop_types_cv', 'null') );
  190. ksort($prop_type_options);
  191. $form["type-$i"] = array(
  192. '#type' => 'select',
  193. //'#title' => t('Type of Property'),
  194. '#options' => $prop_type_options,
  195. '#default_value' => $property->type_id->cvterm_id
  196. );
  197. $form["value-$i"] = array(
  198. '#type' => 'textfield',
  199. //'#title' => t('Value'),
  200. '#default_value' => $property->value
  201. );
  202. $form["submit-$i"] = array(
  203. '#type' => 'submit',
  204. '#value' => t("Delete #$i")
  205. );
  206. }} //end of foreach property
  207. $form['num_properties'] = array(
  208. '#type' => 'hidden',
  209. '#value' => $i
  210. );
  211. $form["submit-edits"] = array(
  212. '#type' => 'submit',
  213. '#value' => t('Update Properties')
  214. );
  215. return $form;
  216. }
  217. /**
  218. *
  219. *
  220. * @ingroup tripal_stock
  221. */
  222. function tripal_stock_edit_ALL_properties_form_submit($form, &$form_state) {
  223. if ($form_state['clicked_button']['#value'] == t('Update Properties') ) {
  224. //Update all
  225. for ($i=1; $i<=$form_state['values']['num_properties']; $i++) {
  226. tripal_stock_update_property($form_state['values']["id-$i"], $form_state['values']["type-$i"], $form_state['values']["value-$i"], $form_state['values']["preferred-$i"], $form_state['values']["nid"]);
  227. }
  228. drupal_set_message(t("Updated all Properties"));
  229. drupal_goto('node/' . $form_state['values']['nid']);
  230. }
  231. elseif ( preg_match('/Delete #(\d+)/', $form_state['clicked_button']['#value'], $matches) ) {
  232. $i = $matches[1];
  233. tripal_stock_delete_property($form_state['values']["id-$i"], $form_state['values']["type-$i"], $form_state['values']["value-$i"]);
  234. drupal_set_message(t("Deleted Property"));
  235. }
  236. else {
  237. drupal_set_message(t("Unrecognized Button Pressed"), 'error');
  238. }
  239. }
  240. /**
  241. *
  242. *
  243. * @ingroup tripal_stock
  244. */
  245. function tripal_stock_update_property($stockprop_id, $cvterm_id, $value, $preferred, $nid) {
  246. $previous_db = tripal_db_set_active('chado');
  247. $old_obj = db_fetch_object(db_query("SELECT * FROM {stockprop} WHERE stockprop_id=%d", $stockprop_id));
  248. tripal_db_set_active($previous_db);
  249. // if they changed the type need to check rank
  250. // (if there is another property of the same type then rank needs to be increased to prevent collisions)
  251. if ($cvterm_id == $old_obj->type_id) {
  252. $previous_db = tripal_db_set_active('chado');
  253. db_query(
  254. "UPDATE {stockprop} SET type_id=%d, value='%s' WHERE stockprop_id=%d",
  255. $cvterm_id,
  256. $value,
  257. $stockprop_id
  258. );
  259. tripal_db_set_active($previous_db);
  260. }
  261. else {
  262. //determine the rank for this property
  263. $max_rank = get_max_chado_rank('stockprop',
  264. array('stock_id' => array('type' => 'INT', 'value' => $old_obj->stock_id),
  265. 'type_id' => array('type' => 'INT', 'value' => $cvterm_id ) ));
  266. if ($max_rank == -1) {
  267. $rank = 0;
  268. }
  269. else { $rank = $max_rank+1; }
  270. $previous_db = tripal_db_set_active('chado');
  271. db_query(
  272. "UPDATE {stockprop} SET type_id=%d, value='%s', rank=%d WHERE stockprop_id=%d",
  273. $cvterm_id,
  274. $value,
  275. $rank,
  276. $stockprop_id
  277. );
  278. tripal_db_set_active($previous_db);
  279. }
  280. // Set Preferred Synonym
  281. //use update node form so that both title and name get set
  282. if ($preferred) {
  283. $node = node_load($nid);
  284. $node->title = $value;
  285. $node_form_state = array(
  286. 'values' => array(
  287. 'title' => $value,
  288. 'op' => 'Save'
  289. )
  290. );
  291. module_load_include('inc', 'node', 'node.pages');
  292. drupal_execute('chado_stock_node_form', $node_form_state, $node);
  293. }
  294. }
  295. /**
  296. *
  297. *
  298. * @ingroup tripal_stock
  299. */
  300. function tripal_stock_delete_property($stockprop_id) {
  301. $previous_db = tripal_db_set_active('chado');
  302. db_query(
  303. "DELETE FROM {stockprop} WHERE stockprop_id=%d",
  304. $stockprop_id
  305. );
  306. tripal_db_set_active($previous_db);
  307. }
  308. /**
  309. *
  310. *
  311. * @ingroup tripal_stock
  312. */
  313. function theme_tripal_stock_edit_ALL_properties_form($form) {
  314. $output = '';
  315. $output .= '<br /><fieldset>';
  316. $output .= '<legend>Edit Already Existing Properties<span class="form-optional" title="This field is optional">(optional)</span></legend>';
  317. $output .= '<p>Below is a list of already existing properties for this stock, one property per line. The type refers to the type of '
  318. .'property and the value is the value for that property. For example, if this stock has a seed coat colour of green then '
  319. .'the property type=sead coat colour and the value=green. When the type of property is synonym, there is an extra checkbox '
  320. .'allowing you to specify which is the <b>Preferred Synonym</b>. This will change the current name of the stock.</p>';
  321. $output .= '<table>';
  322. $output .= '<tr><th>#</th><th>Type</th><th>Value</th><th></th></tr>';
  323. for ($i=1; $i<=$form['num_properties']['#value']; $i++) {
  324. $output .= '<tr><td>' . drupal_render($form["num-$i"]) . '</td><td>' . drupal_render($form["type-$i"]) . '</td><td>' . drupal_render($form["value-$i"]) . drupal_render($form["preferred-$i"]) . '</td><td>' . drupal_render($form["submit-$i"]) . '</td></tr>';
  325. }
  326. $output .= '</table><br />';
  327. $output .= drupal_render($form);
  328. $output .= '</fieldset>';
  329. return $output;
  330. }
  331. /**
  332. *
  333. *
  334. * @ingroup tripal_stock
  335. */
  336. function tripal_stock_list_properties_for_node($properties, $synonyms) {
  337. if (!empty($properties) OR !empty($synonyms) ) {
  338. $output = '<table>';
  339. $output .= '<tr><th>Type</th><th>Value</th></tr>';
  340. if (!empty($synonyms) ) {
  341. foreach ($synonyms as $s) {
  342. $output .= '<tr><td>synonym</td><td>' . $s . '</td></tr>';
  343. }
  344. }
  345. if (!empty($properties) ) {
  346. foreach ($properties as $p) {
  347. $output .= '<tr><td>' . $p->type . '</td><td>' . $p->value . '</td></tr>';
  348. } // end of foreach property
  349. }
  350. $output .= '</table>';
  351. }
  352. else {
  353. $output = 'No Properties Added to the Current Stock';
  354. }
  355. return $output;
  356. }