tripal_pub.chado_node.inc 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. <?php
  2. /**
  3. * @file
  4. * Implements the pub node content type
  5. */
  6. /**
  7. * Implements hook_node_info().
  8. *
  9. * @ingroup tripal_pub
  10. */
  11. function tripal_pub_node_info() {
  12. return array(
  13. 'chado_pub' => array(
  14. 'name' => t('Publication'),
  15. 'base' => 'chado_pub',
  16. 'description' => t('A publication from the Chado database'),
  17. 'has_title' => TRUE,
  18. 'locked' => TRUE,
  19. 'chado_node_api' => array(
  20. 'base_table' => 'pub',
  21. 'hook_prefix' => 'chado_pub',
  22. 'record_type_title' => array(
  23. 'singular' => t('Publication'),
  24. 'plural' => t('Publications')
  25. ),
  26. 'sync_filters' => array(
  27. 'type_id' => FALSE,
  28. 'organism_id' => FALSE
  29. ),
  30. ),
  31. ),
  32. );
  33. }
  34. /**
  35. * Implements hook_form().
  36. *
  37. * @ingroup tripal_pub
  38. */
  39. function chado_pub_form($node, $form_state) {
  40. $form = array();
  41. // Default values can come in the following ways:
  42. //
  43. // 1) as elements of the $node object. This occurs when editing an existing pub
  44. // 2) in the $form_state['values'] array which occurs on a failed validation or
  45. // ajax callbacks from non submit form elements
  46. // 3) in the $form_state['input'] array which occurs on ajax callbacks from submit
  47. // form elements and the form is being rebuilt
  48. //
  49. // set form field defaults
  50. $pub_id = null;
  51. $title = '';
  52. $pyear = '';
  53. $uniquename = '';
  54. $type_id = '';
  55. $is_obsolete = '';
  56. // some of the fields in the pub table should show up in the properties
  57. // form elements to make the form more seemless. We will add them
  58. // to this array.
  59. $more_props = array();
  60. // if we are editing an existing node then the pub is already part of the node
  61. if (property_exists($node, 'pub')) {
  62. $pub = $node->pub;
  63. $pub = chado_expand_var($pub, 'field', 'pub.title');
  64. $pub = chado_expand_var($pub, 'field', 'pub.volumetitle');
  65. $pub = chado_expand_var($pub, 'field', 'pub.uniquename');
  66. $pub_id = $pub->pub_id;
  67. $title = $pub->title;
  68. $pyear = $pub->pyear;
  69. $uniquename = $pub->uniquename;
  70. $type_id = $pub->type_id->cvterm_id;
  71. $is_obsolete = $pub->is_obsolete;
  72. // if the obsolete value is set by the database then it is in the form of
  73. // 't' or 'f', we need to convert to 1 or 0
  74. $is_obsolete = $is_obsolete == 't' ? 1 : $is_obsolete;
  75. $is_obsolete = $is_obsolete == 'f' ? 0 : $is_obsolete;
  76. // set the organism_id in the form
  77. $form['pub_id'] = array(
  78. '#type' => 'value',
  79. '#value' => $pub->pub_id,
  80. );
  81. // get fields from the pub table and convert them to properties. We will add these to the $more_props
  82. // array which gets passed in to the tripal_core_properties_form() API call further down
  83. if ($pub->volumetitle) {
  84. $cvterm = tripal_get_cvterm(array(
  85. 'name' => 'Volume Title',
  86. 'cv_id' => array('name' => 'tripal_pub')
  87. ));
  88. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->volumetitle);
  89. }
  90. if ($pub->volume) {
  91. $cvterm = tripal_get_cvterm(array(
  92. 'name' => 'Volume',
  93. 'cv_id' => array('name' => 'tripal_pub')
  94. ));
  95. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->volume);
  96. }
  97. if ($pub->series_name) {
  98. switch ($pub->type_id->name) {
  99. case 'Journal Article':
  100. $cvterm = tripal_get_cvterm(array(
  101. 'name' => 'Journal Name',
  102. 'cv_id' => array('name' => 'tripal_pub')
  103. ));
  104. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->series_name);
  105. break;
  106. case 'Conference Proceedings':
  107. $cvterm = tripal_get_cvterm(array(
  108. 'name' => 'Conference Name',
  109. 'cv_id' => array('name' => 'tripal_pub')
  110. ));
  111. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->series_name);
  112. break;
  113. default:
  114. $cvterm = tripal_get_cvterm(array(
  115. 'name' => 'Series Name',
  116. 'cv_id' => array('tripal_pub')
  117. ));
  118. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->series_name);
  119. }
  120. }
  121. if ($pub->issue) {
  122. $cvterm = tripal_get_cvterm(array(
  123. 'name' => 'Issue',
  124. 'cv_id' => array('name' => 'tripal_pub')
  125. ));
  126. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->issue);
  127. }
  128. if ($pub->pages) {
  129. $cvterm = tripal_get_cvterm(array(
  130. 'name' => 'Pages',
  131. 'cv_id' => array('name' => 'tripal_pub')
  132. ));
  133. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->pages);
  134. }
  135. if ($pub->miniref) {
  136. // not sure what to do with this one
  137. }
  138. if ($pub->publisher) {
  139. $cvterm = tripal_get_cvterm(array(
  140. 'name' => 'Publisher',
  141. 'cv_id' => array('name' => 'tripal_pub')
  142. ));
  143. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->publisher);
  144. }
  145. if ($pub->pubplace) {
  146. $cvterm = tripal_get_cvterm(array(
  147. 'name' => 'Published Location',
  148. 'cv_id' => array('name' => 'tripal_pub')
  149. ));
  150. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->pages);
  151. }
  152. }
  153. // if we are re constructing the form from a failed validation or ajax callback
  154. // then use the $form_state['values'] values
  155. if (array_key_exists('values', $form_state) and isset($form_state['values']['pubtitle'])) {
  156. $title = $form_state['values']['pubtitle'];
  157. $pyear = $form_state['values']['pyear'];
  158. $uniquename = $form_state['values']['uniquename'];
  159. $type_id = $form_state['values']['type_id'];
  160. $is_obsolete = $form_state['values']['is_obsolete'];
  161. }
  162. // if we are re building the form from after submission (from ajax call) then
  163. // the values are in the $form_state['input'] array
  164. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  165. $title = $form_state['input']['pubtitle'];
  166. $uniquename = $form_state['input']['uniquename'];
  167. $type_id = $form_state['input']['type_id'];
  168. $is_obsolete = array_key_exists('is_obsolete', $form_state['input']) ? $form_state['input']['is_obsolete'] : '';
  169. }
  170. $form['pubtitle'] = array(
  171. '#type' => 'textarea',
  172. '#title' => t('Publication Title'),
  173. '#default_value' => $title,
  174. '#required' => TRUE,
  175. );
  176. $type_cv = tripal_get_default_cv('pub', 'type_id');
  177. if ($type_cv->name == 'tripal_pub') {
  178. // get the list of publication types. In the Tripal publication
  179. // ontologies these are all grouped under the term 'Publication Type'
  180. // we want the default to be 'Journal Article'
  181. $sql = "
  182. SELECT
  183. CVTS.cvterm_id, CVTS.name
  184. FROM {cvtermpath} CVTP
  185. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  186. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  187. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  188. WHERE
  189. CV.name = 'tripal_pub' AND CVTO.name = 'Publication Type' AND
  190. NOT CVTS.is_obsolete = 1
  191. ORDER BY CVTS.name ASC
  192. ";
  193. $results = chado_query($sql);
  194. $pub_types = array();
  195. while ($pub_type = $results->fetchObject()) {
  196. $pub_types[$pub_type->cvterm_id] = $pub_type->name;
  197. // if we don't have a default type then set the default to be 'Journal Article'
  198. if (strcmp($pub_type->name,"Journal Article") == 0 and !$type_id) {
  199. $type_id = $pub_type->cvterm_id;
  200. }
  201. }
  202. }
  203. else {
  204. $pub_types = tripal_get_cvterm_default_select_options('pub', 'type_id', 'publication types');
  205. $pub_types[0] = 'Select a Type';
  206. }
  207. $form['type_id'] = array(
  208. '#type' => 'select',
  209. '#title' => t('Publication Type'),
  210. '#options' => $pub_types,
  211. '#required' => TRUE,
  212. '#default_value' => $type_id,
  213. );
  214. $form['pyear'] = array(
  215. '#type' => 'textfield',
  216. '#title' => t('Publication Year'),
  217. '#default_value' => $pyear,
  218. '#required' => TRUE,
  219. '#size' => 5,
  220. '#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.'),
  221. );
  222. $form['uniquename'] = array(
  223. '#type' => 'textarea',
  224. '#title' => t('Citation'),
  225. '#default_value' => $uniquename,
  226. '#description' => t('All publications must have a unique citation.
  227. <b>Please enter the full citation for this publication or leave blank and one will be generated
  228. automatically if possible</b>. For PubMed style citations list
  229. the last name of the author followed by initials. Each author should be separated by a comma. Next comes
  230. 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.
  231. 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
  232. suffer taxonomic influence?</a> J Ethnopharmacol. 2013 Apr 19; 146(3):842-52.</pre>'),
  233. );
  234. $form['is_obsolete'] = array(
  235. '#type' => 'checkbox',
  236. '#title' => t('Is Obsolete? (Check for Yes)'),
  237. '#default_value' => $is_obsolete,
  238. );
  239. // Properties Form
  240. // ----------------------------------
  241. $select_options = array();
  242. $prop_cv = tripal_get_default_cv('pubprop', 'type_id');
  243. $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
  244. // if the poperty cv is 'tripal_pub' then we need to pass in our own select_options
  245. // for only a subset of the vocabulary
  246. if ($prop_cv->name == 'tripal_pub') {
  247. $select_options[] = 'Select a Property';
  248. $sql = "
  249. SELECT
  250. DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
  251. FROM {cvtermpath} CVTP
  252. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  253. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  254. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  255. WHERE CV.name = 'tripal_pub' and
  256. (CVTO.name = 'Publication Details' OR CVTS.name = 'Publication Type') AND
  257. NOT CVTS.is_obsolete = 1
  258. ORDER BY CVTS.name ASC
  259. ";
  260. $prop_types = chado_query($sql);
  261. while ($prop = $prop_types->fetchObject()) {
  262. // add all properties except the Citation. That property is set via the uniquename field
  263. if ($prop->name == 'Citation') {
  264. continue;
  265. }
  266. // Publication Dbxref's are handled by the dbxref form addition below
  267. if ($prop->name == 'Publication Dbxref') {
  268. continue;
  269. }
  270. $select_options[$prop->cvterm_id] = $prop->name;
  271. }
  272. }
  273. $details = array(
  274. 'property_table' => 'pubprop',
  275. 'chado_id' => $pub_id,
  276. 'cv_id' => $cv_id,
  277. 'select_options' => $select_options,
  278. 'default_properties' => $more_props,
  279. );
  280. chado_add_node_form_properties($form, $form_state, $details);
  281. // RELATIONSHIPS FORM
  282. //---------------------------------------------
  283. $relationship_cv = tripal_get_default_cv('pub_relationship', 'type_id');
  284. $cv_id = $relationship_cv ? $relationship_cv->cv_id : NULL;
  285. $details = array(
  286. 'relationship_table' => 'pub_relationship', // the name of the _relationship table
  287. 'base_table' => 'pub', // the name of your chado base table
  288. 'base_foreign_key' => 'pub_id', // the name of the key in your base chado table
  289. 'base_key_value' => $pub_id, // the value of pub_id for this record
  290. 'nodetype' => 'pub', // the human-readable name of your node type
  291. 'cv_id' => $cv_id, // the cv.cv_id of the cv containing the relationships
  292. 'base_name_field' => 'uniquename', // the base table field you want to be used as the name
  293. );
  294. // Adds the form elements to your current form
  295. chado_add_node_form_relationships($form, $form_state, $details);
  296. // ADDITIONAL DBXREFS FORM
  297. //---------------------------------------------
  298. $details = array(
  299. 'linking_table' => 'pub_dbxref', // the name of the _dbxref table
  300. 'base_foreign_key' => 'pub_id', // the name of the key in your base chado table
  301. 'base_key_value' => $pub_id // the value of pub_id for this record
  302. );
  303. // Adds the form elements to your current form
  304. chado_add_node_form_dbxrefs($form, $form_state, $details);
  305. return $form;
  306. }
  307. /**
  308. * Implements hook_validate().
  309. *
  310. * @ingroup tripal_pub
  311. */
  312. function chado_pub_validate($node, $form, &$form_state) {
  313. // We only want to validate when the node is saved.
  314. // Since this validate can be called on AJAX and Deletion of the node
  315. // we need to make this check to ensure queries are not executed
  316. // without the proper values.
  317. if($node->op != 'Save') {
  318. return;
  319. }
  320. // we are syncing if we do not have a node ID but we do have a pub_id. We don't
  321. // need to validate during syncing so just skip it.
  322. if (is_null($node->nid) and property_exists($node, 'pub_id') and $node->pub_id != 0) {
  323. return;
  324. }
  325. // get the submitted values
  326. $title = trim($node->pubtitle);
  327. $pyear = trim($node->pyear);
  328. $uniquename = trim($node->uniquename);
  329. $is_obsolete = $node->is_obsolete;
  330. $type_id = $node->type_id;
  331. $pub = array();
  332. // make sure the year is four digits
  333. if(!preg_match('/^\d{4}$/', $pyear)){
  334. form_set_error('pyear', t('The publication year should be a 4 digit year.'));
  335. return;
  336. }
  337. // get the type of publication
  338. $values = array('cvterm_id' => $type_id);
  339. $cvterm = chado_select_record('cvterm', array('name'), $values);
  340. if (count($cvterm) == 0) {
  341. $message = t('Invalid publication type.');
  342. form_set_error('type_id', $message);
  343. return;
  344. }
  345. // get the media name looking at the properties
  346. $series_name = '';
  347. $properties = chado_retrieve_node_form_properties($node);
  348. foreach ($properties as $key => $prop_values) {
  349. $values = array('cvterm_id' => $key);
  350. $prop_type = chado_select_record('cvterm', array('name'), $values);
  351. if ($prop_type[0]->name == 'Conference Name' or
  352. $prop_type[0]->name == 'Journal Name' or
  353. $prop_type[0]->name == 'Series Name') {
  354. $series_name = $prop_values[0];
  355. }
  356. if ($prop_type[0]->name == 'Citation') {
  357. $uniquename = $prop_values[0];
  358. }
  359. if (count($prop_values) == 1) {
  360. $pub[$prop_type[0]->name] = $prop_values[0];
  361. }
  362. else {
  363. $pub[$prop_type[0]->name] = $prop_values;
  364. }
  365. }
  366. // if the citation is missing then try to generate one
  367. if (!$uniquename) {
  368. $pub['Title'] = $title;
  369. $pub['Publication Type'][0] = $cvterm[0]->name;
  370. $pub['Year'] = $pyear;
  371. $uniquename = tripal_pub_create_citation($pub);
  372. if (!$uniquename) {
  373. form_set_error('uniquename', "Cannot automatically generate a citation for this publication type. Please add one manually.");
  374. }
  375. }
  376. $skip_duplicate_check = 0;
  377. // if this publication is a Patent then skip the validation below. Patents can have the title
  378. // name and year but be different
  379. if (strcmp($cvterm[0]->name,'Patent') == 0) {
  380. $skip_duplicate_check = 1;
  381. }
  382. // Validating for an update
  383. if (!is_null($node->nid)) {
  384. $pub_id = $node->pub_id;
  385. // check to see if a duplicate publication already exists
  386. if (!$skip_duplicate_check) {
  387. chado_pub_validate_check_duplicate($title, $pyear, $series_name, $cvterm[0], $pub_id);
  388. }
  389. chado_pub_validate_check_uniquename($uniquename, $pub_id);
  390. }
  391. // Validating for an insert
  392. else {
  393. chado_pub_validate_check_duplicate($title, $pyear, $series_name, $cvterm[0]);
  394. chado_pub_validate_check_uniquename($uniquename);
  395. }
  396. }
  397. /**
  398. * Validate the publication uniquename. To be called from hook_validate().
  399. *
  400. * @param $uniquename
  401. * The uniquename of the publication
  402. * @param $pub_id
  403. * If an update, provide the pub_id so we don't check for a matching
  404. * uniquename of the pub we are editing
  405. *
  406. * @ingroup tripal_pub
  407. */
  408. function chado_pub_validate_check_uniquename($uniquename, $pub_id = NULL) {
  409. // check to see if a pub exists with this uniquename
  410. $pub = tripal_get_publication(array('uniquename' => $uniquename));
  411. if ($pub) {
  412. // if a $pub_id is provided to the function then this is an update
  413. // if the pub_id's don't match then a different pub with the same
  414. // uniquename already exists.
  415. if ($pub->pub_id != $pub_id) {
  416. $message = t('A publication with this unique citation already exists.');
  417. form_set_error('uniquename', $message);
  418. }
  419. }
  420. }
  421. /**
  422. * Check for duplicate publications. To be called from hook_validate(). Sets
  423. * the form error if a duplicate
  424. *
  425. * @param $title
  426. * The title of the publication
  427. * @param $pyear
  428. * The year the publication was published
  429. * @param $series_name
  430. * The series name of the publication
  431. * @param $cvterm
  432. * The type of publication
  433. * @param $pub_id
  434. * the unique id of the publication
  435. *
  436. *
  437. * @ingroup tripal_pub
  438. */
  439. function chado_pub_validate_check_duplicate($title, $pyear, $series_name, $cvterm, $pub_id = NULL) {
  440. $pub_details = array(
  441. 'Title' => $title,
  442. 'Year' => $pyear,
  443. 'Series Name' => $series_name,
  444. 'Publication Type' => $cvterm->name,
  445. );
  446. // TODO: need to include the Publication Dbxref in the $pub_details as well
  447. $pub_ids = tripal_publication_exists($pub_details);
  448. // if we found only one publication and it is our publication then return, we're good.
  449. if (count($pub_ids) == 1 and in_array($pub_id, $pub_ids)) {
  450. return;
  451. }
  452. if (count($pub_ids) == 0) {
  453. // there is no match so return
  454. return;
  455. }
  456. // return an appropriate message based on the unique constraint settings
  457. $import_dups_check = variable_get('tripal_pub_import_duplicate_check', 'title_year_media');
  458. switch ($import_dups_check) {
  459. case 'title_year':
  460. $message = t('A publication with this title and publication year already exists.');
  461. form_set_error('pubtitle', $message);
  462. break;
  463. case 'title_year_type':
  464. $message = t('A publication with this title, type and publication year already exists.');
  465. form_set_error('pubtitle', $message);
  466. break;
  467. case 'title_year_media':
  468. $message = t('A publication with this title, media name (e.g. Journal Name) and publication year already exists.');
  469. form_set_error('pubtitle', $message);
  470. break;
  471. }
  472. }
  473. /**
  474. * Implement hook_node_access().
  475. *
  476. * This hook allows node modules to limit access to the node types they define.
  477. *
  478. * @param $node
  479. * The node on which the operation is to be performed, or, if it does not yet exist, the
  480. * type of node to be created
  481. *
  482. * @param $op
  483. * The operation to be performed
  484. *
  485. * @param $account
  486. * A user object representing the user for whom the operation is to be performed
  487. *
  488. * @return
  489. * If the permission for the specified operation is not set then return FALSE. If the
  490. * permission is set then return NULL as this allows other modules to disable
  491. * access. The only exception is when the $op == 'create'. We will always
  492. * return TRUE if the permission is set.
  493. *
  494. * @ingroup tripal_pub
  495. */
  496. function chado_pub_node_access($node, $op, $account) {
  497. $node_type = $node;
  498. if (is_object($node)) {
  499. $node_type = $node->type;
  500. }
  501. if($node_type == 'chado_pub') {
  502. if ($op == 'create') {
  503. if (!user_access('create chado_pub', $account)) {
  504. return NODE_ACCESS_DENY;
  505. }
  506. return NODE_ACCESS_ALLOW;
  507. }
  508. if ($op == 'update') {
  509. if (!user_access('edit chado_pub', $account)) {
  510. return NODE_ACCESS_DENY;
  511. }
  512. }
  513. if ($op == 'delete') {
  514. if (!user_access('delete chado_pub', $account)) {
  515. return NODE_ACCESS_DENY;
  516. }
  517. }
  518. if ($op == 'view') {
  519. if (!user_access('access chado_pub', $account)) {
  520. return NODE_ACCESS_DENY;
  521. }
  522. }
  523. return NODE_ACCESS_IGNORE;
  524. }
  525. }
  526. /**
  527. * Implements hook_insert().
  528. *
  529. * @ingroup tripal_pub
  530. */
  531. function chado_pub_insert($node) {
  532. $node->pubtitle = trim($node->pubtitle);
  533. $node->pyear = trim($node->pyear);
  534. $node->uniquename = trim($node->uniquename);
  535. $is_obsolete = $node->is_obsolete;
  536. $type_id = $node->type_id;
  537. // we need an array suitable for the tripal_pub_create_citation() function
  538. // to automatically generate a citation if a uniquename doesn't already exist
  539. $pub_arr = array();
  540. // if there is an pub_id in the $node object then this must be a sync so
  541. // we can skip adding the pub as it is already there, although
  542. // we do need to proceed with the rest of the insert
  543. if (!property_exists($node, 'pub_id')) {
  544. $properties = array(); // stores all of the properties we need to add
  545. $cross_refs = array(); // stores any cross references for this publication
  546. // get the properties from the form
  547. $properties = chado_retrieve_node_form_properties($node);
  548. // get the list of properties for easy lookup (without doing lots of database queries
  549. $properties_list = array();
  550. $sql = "
  551. SELECT CVTS.cvterm_id, CVTS.name, CVTS.definition
  552. FROM {cvtermpath} CVTP
  553. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  554. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  555. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  556. WHERE CV.name = 'tripal_pub' and CVTO.name = 'Publication Details' and
  557. NOT CVTS.is_obsolete = 1
  558. ORDER BY CVTS.name ASC
  559. ";
  560. $prop_types = chado_query($sql);
  561. while ($prop = $prop_types->fetchObject()) {
  562. $properties_list[$prop->cvterm_id] = $prop->name;
  563. // The 'Citation' term is special because it serves
  564. // both as a property and as the uniquename for the
  565. // pub and we want it stored in both the pub table and the pubprop table
  566. if ($prop->name == 'Citation') {
  567. $citation_id = $prop->cvterm_id;
  568. if (!empty($node->uniquename)) {
  569. $properties[$citation_id][0] = $node->uniquename;
  570. }
  571. }
  572. }
  573. // iterate through all of the properties and remove those that really are
  574. // part of the pub table fields
  575. $volume = '';
  576. $volumetitle = '';
  577. $issue = '';
  578. $pages = '';
  579. $publisher = '';
  580. $series_name = '';
  581. $pubplace = '';
  582. $miniref = '';
  583. $cross_refs = array();
  584. foreach ($properties as $type_id => $element) {
  585. $value = trim($element[0]);
  586. $name = $properties_list[$type_id];
  587. // populate our $pub_array for building a citation
  588. $pub_arr[$name] = $value;
  589. // remove properties that are stored in the pub table
  590. if ($name == "Volume") {
  591. $volume = $value;
  592. unset($properties[$type_id]);
  593. }
  594. elseif ($name == "Volume Title") {
  595. $volumetitle = $value;
  596. unset($properties[$type_id]);
  597. }
  598. elseif ($name == "Issue") {
  599. $issue = $value;
  600. unset($properties[$type_id]);
  601. }
  602. elseif ($name == "Pages") {
  603. $pages = $value;
  604. unset($properties[$type_id]);
  605. }
  606. elseif ($name == "Publisher") {
  607. $publisher = $value;
  608. unset($properties[$type_id]);
  609. }
  610. elseif ($name == "Series Name" or $name == "Journal Name" or $name == "Conference Name") {
  611. $series_name = $value;
  612. unset($properties[$type_id]);
  613. }
  614. elseif ($name == "Journal Country" or $name == "Published Location") {
  615. $pubplace = $value;
  616. // allow this property to go into the pubprop table so we don't loose info
  617. // so don't unset it. But it will also go into the pub.pubplace field
  618. }
  619. elseif ($name == "Publication Dbxref") {
  620. // we will add the cross-references to the pub_dbxref table
  621. // but we also want to keep the property in the pubprop table so don't unset it
  622. $cross_refs[] = $value;
  623. }
  624. }
  625. // generate a citation for this pub if one doesn't already exist
  626. if (!$node->uniquename and !array_key_exists($citation_id, $properties)) {
  627. $pub_type = tripal_get_cvterm(array('cvterm_id' => $node->type_id));
  628. $pub_arr['Title'] = $node->pubtitle;
  629. $pub_arr['Publication Type'][0] = $pub_type->name;
  630. $pub_arr['Year'] = $node->pyear;
  631. $node->uniquename = tripal_pub_create_citation($pub_arr);
  632. $properties[$citation_id][0] = $node->uniquename;
  633. }
  634. // insert the pub record
  635. $values = array(
  636. 'title' => $node->pubtitle,
  637. 'series_name' => substr($series_name, 0, 255),
  638. 'type_id' => $node->type_id,
  639. 'pyear' => $node->pyear,
  640. 'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
  641. 'uniquename' => $node->uniquename,
  642. 'volumetitle' => $volumetitle,
  643. 'volume' => $volume,
  644. 'issue' => $issue,
  645. 'pages' => $pages,
  646. 'miniref' => substr($miniref, 0, 255),
  647. 'publisher' => substr($publisher, 0, 255),
  648. 'pubplace' => substr($pubplace, 0, 255),
  649. );
  650. $pub = chado_insert_record('pub', $values);
  651. if (!$pub) {
  652. drupal_set_message("Error inserting publication", "error");
  653. watchdog('tripal_pub', "Error inserting publication", array(), WATCHDOG_ERROR);
  654. return;
  655. }
  656. $pub_id = $pub['pub_id'];
  657. // now add in the properties
  658. // Only adds in those not used in the pub record
  659. $details = array(
  660. 'property_table' => 'pubprop',
  661. 'base_table' => 'pub',
  662. 'foreignkey_name' => 'pub_id',
  663. 'foreignkey_value' => $pub_id
  664. );
  665. chado_update_node_form_properties($node, $details, $properties);
  666. // * Relationships Form *
  667. $details = array(
  668. 'relationship_table' => 'pub_relationship', // name of the _relationship table
  669. 'foreignkey_value' => $pub_id // value of the pub_id key
  670. );
  671. chado_update_node_form_relationships($node, $details);
  672. // add in any database cross-references
  673. foreach ($cross_refs as $index => $ref) {
  674. $dbxref = array();
  675. if(preg_match('/^(.*?):(.*?)$/', trim($ref), $matches)) {
  676. $dbxref['db_name'] = $matches[1];
  677. $dbxref['accession'] = $matches[2];
  678. }
  679. $success = chado_associate_dbxref('pub', $pub['pub_id'], $dbxref);
  680. if (!$success) {
  681. drupal_set_message("Error cannot add publication cross reference: $ref", "error");
  682. watchdog('tripal_pub', "Error cannot add publication cross reference: %ref",
  683. array('%ref' => $ref), WATCHDOG_ERROR);
  684. }
  685. }
  686. // * Additional DBxrefs Form *
  687. $details = array(
  688. 'linking_table' => 'pub_dbxref', // the name of your _dbxref table
  689. 'foreignkey_name' => 'pub_id', // the name of the key in your base table
  690. 'foreignkey_value' => $pub_id // the value of the pub_id key
  691. );
  692. chado_update_node_form_dbxrefs($node, $details);
  693. }
  694. else {
  695. $pub_id = $node->pub_id;
  696. }
  697. // Make sure the entry for this pub doesn't already exist in the
  698. // chado_pub table if it doesn't exist then we want to add it.
  699. $check_org_id = chado_get_id_from_nid('pub', $node->nid);
  700. if (!$check_org_id) {
  701. $record = new stdClass();
  702. $record->nid = $node->nid;
  703. $record->vid = $node->vid;
  704. $record->pub_id = $pub_id;
  705. drupal_write_record('chado_pub', $record);
  706. }
  707. }
  708. /**
  709. * Implements hook_update().
  710. *
  711. * The purpose of the function is to allow the module to take action when an edited node is being
  712. * updated. It updates any name changes to the database tables that werec reated upon registering a Publication.
  713. * As well, the database will be changed, so the user changed information will be saved to the database.
  714. *
  715. * @param $node
  716. * The node being updated
  717. *
  718. * @ingroup tripal_pub
  719. */
  720. function chado_pub_update($node) {
  721. $node->pubtitle = trim($node->pubtitle);
  722. $node->pyear = trim($node->pyear);
  723. $node->uniquename = trim($node->uniquename);
  724. $is_obsolete = $node->is_obsolete;
  725. $type_id = $node->type_id;
  726. // we need an array suitable for the tripal_pub_create_citation() function
  727. // to automatically generate a citation if a uniquename doesn't already exist
  728. $pub_arr = array();
  729. // get the publication ID for this publication
  730. $pub_id = chado_get_id_from_nid('pub', $node->nid) ;
  731. $properties = array(); // stores all of the properties we need to add
  732. $cross_refs = array(); // stores any cross references for this publication
  733. // get the properties from the form
  734. $properties = chado_retrieve_node_form_properties($node);
  735. // get the list of properties for easy lookup (without doing lots of database queries
  736. $properties_list = array();
  737. $sql = "
  738. SELECT DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
  739. FROM {cvtermpath} CVTP
  740. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  741. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  742. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  743. WHERE CV.name = 'tripal_pub' and
  744. (CVTO.name = 'Publication Details' or CVTS.name = 'Publication Type') and
  745. NOT CVTS.is_obsolete = 1
  746. ORDER BY CVTS.name ASC
  747. ";
  748. $prop_types = chado_query($sql);
  749. while ($prop = $prop_types->fetchObject()) {
  750. $properties_list[$prop->cvterm_id] = $prop->name;
  751. // The 'Citation' term is special because it serves
  752. // both as a property and as the uniquename for the
  753. // pub and we want it stored in both the pub table and the pubprop table
  754. if ($prop->name == 'Citation') {
  755. $citation_id = $prop->cvterm_id;
  756. if (!empty($node->uniquename)) {
  757. $properties[$citation_id][0] = $node->uniquename;
  758. }
  759. }
  760. }
  761. // iterate through all of the properties and remove those that really are
  762. // part of the pub table fields
  763. $volume = '';
  764. $volumetitle = '';
  765. $issue = '';
  766. $pages = '';
  767. $publisher = '';
  768. $series_name = '';
  769. $pubplace = '';
  770. $miniref = '';
  771. $cross_refs = array();
  772. foreach ($properties as $type_id => $element) {
  773. foreach ($element as $index => $value) {
  774. $name = $properties_list[$type_id];
  775. // populate our $pub_array for building a citation
  776. $pub_arr[$name] = $value;
  777. // remove properties that are stored in the pub table
  778. if ($name == "Volume") {
  779. $volume = $value;
  780. unset($properties[$type_id]);
  781. }
  782. elseif ($name == "Volume Title") {
  783. $volumetitle = $value;
  784. unset($properties[$type_id]);
  785. }
  786. elseif ($name == "Issue") {
  787. $issue = $value;
  788. unset($properties[$type_id]);
  789. }
  790. elseif ($name == "Pages") {
  791. $pages = $value;
  792. unset($properties[$type_id]);
  793. }
  794. elseif ($name == "Publisher") {
  795. $publisher = $value;
  796. unset($properties[$type_id]);
  797. }
  798. elseif ($name == "Journal Name" or $name == "Conference Name") {
  799. $series_name = $value;
  800. unset($properties[$type_id]);
  801. }
  802. elseif ($name == "Journal Country" or $name == "Published Location") {
  803. $pubplace = $value;
  804. // allow this property to go into the pubprop table so we don't loose info
  805. // so don't unset it. But it will also go into the pub.pubplace field
  806. }
  807. elseif ($name == "Publication Dbxref") {
  808. // we will add the cross-references to the pub_dbxref table
  809. // but we also want to keep the property in the pubprop table so don't unset it
  810. $cross_refs[] = $value;
  811. }
  812. }
  813. }
  814. // generate a citation for this pub if one doesn't already exist
  815. if (!$node->uniquename) {
  816. $pub_type = tripal_get_cvterm(array('cvterm_id' => $node->type_id));
  817. $pub_arr['Title'] = $node->pubtitle;
  818. $pub_arr['Publication Type'][0] = $pub_type->name;
  819. $pub_arr['Year'] = $node->pyear;
  820. $node->uniquename = tripal_pub_create_citation($pub_arr);
  821. $properties[$citation_id][0] = $node->uniquename;
  822. }
  823. // update the pub record
  824. $match = array(
  825. 'pub_id' => $pub_id,
  826. );
  827. $values = array(
  828. 'title' => $node->pubtitle,
  829. 'type_id' => $node->type_id,
  830. 'pyear' => $node->pyear,
  831. 'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
  832. 'uniquename' => $node->uniquename,
  833. 'series_name' => substr($series_name, 0, 255),
  834. 'volumetitle' => $volumetitle,
  835. 'volume' => $volume,
  836. 'issue' => $issue,
  837. 'pages' => $pages,
  838. 'miniref' => substr($miniref, 0, 255),
  839. 'publisher' => substr($publisher, 0, 255),
  840. 'pubplace' => substr($pubplace, 0, 255),
  841. );
  842. $status = chado_update_record('pub', $match, $values);
  843. if (!$status) {
  844. drupal_set_message("Error updating publication", "error");
  845. watchdog('tripal_pub', "Error updating publication", array(), WATCHDOG_ERROR);
  846. return;
  847. }
  848. // now add in the properties by first removing any the publication
  849. // already has and adding the ones we have
  850. $details = array(
  851. 'property_table' => 'pubprop',
  852. 'base_table' => 'pub',
  853. 'foreignkey_name' => 'pub_id',
  854. 'foreignkey_value'=> $pub_id
  855. );
  856. chado_update_node_form_properties($node, $details, $properties);
  857. // * Relationships Form *
  858. $details = array(
  859. 'relationship_table' => 'pub_relationship', // name of the _relationship table
  860. 'foreignkey_value' => $pub_id // value of the pub_id key
  861. );
  862. chado_update_node_form_relationships($node, $details);
  863. // add in any database cross-references after first removing
  864. chado_delete_record('pub_dbxref', array('pub_id' => $pub_id));
  865. foreach ($cross_refs as $index => $ref) {
  866. $dbxref = array();
  867. if(preg_match('/^(.*?):(.*?)$/', trim($ref), $matches)) {
  868. $dbxref['db_name'] = $matches[1];
  869. $dbxref['accession'] = $matches[2];
  870. }
  871. $success = chado_associate_dbxref('pub', $pub_id, $dbxref);
  872. if (!$success) {
  873. drupal_set_message("Error cannot add publication cross reference: $ref", "error");
  874. watchdog('tripal_pub', "Error cannot add publication cross reference: %ref",
  875. array('%ref' => $ref), WATCHDOG_ERROR);
  876. }
  877. }
  878. // * Additional DBxrefs Form *
  879. $details = array(
  880. 'linking_table' => 'pub_dbxref', // the name of your _dbxref table
  881. 'foreignkey_name' => 'pub_id', // the name of the key in your base table
  882. 'foreignkey_value' => $pub_id // the value of the pub_id key
  883. );
  884. chado_update_node_form_dbxrefs($node, $details);
  885. }
  886. /**
  887. * Implements hook_load().
  888. *
  889. * @param $node
  890. * The node that is to be loaded
  891. *
  892. * @return $node
  893. * The node with the information to be loaded into the database
  894. *
  895. * @ingroup tripal_pub
  896. */
  897. function chado_pub_load($nodes) {
  898. foreach ($nodes as $nid => $node) {
  899. // find the pub and add in the details
  900. $pub_id = chado_get_id_from_nid('pub', $nid);
  901. // if the nid does not have a matching record then skip this node.
  902. // this can happen with orphaned nodes.
  903. if (!$pub_id) {
  904. continue;
  905. }
  906. // get the pub
  907. $values = array('pub_id' => $pub_id);
  908. $pub = chado_generate_var('pub', $values);
  909. // expand the 'text' fields as those aren't included by default
  910. // and they really shouldn't be so large to cause problems
  911. $pub = chado_expand_var($pub, 'field', 'pub.title');
  912. $pub = chado_expand_var($pub, 'field', 'pub.volumetitle');
  913. $pub = chado_expand_var($pub, 'field', 'pub.uniquename');
  914. // set the URL path
  915. $nodes[$nid]->path = "pub/$pub_id";
  916. $nodes[$nid]->pub = $pub;
  917. // Now get the title
  918. $node->title = chado_get_node_title($node);
  919. }
  920. }
  921. /**
  922. * Implements hook_delete().
  923. *
  924. * This function takes a node and if the delete button has been chosen by the user, the publication
  925. * and it's details will be removed.Following,given the node-ID, the instance will be deleted from
  926. * the 'chado_pub' table.
  927. *
  928. * @parm $node
  929. * Then node to be deleted
  930. *
  931. * @ingroup tripal_pub
  932. */
  933. function chado_pub_delete(&$node) {
  934. $pub_id = chado_get_id_from_nid('pub', $node->nid);
  935. // if we don't have a pub id for this node then this isn't a node of
  936. // type chado_pub or the entry in the chado_pub table was lost.
  937. if (!$pub_id) {
  938. return;
  939. }
  940. // Remove data from {chado_pub}, {node} and {node_revision} tables of
  941. // drupal database
  942. $sql_del = "DELETE FROM {chado_pub} WHERE nid = :nid AND vid = :vid";
  943. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  944. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  945. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  946. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  947. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  948. // Remove data from pub and pubprop tables of chado database as well
  949. chado_query("DELETE FROM {pubprop} WHERE pub_id = :pub_id", array(':pub_id' => $pub_id));
  950. chado_query("DELETE FROM {pub} WHERE pub_id = :pub_id", array(':pub_id' => $pub_id));
  951. }
  952. /**
  953. * Implements hook_node_view(). Acts on all content types.
  954. *
  955. * @ingroup tripal_pub
  956. */
  957. function tripal_pub_node_view($node, $view_mode, $langcode) {
  958. switch ($node->type) {
  959. case 'chado_pub':
  960. // Show feature browser and counts
  961. if ($view_mode == 'full') {
  962. $node->content['tripal_pub_authors'] = array(
  963. '#theme' => 'tripal_pub_authors',
  964. '#node' => $node,
  965. '#tripal_toc_id' => 'authors',
  966. '#tripal_toc_title' => 'Author Details',
  967. );
  968. $node->content['tripal_pub_base'] = array(
  969. '#theme' => 'tripal_pub_base',
  970. '#node' => $node,
  971. '#tripal_toc_id' => 'base',
  972. '#tripal_toc_title' => 'Overview',
  973. '#weight' => -100,
  974. );
  975. $node->content['tripal_pub_featuremaps'] = array(
  976. '#theme' => 'tripal_pub_featuremaps',
  977. '#node' => $node,
  978. '#tripal_toc_id' => 'featuremaps',
  979. '#tripal_toc_title' => 'Maps',
  980. );
  981. $node->content['tripal_pub_features'] = array(
  982. '#theme' => 'tripal_pub_features',
  983. '#node' => $node,
  984. '#tripal_toc_id' => 'features',
  985. '#tripal_toc_title' => 'Features',
  986. );
  987. $node->content['tripal_pub_libraries'] = array(
  988. '#theme' => 'tripal_pub_libraries',
  989. '#node' => $node,
  990. '#tripal_toc_id' => 'libraries',
  991. '#tripal_toc_title' => 'Libraries',
  992. );
  993. $node->content['tripal_pub_projects'] = array(
  994. '#theme' => 'tripal_pub_projects',
  995. '#node' => $node,
  996. '#tripal_toc_id' => 'projects',
  997. '#tripal_toc_title' => 'Projects',
  998. );
  999. $node->content['tripal_pub_properties'] = array(
  1000. '#theme' => 'tripal_pub_properties',
  1001. '#node' => $node,
  1002. '#tripal_toc_id' => 'properties',
  1003. '#tripal_toc_title' => 'Properties',
  1004. );
  1005. $node->content['tripal_pub_references'] = array(
  1006. '#theme' => 'tripal_pub_references',
  1007. '#node' => $node,
  1008. '#tripal_toc_id' => 'references',
  1009. '#tripal_toc_title' => 'Cross References',
  1010. );
  1011. $node->content['tripal_pub_relationships'] = array(
  1012. '#theme' => 'tripal_pub_relationships',
  1013. '#node' => $node,
  1014. '#tripal_toc_id' => 'relationships',
  1015. '#tripal_toc_title' => 'Relationships',
  1016. );
  1017. $node->content['tripal_pub_stocks'] = array(
  1018. '#theme' => 'tripal_pub_stocks',
  1019. '#node' => $node,
  1020. '#tripal_toc_id' => 'stocks',
  1021. '#tripal_toc_title' => 'Stocks',
  1022. );
  1023. }
  1024. if ($view_mode == 'teaser') {
  1025. $node->content['tripal_pub_teaser'] = array(
  1026. '#theme' => 'tripal_pub_teaser',
  1027. '#node' => $node,
  1028. );
  1029. }
  1030. break;
  1031. }
  1032. }
  1033. /**
  1034. * Implements hook_node_insert(). Acts on all content types.
  1035. *
  1036. * We want the publications to always have a URL of http://[base url]/pub/[pub id]
  1037. * where [pub id] is the Chado publication ID. This will allow for easy linking
  1038. * into the publication without needing to know the node. Of course if you know the
  1039. * node that will still work too (e.g. http://[base url]/node/[node id]
  1040. * so the nodeapi function ensures that the URL path is set after insert or update
  1041. * of the node and when the node is loaded if it hasn't yet been set.
  1042. *
  1043. * @ingroup tripal_pub
  1044. */
  1045. function tripal_pub_node_insert($node) {
  1046. if ($node->type == 'chado_pub') {
  1047. // get the pub
  1048. $pub_id = chado_get_id_from_nid('pub', $node->nid);
  1049. $values = array('pub_id' => $pub_id);
  1050. $pub = chado_generate_var('pub', $values);
  1051. // expand the 'text' fields as those aren't included by default
  1052. // and they really shouldn't be so large to cause problems
  1053. $pub = chado_expand_var($pub, 'field', 'pub.title');
  1054. $pub = chado_expand_var($pub, 'field', 'pub.volumetitle');
  1055. $pub = chado_expand_var($pub, 'field', 'pub.uniquename');
  1056. $node->pub = $pub;
  1057. // Now get the title
  1058. $node->title = chado_get_node_title($node);
  1059. tripal_pub_set_pub_url($node, $pub_id);
  1060. }
  1061. }
  1062. /**
  1063. * Implements hook_node_load(). Acts on all content types.
  1064. *
  1065. * We want the publications to always have a URL of http://[base url]/pub/[pub id]
  1066. * where [pub id] is the Chado publication ID. This will allow for easy linking
  1067. * into the publication without needing to know the node. Of course if you know the
  1068. * node that will still work too (e.g. http://[base url]/node/[node id]
  1069. * so the nodeapi function ensures that the URL path is set after insert or update
  1070. * of the node and when the node is loaded if it hasn't yet been set.
  1071. *
  1072. * @ingroup tripal_pub
  1073. */
  1074. function tripal_pub_node_load($nodes, $types) {
  1075. if (count(array_intersect(array('chado_pub'), $types))) {
  1076. foreach ($nodes as $nid => $node) {
  1077. if ($node->type == 'chado_pub' and !property_exists($node, 'path')) {
  1078. $pub_id = chado_get_id_from_nid('pub', $node->nid);
  1079. $path = tripal_pub_set_pub_url($node, $pub_id);
  1080. }
  1081. }
  1082. }
  1083. }
  1084. /**
  1085. * Implements hook_node_update(). Acts on all content types.
  1086. *
  1087. * We want the publications to always have a URL of http://[base url]/pub/[pub id]
  1088. * where [pub id] is the Chado publication ID. This will allow for easy linking
  1089. * into the publication without needing to know the node. Of course if you know the
  1090. * node that will still work too (e.g. http://[base url]/node/[node id]
  1091. * so the nodeapi function ensures that the URL path is set after insert or update
  1092. * of the node and when the node is loaded if it hasn't yet been set.
  1093. *
  1094. * @ingroup tripal_pub
  1095. */
  1096. function tripal_pub_node_update($node) {
  1097. if ($node->type == 'chado_pub') {
  1098. // get the pub
  1099. $pub_id = chado_get_id_from_nid('pub', $node->nid);
  1100. $values = array('pub_id' => $pub_id);
  1101. $pub = chado_generate_var('pub', $values);
  1102. // expand the 'text' fields as those aren't included by default
  1103. // and they really shouldn't be so large to cause problems
  1104. $pub = chado_expand_var($pub, 'field', 'pub.title');
  1105. $pub = chado_expand_var($pub, 'field', 'pub.volumetitle');
  1106. $pub = chado_expand_var($pub, 'field', 'pub.uniquename');
  1107. $node->pub = $pub;
  1108. // Now get the title
  1109. $node->title = chado_get_node_title($node);
  1110. tripal_pub_set_pub_url($node, $pub_id);
  1111. }
  1112. }
  1113. /**
  1114. * Implements hook_node_presave(). Acts on all content types.
  1115. *
  1116. * @ingroup tripal_pub
  1117. */
  1118. function tripal_pub_node_presave($node) {
  1119. switch ($node->type) {
  1120. // This step is for setting the title for the Drupal node. This title
  1121. // is permanent and thus is created to be unique. Title changes provided
  1122. // by tokens are generated on the fly dynamically, but the node title
  1123. // seen in the content listing needs to be set here. Do not call
  1124. // the chado_get_node_title() function here to set the title as the node
  1125. // object isn't properly filled out and the function will fail.
  1126. case 'chado_pub':
  1127. // when syncing the details are not present in the $node object
  1128. // as they are when submitted via the form. Therefore, if we do
  1129. // not see any field values from the form, we assume this fucntion
  1130. // is being called for syncing, so we must set the title accordingly
  1131. if (property_exists($node, 'title')) {
  1132. // do nothing, the title is set
  1133. }
  1134. else if (property_exists($node, 'pub')) {
  1135. // in Drupal a node title can only be 255 characters so we truncate
  1136. // it just in case
  1137. $node->title = substr($node->pub->title, 0, 255);
  1138. }
  1139. break;
  1140. }
  1141. }
  1142. /**
  1143. * Implements [content_type]_chado_node_default_title_format().
  1144. *
  1145. * Defines a default title format for the Chado Node API to set the titles on
  1146. * Chado pub nodes based on chado fields.
  1147. */
  1148. function chado_pub_chado_node_default_title_format() {
  1149. return '[pub.title]';
  1150. }
  1151. /**
  1152. * Implements [content_type]_chado_node_sync_select_query().
  1153. *
  1154. * Adds a where clause to the query to exclude the NULL pub.
  1155. */
  1156. function chado_pub_chado_node_sync_select_query($query) {
  1157. $query['where_clauses']['title'][] = 'pub.title <> :pub_title_null';
  1158. $query['where_args']['title'][':pub_title_null'] = 'NULL';
  1159. return $query;
  1160. }