tripal_pub.chado_node.inc 45 KB

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