tripal_contact.form.inc 19 KB

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