pub_form.inc 30 KB

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