pub_form.inc 30 KB

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