tripal_pub.chado_node.inc 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  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. // if this is a delete then don't validate
  314. if($node->op == 'Delete') {
  315. return;
  316. }
  317. // we are syncing if we do not have a node ID but we do have a pub_id. We don't
  318. // need to validate during syncing so just skip it.
  319. if (is_null($node->nid) and property_exists($node, 'pub_id') and $node->pub_id != 0) {
  320. return;
  321. }
  322. // get the submitted values
  323. $title = trim($node->pubtitle);
  324. $pyear = trim($node->pyear);
  325. $uniquename = trim($node->uniquename);
  326. $is_obsolete = $node->is_obsolete;
  327. $type_id = $node->type_id;
  328. $pub = array();
  329. // make sure the year is four digits
  330. if(!preg_match('/^\d{4}$/', $pyear)){
  331. form_set_error('pyear', t('The publication year should be a 4 digit year.'));
  332. return;
  333. }
  334. // get the type of publication
  335. $values = array('cvterm_id' => $type_id);
  336. $cvterm = chado_select_record('cvterm', array('name'), $values);
  337. if (count($cvterm) == 0) {
  338. $message = t('Invalid publication type.');
  339. form_set_error('type_id', $message);
  340. return;
  341. }
  342. // get the media name looking at the properties
  343. $series_name = '';
  344. $properties = chado_retrieve_node_form_properties($node);
  345. foreach ($properties as $key => $prop_values) {
  346. $values = array('cvterm_id' => $key);
  347. $prop_type = chado_select_record('cvterm', array('name'), $values);
  348. if ($prop_type[0]->name == 'Conference Name' or
  349. $prop_type[0]->name == 'Journal Name' or
  350. $prop_type[0]->name == 'Series Name') {
  351. $series_name = $prop_values[0];
  352. }
  353. if ($prop_type[0]->name == 'Citation') {
  354. $uniquename = $prop_values[0];
  355. }
  356. if (count($prop_values) == 1) {
  357. $pub[$prop_type[0]->name] = $prop_values[0];
  358. }
  359. else {
  360. $pub[$prop_type[0]->name] = $prop_values;
  361. }
  362. }
  363. // if the citation is missing then try to generate one
  364. if (!$uniquename) {
  365. $pub['Title'] = $title;
  366. $pub['Publication Type'][0] = $cvterm[0]->name;
  367. $pub['Year'] = $pyear;
  368. $uniquename = tripal_pub_create_citation($pub);
  369. if (!$uniquename) {
  370. form_set_error('uniquename', "Cannot automatically generate a citation for this publication type. Please add one manually.");
  371. }
  372. }
  373. $skip_duplicate_check = 0;
  374. // if this publication is a Patent then skip the validation below. Patents can have the title
  375. // name and year but be different
  376. if (strcmp($cvterm[0]->name,'Patent') == 0) {
  377. $skip_duplicate_check = 1;
  378. }
  379. // Validating for an update
  380. if (!is_null($node->nid)) {
  381. $pub_id = $node->pub_id;
  382. // check to see if a duplicate publication already exists
  383. if (!$skip_duplicate_check) {
  384. chado_pub_validate_check_duplicate($title, $pyear, $series_name, $cvterm[0], $pub_id);
  385. }
  386. chado_pub_validate_check_uniquename($uniquename, $pub_id);
  387. }
  388. // Validating for an insert
  389. else {
  390. chado_pub_validate_check_duplicate($title, $pyear, $series_name, $cvterm[0]);
  391. chado_pub_validate_check_uniquename($uniquename);
  392. }
  393. }
  394. /**
  395. * Validate the publication uniquename. To be called from hook_validate().
  396. *
  397. * @param $uniquename
  398. * The uniquename of the publication
  399. * @param $pub_id
  400. * If an update, provide the pub_id so we don't check for a matching
  401. * uniquename of the pub we are editing
  402. *
  403. * @ingroup tripal_pub
  404. */
  405. function chado_pub_validate_check_uniquename($uniquename, $pub_id = NULL) {
  406. // check to see if a pub exists with this uniquename
  407. $pub = tripal_get_publication(array('uniquename' => $uniquename));
  408. if ($pub) {
  409. // if a $pub_id is provided to the function then this is an update
  410. // if the pub_id's don't match then a different pub with the same
  411. // uniquename already exists.
  412. if ($pub->pub_id != $pub_id) {
  413. $message = t('A publication with this unique citation already exists.');
  414. form_set_error('uniquename', $message);
  415. }
  416. }
  417. }
  418. /**
  419. * Check for duplicate publications. To be called from hook_validate(). Sets
  420. * the form error if a duplicate
  421. *
  422. * @param $title
  423. * The title of the publication
  424. * @param $pyear
  425. * The year the publication was published
  426. * @param $series_name
  427. * The series name of the publication
  428. * @param $cvterm
  429. * The type of publication
  430. * @param $pub_id
  431. * the unique id of the publication
  432. *
  433. *
  434. * @ingroup tripal_pub
  435. */
  436. function chado_pub_validate_check_duplicate($title, $pyear, $series_name, $cvterm, $pub_id = NULL) {
  437. $pub_details = array(
  438. 'Title' => $title,
  439. 'Year' => $pyear,
  440. 'Series Name' => $series_name,
  441. 'Publication Type' => $cvterm->name,
  442. );
  443. // TODO: need to include the Publication Dbxref in the $pub_details as well
  444. $pub_ids = tripal_publication_exists($pub_details);
  445. // if we found only one publication and it is our publication then return, we're good.
  446. if (count($pub_ids) == 1 and in_array($pub_id, $pub_ids)) {
  447. return;
  448. }
  449. if (count($pub_ids) == 0) {
  450. // there is no match so return
  451. return;
  452. }
  453. // return an appropriate message based on the unique constraint settings
  454. $import_dups_check = variable_get('tripal_pub_import_duplicate_check', 'title_year_media');
  455. switch ($import_dups_check) {
  456. case 'title_year':
  457. $message = t('A publication with this title and publication year already exists.');
  458. form_set_error('pubtitle', $message);
  459. break;
  460. case 'title_year_type':
  461. $message = t('A publication with this title, type and publication year already exists.');
  462. form_set_error('pubtitle', $message);
  463. break;
  464. case 'title_year_media':
  465. $message = t('A publication with this title, media name (e.g. Journal Name) and publication year already exists.');
  466. form_set_error('pubtitle', $message);
  467. break;
  468. }
  469. }
  470. /**
  471. * Implement hook_node_access().
  472. *
  473. * This hook allows node modules to limit access to the node types they define.
  474. *
  475. * @param $node
  476. * The node on which the operation is to be performed, or, if it does not yet exist, the
  477. * type of node to be created
  478. *
  479. * @param $op
  480. * The operation to be performed
  481. *
  482. * @param $account
  483. * A user object representing the user for whom the operation is to be performed
  484. *
  485. * @return
  486. * If the permission for the specified operation is not set then return FALSE. If the
  487. * permission is set then return NULL as this allows other modules to disable
  488. * access. The only exception is when the $op == 'create'. We will always
  489. * return TRUE if the permission is set.
  490. *
  491. * @ingroup tripal_pub
  492. */
  493. function chado_pub_node_access($node, $op, $account) {
  494. $node_type = $node;
  495. if (is_object($node)) {
  496. $node_type = $node->type;
  497. }
  498. if($node_type == 'chado_pub') {
  499. if ($op == 'create') {
  500. if (!user_access('create chado_pub', $account)) {
  501. return NODE_ACCESS_DENY;
  502. }
  503. return NODE_ACCESS_ALLOW;
  504. }
  505. if ($op == 'update') {
  506. if (!user_access('edit chado_pub', $account)) {
  507. return NODE_ACCESS_DENY;
  508. }
  509. }
  510. if ($op == 'delete') {
  511. if (!user_access('delete chado_pub', $account)) {
  512. return NODE_ACCESS_DENY;
  513. }
  514. }
  515. if ($op == 'view') {
  516. if (!user_access('access chado_pub', $account)) {
  517. return NODE_ACCESS_DENY;
  518. }
  519. }
  520. return NODE_ACCESS_IGNORE;
  521. }
  522. }
  523. /**
  524. * Implements hook_insert().
  525. *
  526. * @ingroup tripal_pub
  527. */
  528. function chado_pub_insert($node) {
  529. $node->pubtitle = trim($node->pubtitle);
  530. $node->pyear = trim($node->pyear);
  531. $node->uniquename = trim($node->uniquename);
  532. $is_obsolete = $node->is_obsolete;
  533. $type_id = $node->type_id;
  534. // we need an array suitable for the tripal_pub_create_citation() function
  535. // to automatically generate a citation if a uniquename doesn't already exist
  536. $pub_arr = array();
  537. // if there is an pub_id in the $node object then this must be a sync so
  538. // we can skip adding the pub as it is already there, although
  539. // we do need to proceed with the rest of the insert
  540. if (!property_exists($node, 'pub_id')) {
  541. $properties = array(); // stores all of the properties we need to add
  542. $cross_refs = array(); // stores any cross references for this publication
  543. // get the properties from the form
  544. $properties = chado_retrieve_node_form_properties($node);
  545. // get the list of properties for easy lookup (without doing lots of database queries
  546. $properties_list = array();
  547. $sql = "
  548. SELECT CVTS.cvterm_id, CVTS.name, CVTS.definition
  549. FROM {cvtermpath} CVTP
  550. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  551. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  552. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  553. WHERE CV.name = 'tripal_pub' and CVTO.name = 'Publication Details' and
  554. NOT CVTS.is_obsolete = 1
  555. ORDER BY CVTS.name ASC
  556. ";
  557. $prop_types = chado_query($sql);
  558. while ($prop = $prop_types->fetchObject()) {
  559. $properties_list[$prop->cvterm_id] = $prop->name;
  560. // The 'Citation' term is special because it serves
  561. // both as a property and as the uniquename for the
  562. // pub and we want it stored in both the pub table and the pubprop table
  563. if ($prop->name == 'Citation') {
  564. $citation_id = $prop->cvterm_id;
  565. if (!empty($node->uniquename)) {
  566. $properties[$citation_id][0] = $node->uniquename;
  567. }
  568. }
  569. }
  570. // iterate through all of the properties and remove those that really are
  571. // part of the pub table fields
  572. $volume = '';
  573. $volumetitle = '';
  574. $issue = '';
  575. $pages = '';
  576. $publisher = '';
  577. $series_name = '';
  578. $pubplace = '';
  579. $miniref = '';
  580. $cross_refs = array();
  581. foreach ($properties as $type_id => $element) {
  582. $value = trim($element[0]);
  583. $name = $properties_list[$type_id];
  584. // populate our $pub_array for building a citation
  585. $pub_arr[$name] = $value;
  586. // remove properties that are stored in the pub table
  587. if ($name == "Volume") {
  588. $volume = $value;
  589. unset($properties[$type_id]);
  590. }
  591. elseif ($name == "Volume Title") {
  592. $volumetitle = $value;
  593. unset($properties[$type_id]);
  594. }
  595. elseif ($name == "Issue") {
  596. $issue = $value;
  597. unset($properties[$type_id]);
  598. }
  599. elseif ($name == "Pages") {
  600. $pages = $value;
  601. unset($properties[$type_id]);
  602. }
  603. elseif ($name == "Publisher") {
  604. $publisher = $value;
  605. unset($properties[$type_id]);
  606. }
  607. elseif ($name == "Series Name" or $name == "Journal Name" or $name == "Conference Name") {
  608. $series_name = $value;
  609. unset($properties[$type_id]);
  610. }
  611. elseif ($name == "Journal Country" or $name == "Published Location") {
  612. $pubplace = $value;
  613. // allow this property to go into the pubprop table so we don't loose info
  614. // so don't unset it. But it will also go into the pub.pubplace field
  615. }
  616. elseif ($name == "Publication Dbxref") {
  617. // we will add the cross-references to the pub_dbxref table
  618. // but we also want to keep the property in the pubprop table so don't unset it
  619. $cross_refs[] = $value;
  620. }
  621. }
  622. // generate a citation for this pub if one doesn't already exist
  623. if (!$node->uniquename and !array_key_exists($citation_id, $properties)) {
  624. $pub_type = tripal_get_cvterm(array('cvterm_id' => $node->type_id));
  625. $pub_arr['Title'] = $node->pubtitle;
  626. $pub_arr['Publication Type'][0] = $pub_type->name;
  627. $pub_arr['Year'] = $node->pyear;
  628. $node->uniquename = tripal_pub_create_citation($pub_arr);
  629. $properties[$citation_id][0] = $node->uniquename;
  630. }
  631. // insert the pub record
  632. $values = array(
  633. 'title' => $node->pubtitle,
  634. 'series_name' => substr($series_name, 0, 255),
  635. 'type_id' => $node->type_id,
  636. 'pyear' => $node->pyear,
  637. 'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
  638. 'uniquename' => $node->uniquename,
  639. 'volumetitle' => $volumetitle,
  640. 'volume' => $volume,
  641. 'issue' => $issue,
  642. 'pages' => $pages,
  643. 'miniref' => substr($miniref, 0, 255),
  644. 'publisher' => substr($publisher, 0, 255),
  645. 'pubplace' => substr($pubplace, 0, 255),
  646. );
  647. $pub = chado_insert_record('pub', $values);
  648. if (!$pub) {
  649. drupal_set_message("Error inserting publication", "error");
  650. watchdog('tripal_pub', "Error inserting publication", array(), WATCHDOG_ERROR);
  651. return;
  652. }
  653. $pub_id = $pub['pub_id'];
  654. // now add in the properties
  655. // Only adds in those not used in the pub record
  656. $details = array(
  657. 'property_table' => 'pubprop',
  658. 'base_table' => 'pub',
  659. 'foreignkey_name' => 'pub_id',
  660. 'foreignkey_value' => $pub_id
  661. );
  662. chado_update_node_form_properties($node, $details, $properties);
  663. // * Relationships Form *
  664. $details = array(
  665. 'relationship_table' => 'pub_relationship', // name of the _relationship table
  666. 'foreignkey_value' => $pub_id // value of the pub_id key
  667. );
  668. chado_update_node_form_relationships($node, $details);
  669. // add in any database cross-references
  670. foreach ($cross_refs as $index => $ref) {
  671. $dbxref = array();
  672. if(preg_match('/^(.*?):(.*?)$/', trim($ref), $matches)) {
  673. $dbxref['db_name'] = $matches[1];
  674. $dbxref['accession'] = $matches[2];
  675. }
  676. $success = chado_associate_dbxref('pub', $pub['pub_id'], $dbxref);
  677. if (!$success) {
  678. drupal_set_message("Error cannot add publication cross reference: $ref", "error");
  679. watchdog('tripal_pub', "Error cannot add publication cross reference: %ref",
  680. array('%ref' => $ref), WATCHDOG_ERROR);
  681. }
  682. }
  683. // * Additional DBxrefs Form *
  684. $details = array(
  685. 'linking_table' => 'pub_dbxref', // the name of your _dbxref table
  686. 'foreignkey_name' => 'pub_id', // the name of the key in your base table
  687. 'foreignkey_value' => $pub_id // the value of the pub_id key
  688. );
  689. chado_update_node_form_dbxrefs($node, $details);
  690. }
  691. else {
  692. $pub_id = $node->pub_id;
  693. }
  694. // Make sure the entry for this pub doesn't already exist in the
  695. // chado_pub table if it doesn't exist then we want to add it.
  696. $check_org_id = chado_get_id_from_nid('pub', $node->nid);
  697. if (!$check_org_id) {
  698. $record = new stdClass();
  699. $record->nid = $node->nid;
  700. $record->vid = $node->vid;
  701. $record->pub_id = $pub_id;
  702. drupal_write_record('chado_pub', $record);
  703. }
  704. }
  705. /**
  706. * Implements hook_update().
  707. *
  708. * The purpose of the function is to allow the module to take action when an edited node is being
  709. * updated. It updates any name changes to the database tables that werec reated upon registering a Publication.
  710. * As well, the database will be changed, so the user changed information will be saved to the database.
  711. *
  712. * @param $node
  713. * The node being updated
  714. *
  715. * @ingroup tripal_pub
  716. */
  717. function chado_pub_update($node) {
  718. $node->pubtitle = trim($node->pubtitle);
  719. $node->pyear = trim($node->pyear);
  720. $node->uniquename = trim($node->uniquename);
  721. $is_obsolete = $node->is_obsolete;
  722. $type_id = $node->type_id;
  723. // we need an array suitable for the tripal_pub_create_citation() function
  724. // to automatically generate a citation if a uniquename doesn't already exist
  725. $pub_arr = array();
  726. // get the publication ID for this publication
  727. $pub_id = chado_get_id_from_nid('pub', $node->nid) ;
  728. $properties = array(); // stores all of the properties we need to add
  729. $cross_refs = array(); // stores any cross references for this publication
  730. // get the properties from the form
  731. $properties = chado_retrieve_node_form_properties($node);
  732. // get the list of properties for easy lookup (without doing lots of database queries
  733. $properties_list = array();
  734. $sql = "
  735. SELECT DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
  736. FROM {cvtermpath} CVTP
  737. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  738. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  739. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  740. WHERE CV.name = 'tripal_pub' and
  741. (CVTO.name = 'Publication Details' or CVTS.name = 'Publication Type') and
  742. NOT CVTS.is_obsolete = 1
  743. ORDER BY CVTS.name ASC
  744. ";
  745. $prop_types = chado_query($sql);
  746. while ($prop = $prop_types->fetchObject()) {
  747. $properties_list[$prop->cvterm_id] = $prop->name;
  748. // The 'Citation' term is special because it serves
  749. // both as a property and as the uniquename for the
  750. // pub and we want it stored in both the pub table and the pubprop table
  751. if ($prop->name == 'Citation') {
  752. $citation_id = $prop->cvterm_id;
  753. if (!empty($node->uniquename)) {
  754. $properties[$citation_id][0] = $node->uniquename;
  755. }
  756. }
  757. }
  758. // iterate through all of the properties and remove those that really are
  759. // part of the pub table fields
  760. $volume = '';
  761. $volumetitle = '';
  762. $issue = '';
  763. $pages = '';
  764. $publisher = '';
  765. $series_name = '';
  766. $pubplace = '';
  767. $miniref = '';
  768. $cross_refs = array();
  769. foreach ($properties as $type_id => $element) {
  770. foreach ($element as $index => $value) {
  771. $name = $properties_list[$type_id];
  772. // populate our $pub_array for building a citation
  773. $pub_arr[$name] = $value;
  774. // remove properties that are stored in the pub table
  775. if ($name == "Volume") {
  776. $volume = $value;
  777. unset($properties[$type_id]);
  778. }
  779. elseif ($name == "Volume Title") {
  780. $volumetitle = $value;
  781. unset($properties[$type_id]);
  782. }
  783. elseif ($name == "Issue") {
  784. $issue = $value;
  785. unset($properties[$type_id]);
  786. }
  787. elseif ($name == "Pages") {
  788. $pages = $value;
  789. unset($properties[$type_id]);
  790. }
  791. elseif ($name == "Publisher") {
  792. $publisher = $value;
  793. unset($properties[$type_id]);
  794. }
  795. elseif ($name == "Journal Name" or $name == "Conference Name") {
  796. $series_name = $value;
  797. unset($properties[$type_id]);
  798. }
  799. elseif ($name == "Journal Country" or $name == "Published Location") {
  800. $pubplace = $value;
  801. // allow this property to go into the pubprop table so we don't loose info
  802. // so don't unset it. But it will also go into the pub.pubplace field
  803. }
  804. elseif ($name == "Publication Dbxref") {
  805. // we will add the cross-references to the pub_dbxref table
  806. // but we also want to keep the property in the pubprop table so don't unset it
  807. $cross_refs[] = $value;
  808. }
  809. }
  810. }
  811. // generate a citation for this pub if one doesn't already exist
  812. if (!$node->uniquename) {
  813. $pub_type = tripal_get_cvterm(array('cvterm_id' => $node->type_id));
  814. $pub_arr['Title'] = $node->pubtitle;
  815. $pub_arr['Publication Type'][0] = $pub_type->name;
  816. $pub_arr['Year'] = $node->pyear;
  817. $node->uniquename = tripal_pub_create_citation($pub_arr);
  818. $properties[$citation_id][0] = $node->uniquename;
  819. }
  820. // update the pub record
  821. $match = array(
  822. 'pub_id' => $pub_id,
  823. );
  824. $values = array(
  825. 'title' => $node->pubtitle,
  826. 'type_id' => $node->type_id,
  827. 'pyear' => $node->pyear,
  828. 'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
  829. 'uniquename' => $node->uniquename,
  830. 'series_name' => substr($series_name, 0, 255),
  831. 'volumetitle' => $volumetitle,
  832. 'volume' => $volume,
  833. 'issue' => $issue,
  834. 'pages' => $pages,
  835. 'miniref' => substr($miniref, 0, 255),
  836. 'publisher' => substr($publisher, 0, 255),
  837. 'pubplace' => substr($pubplace, 0, 255),
  838. );
  839. $status = chado_update_record('pub', $match, $values);
  840. if (!$status) {
  841. drupal_set_message("Error updating publication", "error");
  842. watchdog('tripal_pub', "Error updating publication", array(), WATCHDOG_ERROR);
  843. return;
  844. }
  845. // now add in the properties by first removing any the publication
  846. // already has and adding the ones we have
  847. $details = array(
  848. 'property_table' => 'pubprop',
  849. 'base_table' => 'pub',
  850. 'foreignkey_name' => 'pub_id',
  851. 'foreignkey_value'=> $pub_id
  852. );
  853. chado_update_node_form_properties($node, $details, $properties);
  854. // * Relationships Form *
  855. $details = array(
  856. 'relationship_table' => 'pub_relationship', // name of the _relationship table
  857. 'foreignkey_value' => $pub_id // value of the pub_id key
  858. );
  859. chado_update_node_form_relationships($node, $details);
  860. // add in any database cross-references after first removing
  861. chado_delete_record('pub_dbxref', array('pub_id' => $pub_id));
  862. foreach ($cross_refs as $index => $ref) {
  863. $dbxref = array();
  864. if(preg_match('/^(.*?):(.*?)$/', trim($ref), $matches)) {
  865. $dbxref['db_name'] = $matches[1];
  866. $dbxref['accession'] = $matches[2];
  867. }
  868. $success = chado_associate_dbxref('pub', $pub_id, $dbxref);
  869. if (!$success) {
  870. drupal_set_message("Error cannot add publication cross reference: $ref", "error");
  871. watchdog('tripal_pub', "Error cannot add publication cross reference: %ref",
  872. array('%ref' => $ref), WATCHDOG_ERROR);
  873. }
  874. }
  875. // * Additional DBxrefs Form *
  876. $details = array(
  877. 'linking_table' => 'pub_dbxref', // the name of your _dbxref table
  878. 'foreignkey_name' => 'pub_id', // the name of the key in your base table
  879. 'foreignkey_value' => $pub_id // the value of the pub_id key
  880. );
  881. chado_update_node_form_dbxrefs($node, $details);
  882. }
  883. /**
  884. * Implements hook_load().
  885. *
  886. * @param $node
  887. * The node that is to be loaded
  888. *
  889. * @return $node
  890. * The node with the information to be loaded into the database
  891. *
  892. * @ingroup tripal_pub
  893. */
  894. function chado_pub_load($nodes) {
  895. foreach ($nodes as $nid => $node) {
  896. // find the pub and add in the details
  897. $pub_id = chado_get_id_from_nid('pub', $nid);
  898. // if the nid does not have a matching record then skip this node.
  899. // this can happen with orphaned nodes.
  900. if (!$pub_id) {
  901. continue;
  902. }
  903. // get the pub
  904. $values = array('pub_id' => $pub_id);
  905. $pub = chado_generate_var('pub', $values);
  906. // expand the 'text' fields as those aren't included by default
  907. // and they really shouldn't be so large to cause problems
  908. $pub = chado_expand_var($pub, 'field', 'pub.title');
  909. $pub = chado_expand_var($pub, 'field', 'pub.volumetitle');
  910. $pub = chado_expand_var($pub, 'field', 'pub.uniquename');
  911. // set the URL path
  912. $nodes[$nid]->path = "pub/$pub_id";
  913. $nodes[$nid]->pub = $pub;
  914. // Now get the title
  915. $node->title = chado_get_node_title($node);
  916. }
  917. }
  918. /**
  919. * Implements hook_delete().
  920. *
  921. * This function takes a node and if the delete button has been chosen by the user, the publication
  922. * and it's details will be removed.Following,given the node-ID, the instance will be deleted from
  923. * the 'chado_pub' table.
  924. *
  925. * @parm $node
  926. * Then node to be deleted
  927. *
  928. * @ingroup tripal_pub
  929. */
  930. function chado_pub_delete(&$node) {
  931. $pub_id = chado_get_id_from_nid('pub', $node->nid);
  932. // if we don't have a pub id for this node then this isn't a node of
  933. // type chado_pub or the entry in the chado_pub table was lost.
  934. if (!$pub_id) {
  935. return;
  936. }
  937. // Remove data from {chado_pub}, {node} and {node_revision} tables of
  938. // drupal database
  939. $sql_del = "DELETE FROM {chado_pub} WHERE nid = :nid AND vid = :vid";
  940. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  941. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  942. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  943. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  944. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  945. // Remove data from pub and pubprop tables of chado database as well
  946. chado_query("DELETE FROM {pubprop} WHERE pub_id = :pub_id", array(':pub_id' => $pub_id));
  947. chado_query("DELETE FROM {pub} WHERE pub_id = :pub_id", array(':pub_id' => $pub_id));
  948. }
  949. /**
  950. * Implements hook_node_view(). Acts on all content types.
  951. *
  952. * @ingroup tripal_pub
  953. */
  954. function tripal_pub_node_view($node, $view_mode, $langcode) {
  955. switch ($node->type) {
  956. case 'chado_pub':
  957. // Show feature browser and counts
  958. if ($view_mode == 'full') {
  959. $node->content['tripal_pub_authors'] = array(
  960. '#theme' => 'tripal_pub_authors',
  961. '#node' => $node,
  962. '#tripal_toc_id' => 'authors',
  963. '#tripal_toc_title' => 'Author Details',
  964. );
  965. $node->content['tripal_pub_base'] = array(
  966. '#theme' => 'tripal_pub_base',
  967. '#node' => $node,
  968. '#tripal_toc_id' => 'base',
  969. '#tripal_toc_title' => 'Overview',
  970. '#weight' => -100,
  971. );
  972. $node->content['tripal_pub_featuremaps'] = array(
  973. '#theme' => 'tripal_pub_featuremaps',
  974. '#node' => $node,
  975. '#tripal_toc_id' => 'featuremaps',
  976. '#tripal_toc_title' => 'Maps',
  977. );
  978. $node->content['tripal_pub_features'] = array(
  979. '#theme' => 'tripal_pub_features',
  980. '#node' => $node,
  981. '#tripal_toc_id' => 'features',
  982. '#tripal_toc_title' => 'Features',
  983. );
  984. $node->content['tripal_pub_libraries'] = array(
  985. '#theme' => 'tripal_pub_libraries',
  986. '#node' => $node,
  987. '#tripal_toc_id' => 'libraries',
  988. '#tripal_toc_title' => 'Libraries',
  989. );
  990. $node->content['tripal_pub_projects'] = array(
  991. '#theme' => 'tripal_pub_projects',
  992. '#node' => $node,
  993. '#tripal_toc_id' => 'projects',
  994. '#tripal_toc_title' => 'Projects',
  995. );
  996. $node->content['tripal_pub_properties'] = array(
  997. '#theme' => 'tripal_pub_properties',
  998. '#node' => $node,
  999. '#tripal_toc_id' => 'properties',
  1000. '#tripal_toc_title' => 'Properties',
  1001. );
  1002. $node->content['tripal_pub_references'] = array(
  1003. '#theme' => 'tripal_pub_references',
  1004. '#node' => $node,
  1005. '#tripal_toc_id' => 'references',
  1006. '#tripal_toc_title' => 'Cross References',
  1007. );
  1008. $node->content['tripal_pub_relationships'] = array(
  1009. '#theme' => 'tripal_pub_relationships',
  1010. '#node' => $node,
  1011. '#tripal_toc_id' => 'relationships',
  1012. '#tripal_toc_title' => 'Relationships',
  1013. );
  1014. $node->content['tripal_pub_stocks'] = array(
  1015. '#theme' => 'tripal_pub_stocks',
  1016. '#node' => $node,
  1017. '#tripal_toc_id' => 'stocks',
  1018. '#tripal_toc_title' => 'Stocks',
  1019. );
  1020. }
  1021. if ($view_mode == 'teaser') {
  1022. $node->content['tripal_pub_teaser'] = array(
  1023. '#theme' => 'tripal_pub_teaser',
  1024. '#node' => $node,
  1025. );
  1026. }
  1027. break;
  1028. }
  1029. }
  1030. /**
  1031. * Implements hook_node_insert(). Acts on all content types.
  1032. *
  1033. * We want the publications to always have a URL of http://[base url]/pub/[pub id]
  1034. * where [pub id] is the Chado publication ID. This will allow for easy linking
  1035. * into the publication without needing to know the node. Of course if you know the
  1036. * node that will still work too (e.g. http://[base url]/node/[node id]
  1037. * so the nodeapi function ensures that the URL path is set after insert or update
  1038. * of the node and when the node is loaded if it hasn't yet been set.
  1039. *
  1040. * @ingroup tripal_pub
  1041. */
  1042. function tripal_pub_node_insert($node) {
  1043. if ($node->type == 'chado_pub') {
  1044. // get the pub
  1045. $pub_id = chado_get_id_from_nid('pub', $node->nid);
  1046. $values = array('pub_id' => $pub_id);
  1047. $pub = chado_generate_var('pub', $values);
  1048. // expand the 'text' fields as those aren't included by default
  1049. // and they really shouldn't be so large to cause problems
  1050. $pub = chado_expand_var($pub, 'field', 'pub.title');
  1051. $pub = chado_expand_var($pub, 'field', 'pub.volumetitle');
  1052. $pub = chado_expand_var($pub, 'field', 'pub.uniquename');
  1053. $node->pub = $pub;
  1054. // Now get the title
  1055. $node->title = chado_get_node_title($node);
  1056. tripal_pub_set_pub_url($node, $pub_id);
  1057. }
  1058. }
  1059. /**
  1060. * Implements hook_node_load(). Acts on all content types.
  1061. *
  1062. * We want the publications to always have a URL of http://[base url]/pub/[pub id]
  1063. * where [pub id] is the Chado publication ID. This will allow for easy linking
  1064. * into the publication without needing to know the node. Of course if you know the
  1065. * node that will still work too (e.g. http://[base url]/node/[node id]
  1066. * so the nodeapi function ensures that the URL path is set after insert or update
  1067. * of the node and when the node is loaded if it hasn't yet been set.
  1068. *
  1069. * @ingroup tripal_pub
  1070. */
  1071. function tripal_pub_node_load($nodes, $types) {
  1072. if (count(array_intersect(array('chado_pub'), $types))) {
  1073. foreach ($nodes as $nid => $node) {
  1074. if ($node->type == 'chado_pub' and !property_exists($node, 'path')) {
  1075. $pub_id = chado_get_id_from_nid('pub', $node->nid);
  1076. $path = tripal_pub_set_pub_url($node, $pub_id);
  1077. }
  1078. }
  1079. }
  1080. }
  1081. /**
  1082. * Implements hook_node_update(). Acts on all content types.
  1083. *
  1084. * We want the publications to always have a URL of http://[base url]/pub/[pub id]
  1085. * where [pub id] is the Chado publication ID. This will allow for easy linking
  1086. * into the publication without needing to know the node. Of course if you know the
  1087. * node that will still work too (e.g. http://[base url]/node/[node id]
  1088. * so the nodeapi function ensures that the URL path is set after insert or update
  1089. * of the node and when the node is loaded if it hasn't yet been set.
  1090. *
  1091. * @ingroup tripal_pub
  1092. */
  1093. function tripal_pub_node_update($node) {
  1094. if ($node->type == 'chado_pub') {
  1095. // get the pub
  1096. $pub_id = chado_get_id_from_nid('pub', $node->nid);
  1097. $values = array('pub_id' => $pub_id);
  1098. $pub = chado_generate_var('pub', $values);
  1099. // expand the 'text' fields as those aren't included by default
  1100. // and they really shouldn't be so large to cause problems
  1101. $pub = chado_expand_var($pub, 'field', 'pub.title');
  1102. $pub = chado_expand_var($pub, 'field', 'pub.volumetitle');
  1103. $pub = chado_expand_var($pub, 'field', 'pub.uniquename');
  1104. $node->pub = $pub;
  1105. // Now get the title
  1106. $node->title = chado_get_node_title($node);
  1107. tripal_pub_set_pub_url($node, $pub_id);
  1108. }
  1109. }
  1110. /**
  1111. * Implements hook_node_presave(). Acts on all content types.
  1112. *
  1113. * @ingroup tripal_pub
  1114. */
  1115. function tripal_pub_node_presave($node) {
  1116. switch ($node->type) {
  1117. // This step is for setting the title for the Drupal node. This title
  1118. // is permanent and thus is created to be unique. Title changes provided
  1119. // by tokens are generated on the fly dynamically, but the node title
  1120. // seen in the content listing needs to be set here. Do not call
  1121. // the chado_get_node_title() function here to set the title as the node
  1122. // object isn't properly filled out and the function will fail.
  1123. case 'chado_pub':
  1124. // when syncing the details are not present in the $node object
  1125. // as they are when submitted via the form. Therefore, if we do
  1126. // not see any field values from the form, we assume this fucntion
  1127. // is being called for syncing, so we must set the title accordingly
  1128. if (property_exists($node, 'title')) {
  1129. // do nothing, the title is set
  1130. }
  1131. else if (property_exists($node, 'pub')) {
  1132. // in Drupal a node title can only be 255 characters so we truncate
  1133. // it just in case
  1134. $node->title = substr($node->pub->title, 0, 255);
  1135. }
  1136. break;
  1137. }
  1138. }
  1139. /**
  1140. * Implements [content_type]_chado_node_default_title_format().
  1141. *
  1142. * Defines a default title format for the Chado Node API to set the titles on
  1143. * Chado pub nodes based on chado fields.
  1144. */
  1145. function chado_pub_chado_node_default_title_format() {
  1146. return '[pub.title]';
  1147. }
  1148. /**
  1149. * Implements [content_type]_chado_node_sync_select_query().
  1150. *
  1151. * Adds a where clause to the query to exclude the NULL pub.
  1152. */
  1153. function chado_pub_chado_node_sync_select_query($query) {
  1154. $query['where_clauses']['title'][] = 'pub.title <> :pub_title_null';
  1155. $query['where_args']['title'][':pub_title_null'] = 'NULL';
  1156. return $query;
  1157. }