tripal_stock-properties.inc 15 KB

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