tripal_stock-properties.inc 14 KB

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