tripal_pub.chado_node.inc 44 KB

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