tripal_contact.form.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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. if (preg_match('/Abstract/', $cvterm[0]->name)) {
  233. $rows = 10;
  234. }
  235. if ($cvterm[0]->name == 'Authors') {
  236. $rows = 2;
  237. }
  238. // add the new fields
  239. $form['properties']['new'][$new_id][$rank]["new_id-$new_id-$rank"] = array(
  240. '#type' => 'item',
  241. '#value' => $cvterm[0]->name
  242. );
  243. $form['properties']['new'][$new_id][$rank]["new_value-$new_id-$rank"] = array(
  244. '#type' => 'textarea',
  245. '#default_value' => $value,
  246. '#cols' => 50,
  247. '#rows' => $rows,
  248. '#description' => $cvterm->definition,
  249. );
  250. $form['properties']['new'][$new_id][$rank]["remove-$new_id-$rank"] = array(
  251. '#type' => 'image_button',
  252. '#value' => t('Remove'),
  253. '#src' => drupal_get_path('theme', 'tripal') . '/images/minus.png',
  254. '#ahah' => array(
  255. 'path' => "tripal_contact/properties/minus/$new_id/$rank",
  256. 'wrapper' => 'tripal-contact-edit-properties-table',
  257. 'event' => 'click',
  258. 'method' => 'replace',
  259. ),
  260. '#attributes' => array('onClick' => 'return false;'),
  261. );
  262. }
  263. }
  264. }
  265. // second add in any new properties added during this callback
  266. if($form_state['post']['add']) {
  267. $new_id = $form_state['values']['new_id'];
  268. $new_value = $form_state['values']['new_value'];
  269. // get the rank by counting the number of entries
  270. $rank = count($d_properties[$new_id]);
  271. // get this new_id information
  272. $cvterm = tripal_core_chado_select('cvterm', array('name', 'definition'), array('cvterm_id' => $new_id));
  273. // add it to the $d_properties array
  274. $d_properties[$new_id][$rank]['name'] = $cvterm->name;
  275. $d_properties[$new_id][$rank]['id'] = $new_id;
  276. $d_properties[$new_id][$rank]['value'] = $value;
  277. $d_properties[$new_id][$rank]['definition'] = $cvterm->definition;
  278. $num_properties++;
  279. // determine how many rows we need in the textarea
  280. $rows = 1;
  281. if (preg_match('/Abstract/', $cvterm[0]->name)) {
  282. $rows = 10;
  283. }
  284. if ($cvterm[0]->name == 'Authors') {
  285. $rows = 2;
  286. }
  287. // add the new fields
  288. $form['properties']['new'][$new_id][$rank]["new_id-$new_id-$rank"] = array(
  289. '#type' => 'item',
  290. '#value' => $cvterm[0]->name
  291. );
  292. $form['properties']['new'][$new_id][$rank]["new_value-$new_id-$rank"] = array(
  293. '#type' => 'textarea',
  294. '#default_value' => $new_value,
  295. '#cols' => 50,
  296. '#rows' => $rows,
  297. '#description' => $cvterm->definition,
  298. );
  299. $form['properties']['new'][$new_id][$rank]["remove-$new_id-$rank"] = array(
  300. '#type' => 'image_button',
  301. '#value' => t('Remove'),
  302. '#src' => drupal_get_path('theme', 'tripal') . '/images/minus.png',
  303. '#ahah' => array(
  304. 'path' => "tripal_contact/properties/minus/$new_id/$rank",
  305. 'wrapper' => 'tripal-contact-edit-properties-table',
  306. 'event' => 'click',
  307. 'method' => 'replace',
  308. ),
  309. '#attributes' => array('onClick' => 'return false;'),
  310. );
  311. }
  312. return $num_properties;
  313. }
  314. /*
  315. *
  316. */
  317. function chado_contact_node_form_add_contactprop_table_props(&$form, $form_state, $contact_id, &$d_properties, &$d_removed) {
  318. // get the properties for this contact
  319. $num_properties = 0;
  320. if(!$contact_id) {
  321. return $num_properties;
  322. }
  323. $sql = "
  324. SELECT CVT.cvterm_id, CVT.name, CVT.definition, PP.value, PP.rank
  325. FROM {contactprop} PP
  326. INNER JOIN {cvterm} CVT on CVT.cvterm_id = PP.type_id
  327. WHERE PP.contact_id = %d
  328. ORDER BY CVT.name, PP.rank
  329. ";
  330. $contact_props = chado_query($sql, $contact_id);
  331. while ($prop = db_fetch_object($contact_props)) {
  332. $type_id = $prop->cvterm_id;
  333. $rank = count($d_properties[$type_id]);
  334. // skip the description as we have a separate form element for that
  335. if($prop->name == "contact_description") {
  336. continue;
  337. }
  338. // skip any properties that the user requested to delete through a previous
  339. // AHAH callback or through the current AHAH callback
  340. if($d_removed["$type_id-$rank"]) {
  341. continue;
  342. }
  343. if($form_state['post']['remove-' . $type_id . '-' . $rank]) {
  344. $d_removed["$type_id-$rank"] = 1;
  345. continue;
  346. }
  347. $d_properties[$type_id][$rank]['name'] = $prop->name;
  348. $d_properties[$type_id][$rank]['id'] = $type_id;
  349. $d_properties[$type_id][$rank]['value'] = $prop->value;
  350. $d_properties[$type_id][$rank]['definition'] = $prop->definition;
  351. $num_properties++;
  352. $form['properties'][$type_id][$rank]["prop_id-$type_id-$rank"] = array(
  353. '#type' => 'item',
  354. '#value' => $prop->name,
  355. );
  356. $form['properties'][$type_id][$rank]["prop_value-$type_id-$rank"] = array(
  357. '#type' => 'textarea',
  358. '#default_value' => $prop->value,
  359. '#cols' => 50,
  360. '#rows' => $rows,
  361. '#description' => $prop->definition,
  362. );
  363. $form['properties'][$type_id][$rank]["remove-$type_id-$rank"] = array(
  364. '#type' => 'image_button',
  365. '#value' => t('Remove'),
  366. '#src' => drupal_get_path('theme', 'tripal') . '/images/minus.png',
  367. '#ahah' => array(
  368. 'path' => "tripal_contact/properties/minus/$type_id/$rank",
  369. 'wrapper' => 'tripal-contact-edit-properties-table',
  370. 'event' => 'click',
  371. 'method' => 'replace',
  372. ),
  373. '#attributes' => array('onClick' => 'return false;'),
  374. );
  375. }
  376. return $num_properties;
  377. }
  378. /*
  379. *
  380. */
  381. function tripal_contact_theme_node_form_properties($form) {
  382. $rows = array();
  383. if ($form['properties']) {
  384. // first add in the properties derived from the contactprop table
  385. // the array tree for these properties looks like this:
  386. // $form['properties'][$type_id][$rank]["prop_id-$type_id-$rank"]
  387. foreach ($form['properties'] as $type_id => $elements) {
  388. // there are other fields in the properties array so we only
  389. // want the numeric ones those are our type_id
  390. if (is_numeric($type_id)) {
  391. foreach ($elements as $rank => $element) {
  392. if (is_numeric($rank)) {
  393. $rows[] = array(
  394. drupal_render($element["prop_id-$type_id-$rank"]),
  395. drupal_render($element["prop_value-$type_id-$rank"]),
  396. drupal_render($element["remove-$type_id-$rank"]),
  397. );
  398. }
  399. }
  400. }
  401. }
  402. // second, add in any new properties added by the user through AHAH callbacks
  403. // the array tree for these properties looks like this:
  404. // $form['properties']['new'][$type_id][$rank]["new_id-$new_id-$rank"]
  405. foreach ($form['properties']['new'] as $type_id => $elements) {
  406. if (is_numeric($type_id)) {
  407. foreach ($elements as $rank => $element) {
  408. if (is_numeric($rank)) {
  409. $rows[] = array(
  410. drupal_render($element["new_id-$type_id-$rank"]),
  411. drupal_render($element["new_value-$type_id-$rank"]),
  412. drupal_render($element["remove-$type_id-$rank"]),
  413. );
  414. }
  415. }
  416. }
  417. }
  418. // finally add in a set of blank field for adding a new property
  419. $rows[] = array(
  420. drupal_render($form['properties']['new']['new_id']),
  421. drupal_render($form['properties']['new']['new_value']),
  422. drupal_render($form['properties']['new']['add']),
  423. );
  424. }
  425. $headers = array('Property Type','Value', '');
  426. return theme('table', $headers, $rows, array('id'=> "tripal-contact-edit-properties-table"));
  427. }
  428. /*
  429. *
  430. */
  431. function tripal_contact_property_add() {
  432. $status = TRUE;
  433. // prepare and render the form
  434. $form = tripal_core_ahah_prepare_form();
  435. // we only want to return the properties as that's all we'll replace with this AHAh callback
  436. $data = tripal_contact_theme_node_form_properties($form);
  437. // bind javascript events to the new objects that will be returned
  438. // so that AHAH enabled elements will work.
  439. $settings = tripal_core_ahah_bind_events();
  440. // return the updated JSON
  441. drupal_json(
  442. array(
  443. 'status' => $status,
  444. 'data' => $data,
  445. 'settings' => $settings,
  446. )
  447. );
  448. }
  449. /*
  450. *
  451. */
  452. function tripal_contact_property_delete() {
  453. $status = TRUE;
  454. // prepare and render the form
  455. $form = tripal_core_ahah_prepare_form();
  456. // we only want to return the properties as that's all we'll replace with this AHAh callback
  457. $data = tripal_contact_theme_node_form_properties($form);
  458. // bind javascript events to the new objects that will be returned
  459. // so that AHAH enabled elements will work.
  460. $settings = tripal_core_ahah_bind_events();
  461. // return the updated JSON
  462. drupal_json(
  463. array(
  464. 'status' => $status,
  465. 'data' => $data,
  466. 'settings' => $settings,
  467. )
  468. );
  469. }
  470. /*
  471. *
  472. */
  473. function tripal_contact_property_get_description() {
  474. $new_id = $_POST['new_id'];
  475. $values = array('cvterm_id' => $new_id);
  476. $cvterm = tripal_core_chado_select('cvterm', array('definition'), $values);
  477. $description = '&nbsp;';
  478. if ($cvterm[0]->definition) {
  479. $description = $cvterm[0]->definition;
  480. }
  481. drupal_json(
  482. array(
  483. 'status' => TRUE,
  484. 'data' => '<div id="tripal-contact-new_value-desc">' . $description . '</div>',
  485. )
  486. );
  487. }
  488. /*
  489. *
  490. */
  491. function theme_chado_contact_node_form($form) {
  492. $properties_table = tripal_contact_theme_node_form_properties($form);
  493. $markup = drupal_render($form['contact_id']);
  494. $markup .= drupal_render($form['title']);
  495. $markup .= drupal_render($form['type_id']);
  496. $markup .= drupal_render($form['description']);
  497. $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";
  498. $markup .= $properties_table;
  499. $form['properties'] = array(
  500. '#type' => 'markup',
  501. '#value' => $markup,
  502. );
  503. return drupal_render($form);
  504. }