pub_form.inc 26 KB

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