tripal_stock-properties.inc 14 KB

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