pub_form.inc 26 KB

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