pub_form.inc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. <?php
  2. /**
  3. * This is the chado_pub node form callback. The arguments
  4. * are out of order from a typical form because it's a defined callback
  5. */
  6. function chado_pub_form($node, $form_state) {
  7. tripal_core_ahah_init_form();
  8. $form = array();
  9. $pub = $node->pub;
  10. $pub_id = $pub->pub_id;
  11. $d_title = $form_state['values']['pubtitle'] ? $form_state['values']['pubtitle'] : $pub->title;
  12. $d_uniquename = $form_state['values']['uniquename'] ? $form_state['values']['uniquename'] : $pub->uniquename;
  13. $d_type_id = $form_state['values']['type_id'] ? $form_state['values']['type_id'] : $pub->type_id->cvterm_id;
  14. $d_volume = $form_state['values']['volume'] ? $form_state['values']['volume'] : $pub->volume;
  15. $d_volumetitle = $form_state['values']['volumetitle'] ? $form_state['values']['volumetitle'] : $pub->volumetitle;
  16. $d_series_name = $form_state['values']['series_name'] ? $form_state['values']['series_name'] : $pub->series_name;
  17. $d_issue = $form_state['values']['issue'] ? $form_state['values']['issue'] : $pub->issue;
  18. $d_pyear = $form_state['values']['pyear'] ? $form_state['values']['pyear'] : $pub->pyear;
  19. $d_pages = $form_state['values']['pages'] ? $form_state['values']['pages'] : $pub->pages;
  20. $d_miniref = $form_state['values']['miniref'] ? $form_state['values']['miniref'] : $pub->miniref;
  21. $d_publisher = $form_state['values']['publisher'] ? $form_state['values']['publisher'] : $pub->publisher;
  22. $d_pubplace = $form_state['values']['pubplace'] ? $form_state['values']['pubplace'] : $pub->pubplace;
  23. $d_is_obsolete = $form_state['values']['is_obsolete'] ? $form_state['values']['is_obsolete'] : $pub->is_obsolete;
  24. // if the obsolete value is set by the database then it is in the form of
  25. // 't' or 'f', we need to convert to 1 or 0
  26. $d_is_obsolete = $d_is_obsolete == 't' ? 1 : $d_is_obsolete;
  27. $d_is_obsolete = $d_is_obsolete == 'f' ? 0 : $d_is_obsolete;
  28. // on AHAH callbacks we want to keep a list of all the properties that have been removed
  29. // we'll store this info in a hidden field and retrieve it here
  30. $d_removed = $form_state['values']['removed'];
  31. // get the number of new fields that have been aded via AHAH callbacks
  32. $num_new = $form_state['values']['num_new'] ? $form_state['values']['num_new'] : 0;
  33. // initialze default properties array. This is where we store the property defaults
  34. $d_properties = array();
  35. // get the list of publication types. In the Tripal publication
  36. // ontologies these are all grouped under the term 'Publication Type'
  37. // we want the default to be 'Journal Article'
  38. $sql = "
  39. SELECT CVTS.cvterm_id, CVTS.name
  40. FROM {cvtermpath} CVTP
  41. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  42. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  43. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  44. WHERE CV.name = 'tripal_pub' and CVTO.name = 'Publication Type' and
  45. NOT CVTS.is_obsolete = 1
  46. ORDER BY CVTS.name ASC
  47. ";
  48. $results = chado_query($sql);
  49. $pub_types = array();
  50. while ($pub_type = db_fetch_object($results)) {
  51. $pub_types[$pub_type->cvterm_id] = $pub_type->name;
  52. // if we don't have a default type then set the default to be 'Journal Article'
  53. if (strcmp($pub_type->name,"Journal Article") == 0 and !$d_type_id) {
  54. $d_type_id = $pub_type->cvterm_id;
  55. }
  56. }
  57. // get publication properties list
  58. $properties_select = array();
  59. $properties_select[] = 'Select a Property';
  60. $properties_list = array();
  61. $sql = "
  62. SELECT DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
  63. FROM {cvtermpath} CVTP
  64. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  65. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  66. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  67. WHERE CV.name = 'tripal_pub' and
  68. (CVTO.name = 'Publication Details' or CVTS.name = 'Publication Type') and
  69. NOT CVTS.is_obsolete = 1
  70. ORDER BY CVTS.name ASC
  71. ";
  72. $prop_types = chado_query($sql);
  73. while ($prop = db_fetch_object($prop_types)) {
  74. // the 'Citation' term is special because it serves
  75. // both as a property and as the uniquename for the publiation table
  76. if ($prop->name != "Citation") {
  77. $properties_select[$prop->cvterm_id] = $prop->name;
  78. }
  79. $properties_list[$prop->cvterm_id] = $prop;
  80. }
  81. $form['pub_id'] = array(
  82. '#type' => 'hidden',
  83. '#value' => $pub_id,
  84. );
  85. // a drupal title can only be 255 characters, but the Chado title can be much longer.
  86. // we use the publication title as the drupal title, but we'll need to truncate it.
  87. $form['title'] = array(
  88. '#type' => 'hidden',
  89. '#value' => substr($d_title, 0, 255),
  90. );
  91. $form['pubtitle'] = array(
  92. '#type' => 'textarea',
  93. '#title' => t('Publication Title'),
  94. '#default_value' => $d_title,
  95. '#required' => TRUE,
  96. );
  97. $form['type_id'] = array(
  98. '#type' => 'select',
  99. '#title' => t('Publication Type'),
  100. '#options' => $pub_types,
  101. '#required' => TRUE,
  102. '#default_value' => $d_type_id,
  103. );
  104. $form['pyear'] = array(
  105. '#type' => 'textfield',
  106. '#title' => t('Publication Year'),
  107. '#default_value' => $d_pyear,
  108. '#required' => TRUE,
  109. '#size' => 5,
  110. );
  111. $form['uniquename'] = array(
  112. '#type' => 'textarea',
  113. '#title' => t('Citation'),
  114. '#default_value' => $d_uniquename,
  115. '#description' => t('All publications must have a unique citation. Please enter the full citation for this publication.
  116. For PubMed style citations list
  117. the last name of the author followed by initials. Each author should be separated by a comma. Next comes
  118. the title, followed by the series title (e.g. journal name), publication date (4 digit year, 3 character Month, day), volume, issue and page numbers. You may also use HTML to provide a link in the citation.
  119. Below is an example: <pre>Medeiros PM, Ladio AH, Santos AM, Albuquerque UP. <a href="http://www.ncbi.nlm.nih.gov/pubmed/23462414" target="_blank">Does the selection of medicinal plants by Brazilian local populations
  120. suffer taxonomic influence?</a> J Ethnopharmacol. 2013 Apr 19; 146(3):842-52.</pre>'),
  121. '#required' => TRUE,
  122. );
  123. // add in the properties that are actually stored in the pub table fields.
  124. $num_properties = chado_pub_node_form_add_pub_table_props($form, $form_state, $properties_list,
  125. $d_properties, $d_removed, $d_volume, $d_volumetitle, $d_issue, $d_pages, $d_series_name);
  126. // add in the properties from the pubprop table
  127. $num_properties += chado_pub_node_form_add_pubprop_table_props($form, $form_state, $pub_id, $d_properties, $d_removed);
  128. // add in any new properties that have been added by the user through an AHAH callback
  129. $num_properties += chado_pub_node_form_add_new_props($form, $form_state, $num_new, $d_properties, $d_removed);
  130. // add an empty row of field to allow for addition of a new property
  131. chado_pub_node_form_add_new_empty_props($form, $properties_select);
  132. $form['removed'] = array(
  133. '#type' => 'hidden',
  134. '#value' => $d_removed,
  135. );
  136. $form['num_new'] = array(
  137. '#type' => 'hidden',
  138. '#value' => $num_new,
  139. );
  140. $form['is_obsolete'] = array(
  141. '#type' => 'checkbox',
  142. '#title' => t('Is Obsolete? (Check for Yes)'),
  143. '#required' => TRUE,
  144. '#default_value' => $d_is_obsolete,
  145. );
  146. return $form;
  147. }
  148. /*
  149. *
  150. */
  151. function chado_pub_validate($node, &$form) {
  152. // get the submitted values
  153. $title = trim($node->pubtitle);
  154. $uniquename = trim($node->uniquename);
  155. $type_id = trim($node->type_id);
  156. $volume = trim($node->volume);
  157. $volumetitle = trim($node->volumetitle);
  158. $series_name = trim($node->series_name);
  159. $issue = trim($node->issue);
  160. $pyear = trim($node->pyear);
  161. $pages = trim($node->pages);
  162. $miniref = trim($node->miniref);
  163. $publisher = trim($node->publisher);
  164. $pubplace = trim($node->pubplace);
  165. $is_obsolete = $node->is_obsolete;
  166. $pub_id = $node->pub_id;
  167. // if this is a delete then don't validate
  168. if($node->op == 'Delete') {
  169. return;
  170. }
  171. // make sure the year is four digits
  172. if(!preg_match('/^\d{4}$/', $pyear)){
  173. form_set_error('pyear', t('The publication year should be a 4 digit year.'));
  174. return;
  175. }
  176. // get the type of publication
  177. $values = array('cvterm_id' => $type_id);
  178. $options = array('statement_name' => 'sel_pub_ty');
  179. $cvterm = tripal_core_chado_select('cvterm', array('name'), $values, $options);
  180. if (count($cvterm) == 0) {
  181. $message = t('Invalided publication type.');
  182. form_set_error('type_id', $message);
  183. return;
  184. }
  185. // on an insert (no $pub_id) make sure the publication doesn't already exist
  186. if (!$pub_id) {
  187. $results = tripal_pub_get_pubs_by_title_type_pyear($title, $cvterm[0]->name, $pyear);
  188. if (count($results) > 0) {
  189. $message = t('A publication with this title, type and publication year, already exists. Cannot add this publication');
  190. form_set_error('pyear', $message);
  191. return;
  192. }
  193. }
  194. // on an update, make sure that if the title has changed that it doesn't
  195. // conflict with any other publication
  196. if ($pub_id) {
  197. // first get the original title, type and year before it was changed
  198. $values = array('pub_id' => $pub_id);
  199. $columns = array('title','pyear','type_id');
  200. $options = array('statement_name' => 'sel_pub_id');
  201. $pub = tripal_core_chado_select('pub', $columns, $values, $options);
  202. // if the title or year doesn't match then it was changed and we want to make
  203. // sure it doesn't already exist in another publication
  204. if((strcmp($pub[0]->title, $title) != 0) or
  205. ($pub[0]->type_id != $type_id) or
  206. ($pub[0]->year != $pyear)) {
  207. $results = tripal_pub_get_pubs_by_title_type_pyear($title, $cvterm[0]->name, $pyear);
  208. // make sure we don't capture our pub_id in the list (remove it)
  209. foreach ($results as $index => $found_pub_id) {
  210. if($found_pub_id == $pub_id){
  211. unset($results[$index]);
  212. }
  213. }
  214. if (count($results) > 0) {
  215. $message = t('A publication with this title and publication year, already exists. Cannot update this publication');
  216. form_set_error('pyear', $message);
  217. }
  218. }
  219. }
  220. }
  221. /*
  222. *
  223. */
  224. function chado_pub_node_form_add_new_empty_props(&$form, $properties_select) {
  225. // add one more blank set of property fields
  226. $form['properties']['new']["new_id"] = array(
  227. '#type' => 'select',
  228. '#options' => $properties_select,
  229. '#ahah' => array(
  230. 'path' => "tripal_pub/properties/description",
  231. 'wrapper' => 'tripal-pub-new_value-desc',
  232. 'event' => 'change',
  233. 'method' => 'replace',
  234. ),
  235. );
  236. $form['properties']['new']["new_value"] = array(
  237. '#type' => 'textarea',
  238. '#default_value' => '',
  239. '#cols' => 5,
  240. '#rows' => $rows,
  241. '#description' => '<div id="tripal-pub-new_value-desc"></div>'
  242. );
  243. $form['properties']['new']["add"] = array(
  244. '#type' => 'image_button',
  245. '#value' => t('Add'),
  246. '#src' => drupal_get_path('theme', 'tripal') . '/images/add.png',
  247. '#ahah' => array(
  248. 'path' => "tripal_pub/properties/add",
  249. 'wrapper' => 'tripal-pub-edit-properties-table',
  250. 'event' => 'click',
  251. 'method' => 'replace',
  252. ),
  253. '#attributes' => array('onClick' => 'return false;'),
  254. );
  255. }
  256. /*
  257. *
  258. */
  259. function chado_pub_node_form_add_new_props(&$form, $form_state, $num_new, &$d_properties, &$d_removed) {
  260. // first, add in all of the new properties that were added through a previous AHAH callback
  261. $j = 0;
  262. $num_properties++;
  263. // we need to find the
  264. if ($form_state['values']) {
  265. foreach ($form_state['values'] as $element_name => $value) {
  266. if (preg_match('/new_value-(\d+)-(\d+)/', $element_name, $matches)) {
  267. $new_id = $matches[1];
  268. $rank = $matches[2];
  269. // skip any properties that the user requested to delete through a previous
  270. // AHAH callback or through the current AHAH callback
  271. if($d_removed["$new_id-$rank"]) {
  272. continue;
  273. }
  274. if($form_state['post']['remove-' . $new_id . '-' . $rank]) {
  275. $d_removed["$new_id-$rank"] = 1;
  276. continue;
  277. }
  278. // get this new_id information
  279. $cvterm = tripal_core_chado_select('cvterm', array('name', 'definition'), array('cvterm_id' => $new_id));
  280. // add it to the $d_properties array
  281. $d_properties[$new_id][$rank]['name'] = $cvterm->name;
  282. $d_properties[$new_id][$rank]['id'] = $new_id;
  283. $d_properties[$new_id][$rank]['value'] = $value;
  284. $d_properties[$new_id][$rank]['definition'] = $cvterm->definition;
  285. $num_properties++;
  286. // determine how many rows we need in the textarea
  287. $rows = 1;
  288. if (preg_match('/Abstract/', $cvterm[0]->name)) {
  289. $rows = 10;
  290. }
  291. if ($cvterm[0]->name == 'Authors') {
  292. $rows = 2;
  293. }
  294. // add the new fields
  295. $form['properties']['new'][$new_id][$rank]["new_id-$new_id-$rank"] = array(
  296. '#type' => 'item',
  297. '#value' => $cvterm[0]->name
  298. );
  299. $form['properties']['new'][$new_id][$rank]["new_value-$new_id-$rank"] = array(
  300. '#type' => 'textarea',
  301. '#default_value' => $value,
  302. '#cols' => 50,
  303. '#rows' => $rows,
  304. '#description' => $cvterm->definition,
  305. );
  306. $form['properties']['new'][$new_id][$rank]["remove-$new_id-$rank"] = array(
  307. '#type' => 'image_button',
  308. '#value' => t('Remove'),
  309. '#src' => drupal_get_path('theme', 'tripal') . '/images/minus.png',
  310. '#ahah' => array(
  311. 'path' => "tripal_pub/properties/minus/$new_id/$rank",
  312. 'wrapper' => 'tripal-pub-edit-properties-table',
  313. 'event' => 'click',
  314. 'method' => 'replace',
  315. ),
  316. '#attributes' => array('onClick' => 'return false;'),
  317. );
  318. }
  319. }
  320. }
  321. // second add in any new properties added during this callback
  322. if($form_state['post']['add']) {
  323. $new_id = $form_state['values']['new_id'];
  324. $new_value = $form_state['values']['new_value'];
  325. // get the rank by counting the number of entries
  326. $rank = count($d_properties[$new_id]);
  327. // get this new_id information
  328. $cvterm = tripal_core_chado_select('cvterm', array('name', 'definition'), array('cvterm_id' => $new_id));
  329. // add it to the $d_properties array
  330. $d_properties[$new_id][$rank]['name'] = $cvterm->name;
  331. $d_properties[$new_id][$rank]['id'] = $new_id;
  332. $d_properties[$new_id][$rank]['value'] = $value;
  333. $d_properties[$new_id][$rank]['definition'] = $cvterm->definition;
  334. $num_properties++;
  335. // determine how many rows we need in the textarea
  336. $rows = 1;
  337. if (preg_match('/Abstract/', $cvterm[0]->name)) {
  338. $rows = 10;
  339. }
  340. if ($cvterm[0]->name == 'Authors') {
  341. $rows = 2;
  342. }
  343. // add the new fields
  344. $form['properties']['new'][$new_id][$rank]["new_id-$new_id-$rank"] = array(
  345. '#type' => 'item',
  346. '#value' => $cvterm[0]->name
  347. );
  348. $form['properties']['new'][$new_id][$rank]["new_value-$new_id-$rank"] = array(
  349. '#type' => 'textarea',
  350. '#default_value' => $new_value,
  351. '#cols' => 50,
  352. '#rows' => $rows,
  353. '#description' => $cvterm->definition,
  354. );
  355. $form['properties']['new'][$new_id][$rank]["remove-$new_id-$rank"] = array(
  356. '#type' => 'image_button',
  357. '#value' => t('Remove'),
  358. '#src' => drupal_get_path('theme', 'tripal') . '/images/minus.png',
  359. '#ahah' => array(
  360. 'path' => "tripal_pub/properties/minus/$new_id/$rank",
  361. 'wrapper' => 'tripal-pub-edit-properties-table',
  362. 'event' => 'click',
  363. 'method' => 'replace',
  364. ),
  365. '#attributes' => array('onClick' => 'return false;'),
  366. );
  367. }
  368. return $num_properties;
  369. }
  370. /*
  371. *
  372. */
  373. function chado_pub_node_form_add_pubprop_table_props(&$form, $form_state, $pub_id, &$d_properties, &$d_removed) {
  374. // get the properties for this publication
  375. $num_properties = 0;
  376. if(!$pub_id) {
  377. return $num_properties;
  378. }
  379. $sql = "
  380. SELECT CVT.cvterm_id, CVT.name, CVT.definition, PP.value, PP.rank
  381. FROM {pubprop} PP
  382. INNER JOIN {cvterm} CVT on CVT.cvterm_id = PP.type_id
  383. WHERE PP.pub_id = %d
  384. ORDER BY CVT.name, PP.rank
  385. ";
  386. $pub_props = chado_query($sql, $pub_id);
  387. while ($prop = db_fetch_object($pub_props)) {
  388. $type_id = $prop->cvterm_id;
  389. $rank = count($d_properties[$type_id]);
  390. // skip properties that are found in the pub table
  391. if($prop->name == "Volume" or $prop->name == "Volume Title" or
  392. $prop->name == "Issue" or $prop->name == "Pages" or
  393. $prop->name == "Citation" or $prop->name == "Journal Name") {
  394. continue;
  395. }
  396. // skip any properties that the user requested to delete through a previous
  397. // AHAH callback or through the current AHAH callback
  398. if($d_removed["$type_id-$rank"]) {
  399. continue;
  400. }
  401. if($form_state['post']['remove-' . $type_id . '-' . $rank]) {
  402. $d_removed["$type_id-$rank"] = 1;
  403. continue;
  404. }
  405. $d_properties[$type_id][$rank]['name'] = $prop->name;
  406. $d_properties[$type_id][$rank]['id'] = $type_id;
  407. $d_properties[$type_id][$rank]['value'] = $prop->value;
  408. $d_properties[$type_id][$rank]['definition'] = $prop->definition;
  409. $num_properties++;
  410. // determine how many rows we need in the textarea
  411. $rows = 1;
  412. if (preg_match('/Abstract/', $prop->name)) {
  413. $rows = 10;
  414. }
  415. if ($prop->name == 'Authors') {
  416. $rows = 2;
  417. }
  418. $form['properties'][$type_id][$rank]["prop_id-$type_id-$rank"] = array(
  419. '#type' => 'item',
  420. '#value' => $prop->name,
  421. );
  422. $form['properties'][$type_id][$rank]["prop_value-$type_id-$rank"] = array(
  423. '#type' => 'textarea',
  424. '#default_value' => $prop->value,
  425. '#cols' => 50,
  426. '#rows' => $rows,
  427. '#description' => $prop->definition,
  428. );
  429. $form['properties'][$type_id][$rank]["remove-$type_id-$rank"] = array(
  430. '#type' => 'image_button',
  431. '#value' => t('Remove'),
  432. '#src' => drupal_get_path('theme', 'tripal') . '/images/minus.png',
  433. '#ahah' => array(
  434. 'path' => "tripal_pub/properties/minus/$type_id/$rank",
  435. 'wrapper' => 'tripal-pub-edit-properties-table',
  436. 'event' => 'click',
  437. 'method' => 'replace',
  438. ),
  439. '#attributes' => array('onClick' => 'return false;'),
  440. );
  441. }
  442. return $num_properties;
  443. }
  444. /*
  445. *
  446. */
  447. function chado_pub_node_form_add_pub_table_props(&$form, $form_state, $properties_list,
  448. &$d_properties, &$d_removed, $d_volume, $d_volumetitle, $d_issue, $d_pages, $d_series_name) {
  449. $num_properties = 0;
  450. $rank = 0;
  451. // add properties that are actually part of the pub table
  452. foreach($properties_list as $type_id => $prop) {
  453. // skip any properties that the user requested to delete through a previous
  454. // AHAH callback or through the current AHAH callback
  455. if($d_removed["$type_id-$rank"]) {
  456. continue;
  457. }
  458. if($form_state['post']["remove-$type_id-$rank"]) {
  459. $d_removed["$type_id-$rank"] = 1;
  460. continue;
  461. }
  462. // if any of the properties match the fields in the pub table then we
  463. // want to include those automatically
  464. if (($prop->name == 'Volume' and $d_volume) or
  465. ($prop->name == 'Issue' and $d_issue) or
  466. ($prop->name == 'Pages' and $d_pages) or
  467. ($prop->name == 'Volume Title' and $d_volumetitle) or
  468. ($prop->name == 'Journal Name' and $d_series_name)) {
  469. $d_properties[$type_id][$rank]['name'] = $prop->name;
  470. $d_properties[$type_id][$rank]['id'] = $type_id;
  471. $d_properties[$type_id][$rank]['definition'] = $prop->definition;
  472. $num_properties++;
  473. if ($prop->name == 'Volume') {
  474. $d_properties[$type_id][$rank]['value'] = $d_volume;
  475. }
  476. if ($prop->name == 'Issue') {
  477. $d_properties[$type_id][$rank]['value'] = $d_issue;
  478. }
  479. if ($prop->name == 'Pages') {
  480. $d_properties[$type_id][$rank]['value'] = $d_pages;
  481. }
  482. if ($prop->name == 'Volume Title') {
  483. $d_properties[$type_id][$rank]['value'] = $d_volumetitle;
  484. }
  485. if ($prop->name == 'Journal Name') {
  486. $d_properties[$type_id][$rank]['value'] = $d_series_name;
  487. }
  488. // determine how many rows we need in the textarea
  489. $rows = 1;
  490. if (preg_match('/Abstract/', $prop->name)) {
  491. $rows = 10;
  492. }
  493. if ($prop->name == 'Authors') {
  494. $rows = 2;
  495. }
  496. // add in the fields
  497. $form['properties'][$type_id][$rank]["prop_id-$type_id-$rank"] = array(
  498. '#type' => 'item',
  499. '#value' => $prop->name
  500. );
  501. $form['properties'][$type_id][$rank]["prop_value-$type_id-$rank"] = array(
  502. '#type' => 'textarea',
  503. '#default_value' => $d_properties[$type_id][$rank]['value'],
  504. '#cols' => 50,
  505. '#rows' => $rows,
  506. '#description' => $description,
  507. );
  508. $form['properties'][$type_id][$rank]["remove-$type_id-$rank"] = array(
  509. '#type' => 'image_button',
  510. '#value' => t('Remove'),
  511. '#src' => drupal_get_path('theme', 'tripal') . '/images/minus.png',
  512. '#ahah' => array(
  513. 'path' => "tripal_pub/properties/minus/$type_id/$rank",
  514. 'wrapper' => 'tripal-pub-edit-properties-table',
  515. 'event' => 'click',
  516. 'method' => 'replace',
  517. ),
  518. '#attributes' => array('onClick' => 'return false;'),
  519. );
  520. }
  521. }
  522. return $num_properties;
  523. }
  524. /*
  525. *
  526. */
  527. function theme_chado_pub_node_form($form) {
  528. $properties_table = tripal_pub_theme_node_form_properties($form);
  529. $markup = drupal_render($form['pub_id']);
  530. $markup .= drupal_render($form['pubtitle']);
  531. $markup .= drupal_render($form['type_id']);
  532. $markup .= drupal_render($form['series_name']);
  533. $markup .= drupal_render($form['pyear']);
  534. $markup .= drupal_render($form['uniquename']);
  535. $markup .= "<b>Include Additional Details</b><br>You may add additional properties to this publication 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";
  536. $markup .= $properties_table;
  537. $markup .= drupal_render($form['is_obsolete']);
  538. $form['properties'] = array(
  539. '#type' => 'markup',
  540. '#value' => $markup,
  541. );
  542. return drupal_render($form);
  543. }
  544. /*
  545. *
  546. */
  547. function tripal_pub_theme_node_form_properties($form) {
  548. $rows = array();
  549. if ($form['properties']) {
  550. // first add in the properties derived from the pub and pubprop tables
  551. // the array tree for these properties looks like this:
  552. // $form['properties'][$type_id][$rank]["prop_id-$type_id-$rank"]
  553. foreach ($form['properties'] as $type_id => $elements) {
  554. // there are other fields in the properties array so we only
  555. // want the numeric ones those are our type_id
  556. if (is_numeric($type_id)) {
  557. foreach ($elements as $rank => $element) {
  558. if (is_numeric($rank)) {
  559. $rows[] = array(
  560. drupal_render($element["prop_id-$type_id-$rank"]),
  561. drupal_render($element["prop_value-$type_id-$rank"]),
  562. drupal_render($element["remove-$type_id-$rank"]),
  563. );
  564. }
  565. }
  566. }
  567. }
  568. // second, add in any new properties added by the user through AHAH callbacks
  569. // the array tree for these properties looks like this:
  570. // $form['properties']['new'][$type_id][$rank]["new_id-$new_id-$rank"]
  571. foreach ($form['properties']['new'] as $type_id => $elements) {
  572. if (is_numeric($type_id)) {
  573. foreach ($elements as $rank => $element) {
  574. if (is_numeric($rank)) {
  575. $rows[] = array(
  576. drupal_render($element["new_id-$type_id-$rank"]),
  577. drupal_render($element["new_value-$type_id-$rank"]),
  578. drupal_render($element["remove-$type_id-$rank"]),
  579. );
  580. }
  581. }
  582. }
  583. }
  584. // finally add in a set of blank field for adding a new property
  585. $rows[] = array(
  586. drupal_render($form['properties']['new']['new_id']),
  587. drupal_render($form['properties']['new']['new_value']),
  588. drupal_render($form['properties']['new']['add']),
  589. );
  590. }
  591. $headers = array('Property Type','Value', '');
  592. return theme('table', $headers, $rows, array('id'=> "tripal-pub-edit-properties-table"));
  593. }
  594. /*
  595. *
  596. */
  597. function tripal_pub_property_add() {
  598. $status = TRUE;
  599. // prepare and render the form
  600. $form = tripal_core_ahah_prepare_form();
  601. // we only want to return the properties as that's all we'll replace with this AHAh callback
  602. $data = tripal_pub_theme_node_form_properties($form);
  603. // bind javascript events to the new objects that will be returned
  604. // so that AHAH enabled elements will work.
  605. $settings = tripal_core_ahah_bind_events();
  606. // return the updated JSON
  607. drupal_json(
  608. array(
  609. 'status' => $status,
  610. 'data' => $data,
  611. 'settings' => $settings,
  612. )
  613. );
  614. }
  615. /*
  616. *
  617. */
  618. function tripal_pub_property_delete() {
  619. $status = TRUE;
  620. // prepare and render the form
  621. $form = tripal_core_ahah_prepare_form();
  622. // we only want to return the properties as that's all we'll replace with this AHAh callback
  623. $data = tripal_pub_theme_node_form_properties($form);
  624. // bind javascript events to the new objects that will be returned
  625. // so that AHAH enabled elements will work.
  626. $settings = tripal_core_ahah_bind_events();
  627. // return the updated JSON
  628. drupal_json(
  629. array(
  630. 'status' => $status,
  631. 'data' => $data,
  632. 'settings' => $settings,
  633. )
  634. );
  635. }
  636. /*
  637. *
  638. */
  639. function tripal_pub_property_get_description() {
  640. $new_id = $_POST['new_id'];
  641. $values = array('cvterm_id' => $new_id);
  642. $cvterm = tripal_core_chado_select('cvterm', array('definition'), $values);
  643. $description = '&nbsp;';
  644. if ($cvterm[0]->definition) {
  645. $description = $cvterm[0]->definition;
  646. }
  647. drupal_json(
  648. array(
  649. 'status' => TRUE,
  650. 'data' => '<div id="tripal-pub-new_value-desc">' . $description . '</div>',
  651. )
  652. );
  653. }