pub_form.inc 30 KB

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