tripal_pub.chado_node.inc 45 KB

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