pub_form.inc 30 KB

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