tripal_stock-properties.inc 15 KB

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