tripal_featuremap.form.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. <?php
  2. /**
  3. * When editing or creating a new node of type 'chado_featuremap' we need
  4. * a form. This function creates the form that will be used for this.
  5. *
  6. * @ingroup tripal_featuremap
  7. */
  8. function chado_featuremap_form($node) {
  9. tripal_core_ahah_init_form();
  10. $form = array();
  11. $featuremap = $node->featuremap;
  12. $featuremap_id = $featuremap->featuremap_id;
  13. $d_title = $form_state['values']['title'] ? $form_state['values']['title'] : $featuremap->name;
  14. $d_description = $form_state['values']['description'] ? $form_state['values']['description'] : $featuremap->description;
  15. $d_unittype_id = $form_state['values']['unittype_id'] ? $form_state['values']['unittype_id'] : $featuremap->unittype_id->cvterm_id;
  16. // on AHAH callbacks we want to keep a list of all the properties that have been removed
  17. // we'll store this info in a hidden field and retrieve it here
  18. $d_removed = $form_state['values']['removed'];
  19. // get the number of new fields that have been aded via AHAH callbacks
  20. $num_new = $form_state['values']['num_new'] ? $form_state['values']['num_new'] : 0;
  21. // initialze default properties array. This is where we store the property defaults
  22. $d_properties = array();
  23. // get the list of unit types
  24. $values = array(
  25. 'cv_id' => array(
  26. 'name' => 'featuremap_units',
  27. )
  28. );
  29. $columns = array('cvterm_id','name');
  30. $options = array('order_by' => array('name' => 'ASC'));
  31. $featuremap_units = tripal_core_chado_select('cvterm', $columns, $values, $options);
  32. $units = array();
  33. $units[''] = '';
  34. foreach($featuremap_units as $unit) {
  35. $units[$unit->cvterm_id] = $unit->name;
  36. }
  37. // get the featuremap properties
  38. $properties_select = array();
  39. $properties_select[] = 'Select a Property';
  40. $properties_list = array();
  41. $sql = "
  42. SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
  43. FROM {cvterm} CVT
  44. INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
  45. WHERE
  46. CV.name = 'featuremap_property' AND
  47. NOT CVT.is_obsolete = 1
  48. ORDER BY CVT.name ASC
  49. ";
  50. $prop_types = chado_query($sql);
  51. while ($prop = db_fetch_object($prop_types)) {
  52. $properties_select[$prop->cvterm_id] = $prop->name;
  53. $properties_list[$prop->cvterm_id] = $prop;
  54. }
  55. // keep track of the map id if we have. If we do have one then
  56. // this is an update as opposed to an insert.
  57. $form['featuremap_id'] = array(
  58. '#type' => 'hidden',
  59. '#value' => $featuremap_id,
  60. );
  61. $form['title']= array(
  62. '#type' => 'textfield',
  63. '#title' => t('Map Name'),
  64. '#description' => t('Please enter a name for this map'),
  65. '#required' => TRUE,
  66. '#default_value' => $d_title,
  67. '#maxlength' => 255
  68. );
  69. $form['description']= array(
  70. '#type' => 'textarea',
  71. '#title' => t('Map Description'),
  72. '#description' => t('A description of the map.'),
  73. '#required' => TRUE,
  74. '#default_value' => $d_description,
  75. );
  76. $form['unittype_id'] = array(
  77. '#title' => t('Map Units'),
  78. '#type' => t('select'),
  79. '#description' => t("Chose the units for this map"),
  80. '#required' => TRUE,
  81. '#default_value' => $d_unittype_id,
  82. '#options' => $units,
  83. );
  84. // add in the properties from the featuremapprop table
  85. $num_properties += chado_featuremap_node_form_add_featuremapprop_table_props($form, $form_state, $featuremap_id, $d_properties, $d_removed);
  86. // add in any new properties that have been added by the user through an AHAH callback
  87. $num_new = chado_featuremap_node_form_add_new_props($form, $form_state, $d_properties, $d_removed);
  88. // add an empty row of field to allow for addition of a new property
  89. chado_featuremap_node_form_add_new_empty_props($form, $properties_select);
  90. return $form;
  91. }
  92. /**
  93. * validates submission of form when adding or updating a map node
  94. *
  95. * @ingroup tripal_featuremap
  96. */
  97. function chado_featuremap_validate($node, &$form) {
  98. $name = trim($node->title);
  99. $featuremap_id = trim($node->featuremap_id);
  100. $unittype_id = trim($node->unittype_id);
  101. $description = trim($node->description);
  102. $num_properties = $node->num_properties;
  103. $num_new = $node->num_new;
  104. $featuremap = 0;
  105. // check to make sure the unique name on the map is unique
  106. // before we try to insert into chado. If this is an update then we will
  107. // have a featuremap_id, therefore we want to look for another map with this
  108. // name but with a different featuremap_id. If this is an insert, just look
  109. // for a case where the name already exists.
  110. if ($node->featuremap_id) {
  111. $sql = "
  112. SELECT * FROM {featuremap}
  113. WHERE name = '%s' AND NOT featuremap_id = %d
  114. ";
  115. $featuremap = db_fetch_object(chado_query($sql, $node->title, $node->featuremap_id));
  116. }
  117. else {
  118. $sql = "SELECT * FROM {featuremap} WHERE name = '%s'";
  119. $featuremap = db_fetch_object(chado_query($sql, $node->title));
  120. }
  121. if ($featuremap) {
  122. form_set_error('name', t('The unique map name already exists. Please choose another'));
  123. }
  124. }
  125. /*
  126. *
  127. */
  128. function chado_featuremap_node_form_add_new_empty_props(&$form, $properties_select) {
  129. // add one more blank set of property fields
  130. $form['properties']['new']["new_id"] = array(
  131. '#type' => 'select',
  132. '#options' => $properties_select,
  133. '#ahah' => array(
  134. 'path' => "tripal_featuremap/properties/description",
  135. 'wrapper' => 'tripal-featuremap-new_value-desc',
  136. 'event' => 'change',
  137. 'method' => 'replace',
  138. ),
  139. );
  140. $form['properties']['new']["new_value"] = array(
  141. '#type' => 'textarea',
  142. '#default_value' => '',
  143. '#cols' => 5,
  144. '#rows' => $rows,
  145. '#description' => '<div id="tripal-featuremap-new_value-desc"></div>'
  146. );
  147. $form['properties']['new']["add"] = array(
  148. '#type' => 'image_button',
  149. '#value' => t('Add'),
  150. '#src' => drupal_get_path('theme', 'tripal') . '/images/add.png',
  151. '#ahah' => array(
  152. 'path' => "tripal_featuremap/properties/add",
  153. 'wrapper' => 'tripal-featuremap-edit-properties-table',
  154. 'event' => 'click',
  155. 'method' => 'replace',
  156. ),
  157. '#attributes' => array('onClick' => 'return false;'),
  158. );
  159. }
  160. /*
  161. *
  162. */
  163. function chado_featuremap_node_form_add_new_props(&$form, $form_state, &$d_properties, &$d_removed) {
  164. // first, add in all of the new properties that were added through a previous AHAH callback
  165. $j = 0;
  166. $num_properties++;
  167. // we need to find the
  168. if ($form_state['values']) {
  169. foreach ($form_state['values'] as $element_name => $value) {
  170. if (preg_match('/new_value-(\d+)-(\d+)/', $element_name, $matches)) {
  171. $new_id = $matches[1];
  172. $rank = $matches[2];
  173. // skip any properties that the user requested to delete through a previous
  174. // AHAH callback or through the current AHAH callback
  175. if($d_removed["$new_id-$rank"]) {
  176. continue;
  177. }
  178. if($form_state['post']['remove-' . $new_id . '-' . $rank]) {
  179. $d_removed["$new_id-$rank"] = 1;
  180. continue;
  181. }
  182. // get this new_id information
  183. $cvterm = tripal_core_chado_select('cvterm', array('name', 'definition'), array('cvterm_id' => $new_id));
  184. // add it to the $d_properties array
  185. $d_properties[$new_id][$rank]['name'] = $cvterm->name;
  186. $d_properties[$new_id][$rank]['id'] = $new_id;
  187. $d_properties[$new_id][$rank]['value'] = $value;
  188. $d_properties[$new_id][$rank]['definition'] = $cvterm->definition;
  189. $num_properties++;
  190. // determine how many rows we need in the textarea
  191. $rows = 1;
  192. if (preg_match('/Abstract/', $cvterm[0]->name)) {
  193. $rows = 10;
  194. }
  195. if ($cvterm[0]->name == 'Authors') {
  196. $rows = 2;
  197. }
  198. // add the new fields
  199. $form['properties']['new'][$new_id][$rank]["new_id-$new_id-$rank"] = array(
  200. '#type' => 'item',
  201. '#value' => $cvterm[0]->name
  202. );
  203. $form['properties']['new'][$new_id][$rank]["new_value-$new_id-$rank"] = array(
  204. '#type' => 'textarea',
  205. '#default_value' => $value,
  206. '#cols' => 50,
  207. '#rows' => $rows,
  208. '#description' => $cvterm->definition,
  209. );
  210. $form['properties']['new'][$new_id][$rank]["remove-$new_id-$rank"] = array(
  211. '#type' => 'image_button',
  212. '#value' => t('Remove'),
  213. '#src' => drupal_get_path('theme', 'tripal') . '/images/minus.png',
  214. '#ahah' => array(
  215. 'path' => "tripal_featuremap/properties/minus/$new_id/$rank",
  216. 'wrapper' => 'tripal-featuremap-edit-properties-table',
  217. 'event' => 'click',
  218. 'method' => 'replace',
  219. ),
  220. '#attributes' => array('onClick' => 'return false;'),
  221. );
  222. }
  223. }
  224. }
  225. // second add in any new properties added during this callback
  226. if($form_state['post']['add']) {
  227. $new_id = $form_state['values']['new_id'];
  228. $new_value = $form_state['values']['new_value'];
  229. // get the rank by counting the number of entries
  230. $rank = count($d_properties[$new_id]);
  231. // get this new_id information
  232. $cvterm = tripal_core_chado_select('cvterm', array('name', 'definition'), array('cvterm_id' => $new_id));
  233. // add it to the $d_properties array
  234. $d_properties[$new_id][$rank]['name'] = $cvterm->name;
  235. $d_properties[$new_id][$rank]['id'] = $new_id;
  236. $d_properties[$new_id][$rank]['value'] = $value;
  237. $d_properties[$new_id][$rank]['definition'] = $cvterm->definition;
  238. $num_properties++;
  239. // determine how many rows we need in the textarea
  240. $rows = 1;
  241. if (preg_match('/Abstract/', $cvterm[0]->name)) {
  242. $rows = 10;
  243. }
  244. if ($cvterm[0]->name == 'Authors') {
  245. $rows = 2;
  246. }
  247. // add the new fields
  248. $form['properties']['new'][$new_id][$rank]["new_id-$new_id-$rank"] = array(
  249. '#type' => 'item',
  250. '#value' => $cvterm[0]->name
  251. );
  252. $form['properties']['new'][$new_id][$rank]["new_value-$new_id-$rank"] = array(
  253. '#type' => 'textarea',
  254. '#default_value' => $new_value,
  255. '#cols' => 50,
  256. '#rows' => $rows,
  257. '#description' => $cvterm->definition,
  258. );
  259. $form['properties']['new'][$new_id][$rank]["remove-$new_id-$rank"] = array(
  260. '#type' => 'image_button',
  261. '#value' => t('Remove'),
  262. '#src' => drupal_get_path('theme', 'tripal') . '/images/minus.png',
  263. '#ahah' => array(
  264. 'path' => "tripal_featuremap/properties/minus/$new_id/$rank",
  265. 'wrapper' => 'tripal-featuremap-edit-properties-table',
  266. 'event' => 'click',
  267. 'method' => 'replace',
  268. ),
  269. '#attributes' => array('onClick' => 'return false;'),
  270. );
  271. }
  272. return $num_properties;
  273. }
  274. /*
  275. *
  276. */
  277. function chado_featuremap_node_form_add_featuremapprop_table_props(&$form, $form_state, $featuremap_id, &$d_properties, &$d_removed) {
  278. // get the properties for this featuremap
  279. $num_properties = 0;
  280. if(!$featuremap_id) {
  281. return $num_properties;
  282. }
  283. $sql = "
  284. SELECT CVT.cvterm_id, CVT.name, CVT.definition, PP.value, PP.rank
  285. FROM {featuremapprop} PP
  286. INNER JOIN {cvterm} CVT on CVT.cvterm_id = PP.type_id
  287. WHERE PP.featuremap_id = %d
  288. ORDER BY CVT.name, PP.rank
  289. ";
  290. $featuremap_props = chado_query($sql, $featuremap_id);
  291. while ($prop = db_fetch_object($featuremap_props)) {
  292. $type_id = $prop->cvterm_id;
  293. $rank = count($d_properties[$type_id]);
  294. // skip any properties that the user requested to delete through a previous
  295. // AHAH callback or through the current AHAH callback
  296. if($d_removed["$type_id-$rank"]) {
  297. continue;
  298. }
  299. if($form_state['post']['remove-' . $type_id . '-' . $rank]) {
  300. $d_removed["$type_id-$rank"] = 1;
  301. continue;
  302. }
  303. $d_properties[$type_id][$rank]['name'] = $prop->name;
  304. $d_properties[$type_id][$rank]['id'] = $type_id;
  305. $d_properties[$type_id][$rank]['value'] = $prop->value;
  306. $d_properties[$type_id][$rank]['definition'] = $prop->definition;
  307. $num_properties++;
  308. $form['properties'][$type_id][$rank]["prop_id-$type_id-$rank"] = array(
  309. '#type' => 'item',
  310. '#value' => $prop->name,
  311. );
  312. $form['properties'][$type_id][$rank]["prop_value-$type_id-$rank"] = array(
  313. '#type' => 'textarea',
  314. '#default_value' => $prop->value,
  315. '#cols' => 50,
  316. '#rows' => $rows,
  317. '#description' => $prop->definition,
  318. );
  319. $form['properties'][$type_id][$rank]["remove-$type_id-$rank"] = array(
  320. '#type' => 'image_button',
  321. '#value' => t('Remove'),
  322. '#src' => drupal_get_path('theme', 'tripal') . '/images/minus.png',
  323. '#ahah' => array(
  324. 'path' => "tripal_featuremap/properties/minus/$type_id/$rank",
  325. 'wrapper' => 'tripal-featuremap-edit-properties-table',
  326. 'event' => 'click',
  327. 'method' => 'replace',
  328. ),
  329. '#attributes' => array('onClick' => 'return false;'),
  330. );
  331. }
  332. return $num_properties;
  333. }
  334. /*
  335. *
  336. */
  337. function tripal_featuremap_theme_node_form_properties($form) {
  338. $rows = array();
  339. if ($form['properties']) {
  340. // first add in the properties derived from the featuremapprop table
  341. // the array tree for these properties looks like this:
  342. // $form['properties'][$type_id][$rank]["prop_id-$type_id-$rank"]
  343. foreach ($form['properties'] as $type_id => $elements) {
  344. // there are other fields in the properties array so we only
  345. // want the numeric ones those are our type_id
  346. if (is_numeric($type_id)) {
  347. foreach ($elements as $rank => $element) {
  348. if (is_numeric($rank)) {
  349. $rows[] = array(
  350. drupal_render($element["prop_id-$type_id-$rank"]),
  351. drupal_render($element["prop_value-$type_id-$rank"]),
  352. drupal_render($element["remove-$type_id-$rank"]),
  353. );
  354. }
  355. }
  356. }
  357. }
  358. // second, add in any new properties added by the user through AHAH callbacks
  359. // the array tree for these properties looks like this:
  360. // $form['properties']['new'][$type_id][$rank]["new_id-$new_id-$rank"]
  361. foreach ($form['properties']['new'] as $type_id => $elements) {
  362. if (is_numeric($type_id)) {
  363. foreach ($elements as $rank => $element) {
  364. if (is_numeric($rank)) {
  365. $rows[] = array(
  366. drupal_render($element["new_id-$type_id-$rank"]),
  367. drupal_render($element["new_value-$type_id-$rank"]),
  368. drupal_render($element["remove-$type_id-$rank"]),
  369. );
  370. }
  371. }
  372. }
  373. }
  374. // finally add in a set of blank field for adding a new property
  375. $rows[] = array(
  376. drupal_render($form['properties']['new']['new_id']),
  377. drupal_render($form['properties']['new']['new_value']),
  378. drupal_render($form['properties']['new']['add']),
  379. );
  380. }
  381. $headers = array('Property Type','Value', '');
  382. return theme('table', $headers, $rows, array('id'=> "tripal-featuremap-edit-properties-table"));
  383. }
  384. /*
  385. *
  386. */
  387. function tripal_featuremap_property_add() {
  388. $status = TRUE;
  389. // prepare and render the form
  390. $form = tripal_core_ahah_prepare_form();
  391. // we only want to return the properties as that's all we'll replace with this AHAh callback
  392. $data = tripal_featuremap_theme_node_form_properties($form);
  393. // bind javascript events to the new objects that will be returned
  394. // so that AHAH enabled elements will work.
  395. $settings = tripal_core_ahah_bind_events();
  396. // return the updated JSON
  397. drupal_json(
  398. array(
  399. 'status' => $status,
  400. 'data' => $data,
  401. 'settings' => $settings,
  402. )
  403. );
  404. }
  405. /*
  406. *
  407. */
  408. function tripal_featuremap_property_delete() {
  409. $status = TRUE;
  410. // prepare and render the form
  411. $form = tripal_core_ahah_prepare_form();
  412. // we only want to return the properties as that's all we'll replace with this AHAh callback
  413. $data = tripal_featuremap_theme_node_form_properties($form);
  414. // bind javascript events to the new objects that will be returned
  415. // so that AHAH enabled elements will work.
  416. $settings = tripal_core_ahah_bind_events();
  417. // return the updated JSON
  418. drupal_json(
  419. array(
  420. 'status' => $status,
  421. 'data' => $data,
  422. 'settings' => $settings,
  423. )
  424. );
  425. }
  426. /*
  427. *
  428. */
  429. function tripal_featuremap_property_get_description() {
  430. $new_id = $_POST['new_id'];
  431. $values = array('cvterm_id' => $new_id);
  432. $cvterm = tripal_core_chado_select('cvterm', array('definition'), $values);
  433. $description = '&nbsp;';
  434. if ($cvterm[0]->definition) {
  435. $description = $cvterm[0]->definition;
  436. }
  437. drupal_json(
  438. array(
  439. 'status' => TRUE,
  440. 'data' => '<div id="tripal-featuremap-new_value-desc">' . $description . '</div>',
  441. )
  442. );
  443. }
  444. /*
  445. *
  446. */
  447. function theme_chado_featuremap_node_form($form) {
  448. $properties_table = tripal_featuremap_theme_node_form_properties($form);
  449. $markup = drupal_render($form['featuremap_id']);
  450. $markup .= drupal_render($form['title']);
  451. $markup .= drupal_render($form['unittype_id']);
  452. $markup .= drupal_render($form['description']);
  453. $markup .= "<b>Include Additional Details</b><br>You may add additional
  454. properties to this map by scrolling to the bottom of this table, selecting
  455. a property type from the dropdown and adding text. You may add as many
  456. properties as desired by clicking the plus button on the right. To
  457. remove a property, click the minus button. If a property is not available
  458. you may add it by " . l('adding the term', 'admin/tripal/tripal_cv/cvterm/add') . "
  459. to the <b>featuremap_property</b> vocabulary within the <b>tripal</b> database.";
  460. $markup .= $properties_table;
  461. $markup .= drupal_render($form['is_obsolete']);
  462. $form['properties'] = array(
  463. '#type' => 'markup',
  464. '#value' => $markup,
  465. );
  466. return drupal_render($form);
  467. }