pub_form.inc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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_new = chado_pub_node_form_add_new_props($form, $form_state, $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['num_properties'] = array(
  141. '#type' => 'hidden',
  142. '#value' => $num_properties,
  143. );
  144. $form['is_obsolete'] = array(
  145. '#type' => 'checkbox',
  146. '#title' => t('Is Obsolete? (Check for Yes)'),
  147. '#required' => TRUE,
  148. '#default_value' => $d_is_obsolete,
  149. );
  150. return $form;
  151. }
  152. /*
  153. *
  154. */
  155. function chado_pub_validate($node, &$form) {
  156. // get the submitted values
  157. $title = trim($node->pubtitle);
  158. $uniquename = trim($node->uniquename);
  159. $type_id = trim($node->type_id);
  160. $volume = trim($node->volume);
  161. $volumetitle = trim($node->volumetitle);
  162. $series_name = trim($node->series_name);
  163. $issue = trim($node->issue);
  164. $pyear = trim($node->pyear);
  165. $pages = trim($node->pages);
  166. $miniref = trim($node->miniref);
  167. $publisher = trim($node->publisher);
  168. $pubplace = trim($node->pubplace);
  169. $is_obsolete = $node->is_obsolete;
  170. $pub_id = $node->pub_id;
  171. $num_properties = $node->num_properties;
  172. $num_new = $node->num_new;
  173. // if this is a delete then don't validate
  174. if($node->op == 'Delete') {
  175. return;
  176. }
  177. // make sure the year is four digits
  178. if(!preg_match('/^\d{4}$/', $pyear)){
  179. form_set_error('pyear', t('The publication year should be a 4 digit year.'));
  180. return;
  181. }
  182. // get the type of publication
  183. $values = array('cvterm_id' => $type_id);
  184. $options = array('statement_name' => 'sel_pub_ty');
  185. $cvterm = tripal_core_chado_select('cvterm', array('name'), $values, $options);
  186. if (count($cvterm) == 0) {
  187. $message = t('Invalid publication type.');
  188. form_set_error('type_id', $message);
  189. return;
  190. }
  191. // get the media name looking at the properties
  192. foreach ($node as $element => $value) {
  193. // if this is an existing property (either previously in the database or
  194. // added via AHAH/AJAX callback)
  195. if (preg_match('/^prop_value-(\d+)-(\d+)$/', $element, $matches)) {
  196. $prop_type_id = $matches[1];
  197. $prop_type = tripal_cv_get_cvterm_by_id($prop_type_id);
  198. if($prop_type->name == 'Conference Name' or $prop_type->name == 'Journal Name') {
  199. $series_name = $value;
  200. }
  201. if($prop_type->name == 'Citation') {
  202. $uniquename = $value;
  203. }
  204. }
  205. // if this is a new property (added by this submit of the form)
  206. elseif ($element = 'new_id') {
  207. $prop_type = tripal_cv_get_cvterm_by_id($value);
  208. if($prop_type->name == 'Conference Name' or $prop_type->name == 'Journal Name') {
  209. $series_name = $node->new_value;
  210. }
  211. if($prop_type->name == 'Citation') {
  212. $uniquename = $value;
  213. }
  214. }
  215. }
  216. $skip_duplicate_check = 0;
  217. // if this publication is a Patent then skip the validation below. Patents can have the title
  218. // name and year but be different
  219. if (strcmp($cvterm[0]->name,'Patent') == 0) {
  220. $skip_duplicate_check = 1;
  221. }
  222. // on an update ($pub_id is set), check to see if there have been changes to fields that
  223. // are used to check for duplicates. If not, then no need to check for duplicates
  224. if ($pub_id) {
  225. // first get the original title, type and year before it was changed
  226. $values = array('pub_id' => $pub_id);
  227. $columns = array('title', 'pyear', 'type_id', 'series_name');
  228. $options = array('statement_name' => 'sel_pub_id');
  229. $pub = tripal_core_chado_select('pub', $columns, $values, $options);
  230. // if the title, type, year or series_name have changed then check the pub
  231. // to see if it is a duplicate of another
  232. if((strcmp(strtolower($pub[0]->title), strtolower($title)) == 0) and
  233. (strcmp(strtolower($pub[0]->series_name), strtolower($series_name)) == 0) and
  234. ($pub[0]->type_id == $type_id) and
  235. ($pub[0]->year == $pyear)) {
  236. $skip_duplicate_check = 1;
  237. }
  238. }
  239. // check to see if a duplicate publication already exists
  240. if (!$skip_duplicate_check) {
  241. // make sure the publication is unique using the prefereed import duplication check
  242. $import_dups_check = variable_get('tripal_pub_import_duplicate_check', 'title_year_media');
  243. switch ($import_dups_check) {
  244. case 'title_year':
  245. $results = tripal_pub_get_pubs_by_title_type_pyear_series($title, NULL, $pyear, NULL);
  246. // make sure we don't capture our pub_id in the list (remove it)
  247. foreach ($results as $index => $found_pub_id) {
  248. if($found_pub_id == $pub_id){
  249. unset($results[$index]);
  250. }
  251. }
  252. if (count($results) > 0) {
  253. $message = t('A publication with this title and publication year, already exists.');
  254. form_set_error('pyear', $message);
  255. }
  256. break;
  257. case 'title_year_type':
  258. $results = tripal_pub_get_pubs_by_title_type_pyear_series($title, $cvterm[0]->name, $pyear, NULL);
  259. // make sure we don't capture our pub_id in the list (remove it)
  260. foreach ($results as $index => $found_pub_id) {
  261. if($found_pub_id == $pub_id){
  262. unset($results[$index]);
  263. }
  264. }
  265. if (count($results) > 0) {
  266. $message = t('A publication with this title, type and publication year, already exists.');
  267. form_set_error('pyear', $message);
  268. }
  269. break;
  270. case 'title_year_media':
  271. $results = tripal_pub_get_pubs_by_title_type_pyear_series($title, NULL, $pyear, $series_name);
  272. // make sure we don't capture our pub_id in the list (remove it)
  273. foreach ($results as $index => $found_pub_id) {
  274. if($found_pub_id == $pub_id){
  275. unset($results[$index]);
  276. }
  277. }
  278. if (count($results) > 0) {
  279. $message = t('A publication with this title, media name (e.g. Journal Name) and publication year, already exists.');
  280. form_set_error('pyear', $message);
  281. }
  282. break;
  283. }
  284. }
  285. // even though we are skipping the duplication checks above we must make sure the uniquename is unique
  286. // as that is the offical table constraint
  287. else {
  288. $results = tripal_pub_get_pub_by_uniquename($uniquename);
  289. // make sure we don't capture our pub_id in the list (remove it)
  290. foreach ($results as $index => $found_pub_id) {
  291. if($found_pub_id == $pub_id){
  292. unset($results[$index]);
  293. }
  294. }
  295. if (count($results) > 0) {
  296. $message = t('A publication with this unique citation already exists.');
  297. form_set_error('uniquename', $message);
  298. }
  299. }
  300. }
  301. /*
  302. *
  303. */
  304. function chado_pub_node_form_add_new_empty_props(&$form, $properties_select) {
  305. // add one more blank set of property fields
  306. $form['properties']['new']["new_id"] = array(
  307. '#type' => 'select',
  308. '#options' => $properties_select,
  309. '#ahah' => array(
  310. 'path' => "tripal_pub/properties/description",
  311. 'wrapper' => 'tripal-pub-new_value-desc',
  312. 'event' => 'change',
  313. 'method' => 'replace',
  314. ),
  315. );
  316. $form['properties']['new']["new_value"] = array(
  317. '#type' => 'textarea',
  318. '#default_value' => '',
  319. '#cols' => 5,
  320. '#rows' => $rows,
  321. '#description' => '<div id="tripal-pub-new_value-desc"></div>'
  322. );
  323. $form['properties']['new']["add"] = array(
  324. '#type' => 'image_button',
  325. '#value' => t('Add'),
  326. '#src' => drupal_get_path('theme', 'tripal') . '/images/add.png',
  327. '#ahah' => array(
  328. 'path' => "tripal_pub/properties/add",
  329. 'wrapper' => 'tripal-pub-edit-properties-table',
  330. 'event' => 'click',
  331. 'method' => 'replace',
  332. ),
  333. '#attributes' => array('onClick' => 'return false;'),
  334. );
  335. }
  336. /*
  337. *
  338. */
  339. function chado_pub_node_form_add_new_props(&$form, $form_state, &$d_properties, &$d_removed) {
  340. // first, add in all of the new properties that were added through a previous AHAH callback
  341. $j = 0;
  342. $num_properties++;
  343. // we need to find the
  344. if ($form_state['values']) {
  345. foreach ($form_state['values'] as $element_name => $value) {
  346. if (preg_match('/new_value-(\d+)-(\d+)/', $element_name, $matches)) {
  347. $new_id = $matches[1];
  348. $rank = $matches[2];
  349. // skip any properties that the user requested to delete through a previous
  350. // AHAH callback or through the current AHAH callback
  351. if($d_removed["$new_id-$rank"]) {
  352. continue;
  353. }
  354. if($form_state['post']['remove-' . $new_id . '-' . $rank]) {
  355. $d_removed["$new_id-$rank"] = 1;
  356. continue;
  357. }
  358. // get this new_id information
  359. $cvterm = tripal_core_chado_select('cvterm', array('name', 'definition'), array('cvterm_id' => $new_id));
  360. // add it to the $d_properties array
  361. $d_properties[$new_id][$rank]['name'] = $cvterm->name;
  362. $d_properties[$new_id][$rank]['id'] = $new_id;
  363. $d_properties[$new_id][$rank]['value'] = $value;
  364. $d_properties[$new_id][$rank]['definition'] = $cvterm->definition;
  365. $num_properties++;
  366. // determine how many rows we need in the textarea
  367. $rows = 1;
  368. if (preg_match('/Abstract/', $cvterm[0]->name)) {
  369. $rows = 10;
  370. }
  371. if ($cvterm[0]->name == 'Authors') {
  372. $rows = 2;
  373. }
  374. // add the new fields
  375. $form['properties']['new'][$new_id][$rank]["new_id-$new_id-$rank"] = array(
  376. '#type' => 'item',
  377. '#value' => $cvterm[0]->name
  378. );
  379. $form['properties']['new'][$new_id][$rank]["new_value-$new_id-$rank"] = array(
  380. '#type' => 'textarea',
  381. '#default_value' => $value,
  382. '#cols' => 50,
  383. '#rows' => $rows,
  384. '#description' => $cvterm->definition,
  385. );
  386. $form['properties']['new'][$new_id][$rank]["remove-$new_id-$rank"] = array(
  387. '#type' => 'image_button',
  388. '#value' => t('Remove'),
  389. '#src' => drupal_get_path('theme', 'tripal') . '/images/minus.png',
  390. '#ahah' => array(
  391. 'path' => "tripal_pub/properties/minus/$new_id/$rank",
  392. 'wrapper' => 'tripal-pub-edit-properties-table',
  393. 'event' => 'click',
  394. 'method' => 'replace',
  395. ),
  396. '#attributes' => array('onClick' => 'return false;'),
  397. );
  398. }
  399. }
  400. }
  401. // second add in any new properties added during this callback
  402. if($form_state['post']['add']) {
  403. $new_id = $form_state['values']['new_id'];
  404. $new_value = $form_state['values']['new_value'];
  405. // get the rank by counting the number of entries
  406. $rank = count($d_properties[$new_id]);
  407. // get this new_id information
  408. $cvterm = tripal_core_chado_select('cvterm', array('name', 'definition'), array('cvterm_id' => $new_id));
  409. // add it to the $d_properties array
  410. $d_properties[$new_id][$rank]['name'] = $cvterm->name;
  411. $d_properties[$new_id][$rank]['id'] = $new_id;
  412. $d_properties[$new_id][$rank]['value'] = $value;
  413. $d_properties[$new_id][$rank]['definition'] = $cvterm->definition;
  414. $num_properties++;
  415. // determine how many rows we need in the textarea
  416. $rows = 1;
  417. if (preg_match('/Abstract/', $cvterm[0]->name)) {
  418. $rows = 10;
  419. }
  420. if ($cvterm[0]->name == 'Authors') {
  421. $rows = 2;
  422. }
  423. // add the new fields
  424. $form['properties']['new'][$new_id][$rank]["new_id-$new_id-$rank"] = array(
  425. '#type' => 'item',
  426. '#value' => $cvterm[0]->name
  427. );
  428. $form['properties']['new'][$new_id][$rank]["new_value-$new_id-$rank"] = array(
  429. '#type' => 'textarea',
  430. '#default_value' => $new_value,
  431. '#cols' => 50,
  432. '#rows' => $rows,
  433. '#description' => $cvterm->definition,
  434. );
  435. $form['properties']['new'][$new_id][$rank]["remove-$new_id-$rank"] = array(
  436. '#type' => 'image_button',
  437. '#value' => t('Remove'),
  438. '#src' => drupal_get_path('theme', 'tripal') . '/images/minus.png',
  439. '#ahah' => array(
  440. 'path' => "tripal_pub/properties/minus/$new_id/$rank",
  441. 'wrapper' => 'tripal-pub-edit-properties-table',
  442. 'event' => 'click',
  443. 'method' => 'replace',
  444. ),
  445. '#attributes' => array('onClick' => 'return false;'),
  446. );
  447. }
  448. return $num_properties;
  449. }
  450. /*
  451. *
  452. */
  453. function chado_pub_node_form_add_pubprop_table_props(&$form, $form_state, $pub_id, &$d_properties, &$d_removed) {
  454. // get the properties for this publication
  455. $num_properties = 0;
  456. if(!$pub_id) {
  457. return $num_properties;
  458. }
  459. $sql = "
  460. SELECT CVT.cvterm_id, CVT.name, CVT.definition, PP.value, PP.rank
  461. FROM {pubprop} PP
  462. INNER JOIN {cvterm} CVT on CVT.cvterm_id = PP.type_id
  463. WHERE PP.pub_id = %d
  464. ORDER BY CVT.name, PP.rank
  465. ";
  466. $pub_props = chado_query($sql, $pub_id);
  467. while ($prop = db_fetch_object($pub_props)) {
  468. $type_id = $prop->cvterm_id;
  469. $rank = count($d_properties[$type_id]);
  470. // skip properties that are found in the pub table
  471. if($prop->name == "Volume" or $prop->name == "Volume Title" or
  472. $prop->name == "Issue" or $prop->name == "Pages" or
  473. $prop->name == "Citation" or $prop->name == "Journal Name") {
  474. continue;
  475. }
  476. // skip any properties that the user requested to delete through a previous
  477. // AHAH callback or through the current AHAH callback
  478. if($d_removed["$type_id-$rank"]) {
  479. continue;
  480. }
  481. if($form_state['post']['remove-' . $type_id . '-' . $rank]) {
  482. $d_removed["$type_id-$rank"] = 1;
  483. continue;
  484. }
  485. $d_properties[$type_id][$rank]['name'] = $prop->name;
  486. $d_properties[$type_id][$rank]['id'] = $type_id;
  487. $d_properties[$type_id][$rank]['value'] = $prop->value;
  488. $d_properties[$type_id][$rank]['definition'] = $prop->definition;
  489. $num_properties++;
  490. // determine how many rows we need in the textarea
  491. $rows = 1;
  492. if (preg_match('/Abstract/', $prop->name)) {
  493. $rows = 10;
  494. }
  495. if ($prop->name == 'Authors') {
  496. $rows = 2;
  497. }
  498. $form['properties'][$type_id][$rank]["prop_id-$type_id-$rank"] = array(
  499. '#type' => 'item',
  500. '#value' => $prop->name,
  501. );
  502. $form['properties'][$type_id][$rank]["prop_value-$type_id-$rank"] = array(
  503. '#type' => 'textarea',
  504. '#default_value' => $prop->value,
  505. '#cols' => 50,
  506. '#rows' => $rows,
  507. '#description' => $prop->definition,
  508. );
  509. $form['properties'][$type_id][$rank]["remove-$type_id-$rank"] = array(
  510. '#type' => 'image_button',
  511. '#value' => t('Remove'),
  512. '#src' => drupal_get_path('theme', 'tripal') . '/images/minus.png',
  513. '#ahah' => array(
  514. 'path' => "tripal_pub/properties/minus/$type_id/$rank",
  515. 'wrapper' => 'tripal-pub-edit-properties-table',
  516. 'event' => 'click',
  517. 'method' => 'replace',
  518. ),
  519. '#attributes' => array('onClick' => 'return false;'),
  520. );
  521. }
  522. return $num_properties;
  523. }
  524. /*
  525. *
  526. */
  527. function chado_pub_node_form_add_pub_table_props(&$form, $form_state, $properties_list,
  528. &$d_properties, &$d_removed, $d_volume, $d_volumetitle, $d_issue, $d_pages, $d_series_name) {
  529. $num_properties = 0;
  530. $rank = 0;
  531. // add properties that are actually part of the pub table
  532. foreach($properties_list as $type_id => $prop) {
  533. // skip any properties that the user requested to delete through a previous
  534. // AHAH callback or through the current AHAH callback
  535. if($d_removed["$type_id-$rank"]) {
  536. continue;
  537. }
  538. if($form_state['post']["remove-$type_id-$rank"]) {
  539. $d_removed["$type_id-$rank"] = 1;
  540. continue;
  541. }
  542. // if any of the properties match the fields in the pub table then we
  543. // want to include those automatically
  544. if (($prop->name == 'Volume' and $d_volume) or
  545. ($prop->name == 'Issue' and $d_issue) or
  546. ($prop->name == 'Pages' and $d_pages) or
  547. ($prop->name == 'Volume Title' and $d_volumetitle) or
  548. ($prop->name == 'Journal Name' and $d_series_name)) {
  549. $d_properties[$type_id][$rank]['name'] = $prop->name;
  550. $d_properties[$type_id][$rank]['id'] = $type_id;
  551. $d_properties[$type_id][$rank]['definition'] = $prop->definition;
  552. $num_properties++;
  553. if ($prop->name == 'Volume') {
  554. $d_properties[$type_id][$rank]['value'] = $d_volume;
  555. }
  556. if ($prop->name == 'Issue') {
  557. $d_properties[$type_id][$rank]['value'] = $d_issue;
  558. }
  559. if ($prop->name == 'Pages') {
  560. $d_properties[$type_id][$rank]['value'] = $d_pages;
  561. }
  562. if ($prop->name == 'Volume Title') {
  563. $d_properties[$type_id][$rank]['value'] = $d_volumetitle;
  564. }
  565. if ($prop->name == 'Journal Name') {
  566. $d_properties[$type_id][$rank]['value'] = $d_series_name;
  567. }
  568. // determine how many rows we need in the textarea
  569. $rows = 1;
  570. if (preg_match('/Abstract/', $prop->name)) {
  571. $rows = 10;
  572. }
  573. if ($prop->name == 'Authors') {
  574. $rows = 2;
  575. }
  576. // add in the fields
  577. $form['properties'][$type_id][$rank]["prop_id-$type_id-$rank"] = array(
  578. '#type' => 'item',
  579. '#value' => $prop->name
  580. );
  581. $form['properties'][$type_id][$rank]["prop_value-$type_id-$rank"] = array(
  582. '#type' => 'textarea',
  583. '#default_value' => $d_properties[$type_id][$rank]['value'],
  584. '#cols' => 50,
  585. '#rows' => $rows,
  586. '#description' => $description,
  587. );
  588. $form['properties'][$type_id][$rank]["remove-$type_id-$rank"] = array(
  589. '#type' => 'image_button',
  590. '#value' => t('Remove'),
  591. '#src' => drupal_get_path('theme', 'tripal') . '/images/minus.png',
  592. '#ahah' => array(
  593. 'path' => "tripal_pub/properties/minus/$type_id/$rank",
  594. 'wrapper' => 'tripal-pub-edit-properties-table',
  595. 'event' => 'click',
  596. 'method' => 'replace',
  597. ),
  598. '#attributes' => array('onClick' => 'return false;'),
  599. );
  600. }
  601. }
  602. return $num_properties;
  603. }
  604. /*
  605. *
  606. */
  607. function theme_chado_pub_node_form($form) {
  608. $properties_table = tripal_pub_theme_node_form_properties($form);
  609. $markup = drupal_render($form['pub_id']);
  610. $markup .= drupal_render($form['pubtitle']);
  611. $markup .= drupal_render($form['type_id']);
  612. $markup .= drupal_render($form['series_name']);
  613. $markup .= drupal_render($form['pyear']);
  614. $markup .= drupal_render($form['uniquename']);
  615. $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";
  616. $markup .= $properties_table;
  617. $markup .= drupal_render($form['is_obsolete']);
  618. $form['properties'] = array(
  619. '#type' => 'markup',
  620. '#value' => $markup,
  621. );
  622. return drupal_render($form);
  623. }
  624. /*
  625. *
  626. */
  627. function tripal_pub_theme_node_form_properties($form) {
  628. $rows = array();
  629. if ($form['properties']) {
  630. // first add in the properties derived from the pub and pubprop tables
  631. // the array tree for these properties looks like this:
  632. // $form['properties'][$type_id][$rank]["prop_id-$type_id-$rank"]
  633. foreach ($form['properties'] as $type_id => $elements) {
  634. // there are other fields in the properties array so we only
  635. // want the numeric ones those are our type_id
  636. if (is_numeric($type_id)) {
  637. foreach ($elements as $rank => $element) {
  638. if (is_numeric($rank)) {
  639. $rows[] = array(
  640. drupal_render($element["prop_id-$type_id-$rank"]),
  641. drupal_render($element["prop_value-$type_id-$rank"]),
  642. drupal_render($element["remove-$type_id-$rank"]),
  643. );
  644. }
  645. }
  646. }
  647. }
  648. // second, add in any new properties added by the user through AHAH callbacks
  649. // the array tree for these properties looks like this:
  650. // $form['properties']['new'][$type_id][$rank]["new_id-$new_id-$rank"]
  651. foreach ($form['properties']['new'] as $type_id => $elements) {
  652. if (is_numeric($type_id)) {
  653. foreach ($elements as $rank => $element) {
  654. if (is_numeric($rank)) {
  655. $rows[] = array(
  656. drupal_render($element["new_id-$type_id-$rank"]),
  657. drupal_render($element["new_value-$type_id-$rank"]),
  658. drupal_render($element["remove-$type_id-$rank"]),
  659. );
  660. }
  661. }
  662. }
  663. }
  664. // finally add in a set of blank field for adding a new property
  665. $rows[] = array(
  666. drupal_render($form['properties']['new']['new_id']),
  667. drupal_render($form['properties']['new']['new_value']),
  668. drupal_render($form['properties']['new']['add']),
  669. );
  670. }
  671. $headers = array('Property Type','Value', '');
  672. return theme('table', $headers, $rows, array('id'=> "tripal-pub-edit-properties-table"));
  673. }
  674. /*
  675. *
  676. */
  677. function tripal_pub_property_add() {
  678. $status = TRUE;
  679. // prepare and render the form
  680. $form = tripal_core_ahah_prepare_form();
  681. // we only want to return the properties as that's all we'll replace with this AHAh callback
  682. $data = tripal_pub_theme_node_form_properties($form);
  683. // bind javascript events to the new objects that will be returned
  684. // so that AHAH enabled elements will work.
  685. $settings = tripal_core_ahah_bind_events();
  686. // return the updated JSON
  687. drupal_json(
  688. array(
  689. 'status' => $status,
  690. 'data' => $data,
  691. 'settings' => $settings,
  692. )
  693. );
  694. }
  695. /*
  696. *
  697. */
  698. function tripal_pub_property_delete() {
  699. $status = TRUE;
  700. // prepare and render the form
  701. $form = tripal_core_ahah_prepare_form();
  702. // we only want to return the properties as that's all we'll replace with this AHAh callback
  703. $data = tripal_pub_theme_node_form_properties($form);
  704. // bind javascript events to the new objects that will be returned
  705. // so that AHAH enabled elements will work.
  706. $settings = tripal_core_ahah_bind_events();
  707. // return the updated JSON
  708. drupal_json(
  709. array(
  710. 'status' => $status,
  711. 'data' => $data,
  712. 'settings' => $settings,
  713. )
  714. );
  715. }
  716. /*
  717. *
  718. */
  719. function tripal_pub_property_get_description() {
  720. $new_id = $_POST['new_id'];
  721. $values = array('cvterm_id' => $new_id);
  722. $cvterm = tripal_core_chado_select('cvterm', array('definition'), $values);
  723. $description = '&nbsp;';
  724. if ($cvterm[0]->definition) {
  725. $description = $cvterm[0]->definition;
  726. }
  727. drupal_json(
  728. array(
  729. 'status' => TRUE,
  730. 'data' => '<div id="tripal-pub-new_value-desc">' . $description . '</div>',
  731. )
  732. );
  733. }