tripal_example.chado_node.inc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. <?php
  2. /**
  3. * @file
  4. * This file should contain all Drupal hooks for interacting with nodes.
  5. *
  6. */
  7. /**
  8. * Implementation of hook_node_info().
  9. *
  10. * This hook provides information to drupal about any node types that are being
  11. * created by this module. If your module does not create any node types then
  12. * this function is not required.
  13. *
  14. * @ingroup tripal_example
  15. */
  16. function tripal_example_node_info() {
  17. $nodes = array();
  18. // EXPLANATION: this array describes all of the node types that are created
  19. // by this module. For many Tripal modules (e.g. tripal_example, tripal_stock,
  20. // tripal_library, tripal_pub, etc.) new node types are created. It is
  21. // customary to name all new node types that interact with data in Chado
  22. // with a 'chado_' prefix.
  23. $nodes['chado_example'] = array(
  24. 'name' => t('Example'),
  25. 'base' => 'chado_example',
  26. 'description' => t('A record from the fake chado example table'),
  27. 'has_title' => TRUE,
  28. 'locked' => TRUE,
  29. // EXPLANATION: This section of the node type array specifies how Tripal will sync the node
  30. // types with data in Chado. When Drupal creates a node it has no way of
  31. // coordinating which node belongs to which record in Chado. Therefore,
  32. // Tripal maintains tables in the Drupal schema that maps Drupal nodes
  33. // to recrords in Chado. Syncing is the process of creating Drupal nodes
  34. // and linking them to the appropriate record.
  35. 'chado_node_api' => array(
  36. 'base_table' => 'example', // the base table name (e.g. example, example, contact)
  37. 'hook_prefix' => 'chado_example',// the node type hook prefix
  38. 'record_type_title' => array(
  39. 'singular' => t('Example'), // how to refer to the record
  40. 'plural' => t('Examples') // how to refer to the record in plurals
  41. ),
  42. 'sync_filters' => array(
  43. 'type_id' => TRUE, // if the record has a type_id set to TRUE
  44. 'organism_id' => TRUE // if the record has an organism_id set to TRUE
  45. ),
  46. )
  47. );
  48. return $nodes;
  49. }
  50. /**
  51. * Implement hook_access(). This hook provides instructions to
  52. * drupal for which users can access the custom content types
  53. * created in the function above. The available permissions
  54. * are set in the chado_example_permissions() hook in the
  55. * tripal_example.module file. This hook is not needed
  56. * if no node types were defined in the hook_node_info() hook.
  57. *
  58. * @return
  59. * This function should return null if it does not specificially
  60. * deny access. This allows for other mechanisms to to deny
  61. * or reject access. If the return value is TRUE then access
  62. * is granted regardless of any other rules that might be implemented
  63. * by other modules.
  64. */
  65. function chado_example_node_access($node, $op, $account) {
  66. $node_type = $node;
  67. if (is_object($node)) {
  68. $node_type = $node->type;
  69. }
  70. // EXPLANATION: in the tripal_example_permissions() function we
  71. // created the permission types that are used here to check for
  72. // access permissions to the 'chado_exmaple' node type.
  73. if($node_type == 'chado_example') {
  74. if ($op == 'create') {
  75. if (!user_access('create chado_example content', $account)) {
  76. return NODE_ACCESS_DENY;
  77. }
  78. return NODE_ACCESS_ALLOW;
  79. }
  80. if ($op == 'update') {
  81. if (!user_access('edit chado_example content', $account)) {
  82. return NODE_ACCESS_DENY;
  83. }
  84. }
  85. if ($op == 'delete') {
  86. if (!user_access('delete chado_example content', $account)) {
  87. return NODE_ACCESS_DENY;
  88. }
  89. }
  90. if ($op == 'view') {
  91. if (!user_access('access chado_example content', $account)) {
  92. return NODE_ACCESS_DENY;
  93. }
  94. }
  95. }
  96. return NODE_ACCESS_IGNORE;
  97. }
  98. /**
  99. * Implementation of hook_form()
  100. *
  101. * Creates the form for editing or inserting a record
  102. *
  103. * @ingroup tripal_example
  104. */
  105. function chado_example_form($node, &$form_state) {
  106. // EXPLANATION: This function should construct a form array that is used
  107. // by Drupal to contruct a form for inserting or editing our new node type.
  108. // See this page for information about the Form API:
  109. // https://api.drupal.org/api/drupal/includes!form.inc/group/form_api/7
  110. //
  111. // The code below is laid out in the following order
  112. // 1) Set default values
  113. // 2) Add form elements used by this node type
  114. // 3) Use the Tripal API to add form elemetns for properties,
  115. // dbxref's and relationships
  116. //
  117. // For the example code below we assume that the fake 'example' table
  118. // only has a uniquename, organism_id, type_id and example_id.
  119. $form = array();
  120. // Default values can come in the following ways:
  121. //
  122. // 1) as elements of the $node object. This occurs when editing an existing example
  123. // 2) in the $form_state['values'] array which occurs on a failed validation or
  124. // ajax callbacks from non submit form elements
  125. // 3) in the $form_state['input'[ array which occurs on ajax callbacks from submit
  126. // form elements and the form is being rebuilt
  127. //
  128. // set form field defaults
  129. // SET FORM DEFAULTS
  130. //---------------------------------------------
  131. $example = null; // holds the example object record
  132. $example_id = null; // when editing an example record we'll have an example_id
  133. // initialize the defaults for the form fields
  134. $uniquename = '';
  135. $example_type = '';
  136. $organism_id = '';
  137. $description = '';
  138. // if we are editing an existing node then the 'example' record from Chado
  139. // is already part of the node, so we set the defaults from that object
  140. if (property_exists($node, 'example')) {
  141. $example = $node->example;
  142. $example_id = $example->example_id;
  143. $uniquename = $example->uniquename;
  144. $description = $example->description;
  145. $organism_id = $example->organism_id;
  146. // keep track of the example id
  147. $form['example_id'] = array(
  148. '#type' => 'value',
  149. '#value' => $example_id,
  150. );
  151. }
  152. // if we are re constructing the form from a failed validation or ajax callback
  153. // then use the $form_state['values'] values
  154. if (array_key_exists('values', $form_state)) {
  155. $uniquename = $form_state['values']['uniquename'];
  156. $example_type = $form_state['values']['example_type'];
  157. $description = $form_state['values']['description'];
  158. $organism_id = $form_state['values']['organism_id'];
  159. }
  160. // if we are re building the form from after submission (from ajax call) then
  161. // the values are in the $form_state['input'] array
  162. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  163. $uniquename = $form_state['input']['uniquename'];
  164. $example_type = $form_state['input']['example_type'];
  165. $organism_id = $form_state['input']['organism_id'];
  166. }
  167. // FORM ELEMENTS
  168. //---------------------------------------------
  169. $form['uniquename'] = array(
  170. '#type' => 'textfield',
  171. '#title' => t('Unique Name'),
  172. '#required' => TRUE,
  173. '#default_value' => $uniquename,
  174. '#description' => t('Enter a unique name for this example. This name must be unique.'),
  175. '#maxlength' => 255
  176. );
  177. // for the type_id we want to use the default vocabulary so that this
  178. // field can have autocomplete functionality
  179. $type_cv = tripal_get_default_cv('example', 'type_id');
  180. $cv_id = $type_cv->cv_id;
  181. $form['example_type'] = array(
  182. '#title' => t('Example Type'),
  183. '#type' => 'textfield',
  184. '#description' => t("Choose the example type (e.g. Test Type)."),
  185. '#required' => TRUE,
  186. '#default_value' => $example_type,
  187. '#autocomplete_path' => "admin/tripal/chado/tripal_cv/cvterm/auto_name/$cv_id",
  188. );
  189. // add a select box of organisms
  190. $organisms = tripal_get_organism_select_options();
  191. $form['organism_id'] = array(
  192. '#title' => t('Organism'),
  193. '#type' => t('select'),
  194. '#description' => t("Choose the organism with which this example is associated"),
  195. '#required' => TRUE,
  196. '#default_value' => $organism_id,
  197. '#options' => $organisms,
  198. );
  199. $form['description'] = array(
  200. '#type' => 'textarea',
  201. '#title' => t('Description'),
  202. '#required' => TRUE,
  203. '#default_value' => $description,
  204. '#description' => t('Enter a description for this example.'),
  205. );
  206. // PROPERTIES FORM
  207. //---------------------------------------------
  208. // If there is a exampleprop table and you want to allow users to add/remove entries
  209. // from it through your node form then add this section to your own node form
  210. $prop_cv = tripal_get_default_cv('exampleprop', 'type_id');
  211. $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
  212. $details = array(
  213. 'property_table' => 'exampleprop', // the name of the prop table
  214. 'chado_id' => $example_id, // the value of example_id for this record
  215. 'cv_id' => $cv_id // the cv.cv_id of the cv governing exampleprop.type_id
  216. );
  217. // Adds the form elements to your current form
  218. chado_add_node_form_properties($form, $form_state, $details);
  219. // ADDITIONAL DBXREFS FORM
  220. //---------------------------------------------
  221. // If there is a example_dbxref table and you want to allow users to add/remove entries
  222. // from it through your node form then add this section to your own node form
  223. $details = array(
  224. 'linking_table' => 'example_dbxref', // the name of the _dbxref table
  225. 'base_foreign_key' => 'example_id', // the name of the key in your base chado table
  226. 'base_key_value' => $example_id // the value of example_id for this record
  227. );
  228. // Adds the form elements to your current form
  229. chado_add_node_form_dbxrefs($form, $form_state, $details);
  230. // RELATIONSHIPS FORM
  231. //---------------------------------------------
  232. // If there is a example_relationship table and you want to allow users to add/remove entries
  233. // from it through your node form then add this section to your own node form
  234. $rels_cv = tripal_get_default_cv('example_relationship', 'type_id');
  235. $cv_id = $rels_cv ? $rels_cv->cv_id : NULL;
  236. $details = array(
  237. 'relationship_table' => 'example_relationship', // the name of the _relationship table
  238. 'base_table' => 'example', // the name of your chado base table
  239. 'base_foreign_key' => 'example_id', // the name of the key in your base chado table
  240. 'base_key_value' => $example_id, // the value of example_id for this record
  241. 'nodetype' => 'example', // the human-readable name of your node type
  242. 'cv_id' => $cv_id // the cv.cv_id of the cv governing example_relationship.type_id
  243. );
  244. // Adds the form elements to your current form
  245. chado_add_node_form_relationships($form, $form_state, $details);
  246. // return the form
  247. return $form;
  248. }
  249. /**
  250. * Implementation of hook_validate
  251. *
  252. * This function validates a form prior to insert or update. If an error
  253. * is detected, it sets the error using form_set_error() which takes the
  254. * user back to the form to make corrections.
  255. *
  256. * This validation is being used for three activities:
  257. * CASE A: Update a node that exists in both drupal and chado
  258. * CASE B: Synchronizing a node from chado to drupal
  259. * CASE C: Inserting a new node that exists in niether drupal nor chado
  260. *
  261. * @param $node
  262. *
  263. *
  264. * @ingroup tripal_example
  265. */
  266. function chado_example_validate($node, $form, &$form_state) {
  267. // if this is a delete then don't validate
  268. if($node->op == 'Delete') {
  269. return;
  270. }
  271. // we are syncing if we do not have a node ID but we do have a example_id. We don't
  272. // need to validate during syncing so just skip it.
  273. if (is_null($node->nid) and property_exists($node, 'example_id') and $node->example_id != 0) {
  274. return;
  275. }
  276. // be sure to always trim text fields
  277. $node->uniquename = trim($node->uniquename);
  278. $node->description = trim($node->description);
  279. // Validating for an update. If the 'nid' property is present in the node then
  280. // this is an update and validation can be different for updates
  281. if (property_exists($node, 'nid')) {
  282. // make sure the feature type is an allowed term
  283. $type_cv = tripal_get_default_cv('example', 'type_id');
  284. $type = tripal_get_cvterm(array(
  285. 'name' => $node->example_type,
  286. 'cv_id' => $type_cv->cv_id,
  287. ));
  288. if (!$type) {
  289. form_set_error('feature_type', t("The feature type is not a valid name from the Sequence Ontology."));
  290. }
  291. // TODO: also we should check that the unique constraint is not invalidated by
  292. // changing either the type_id, organism_id or uniquename.
  293. }
  294. // Validating for an insert
  295. else {
  296. // make sure the feature type is an allowed term
  297. $type_cv = tripal_get_default_cv('example', 'type_id');
  298. $type = tripal_get_cvterm(array(
  299. 'name' => $node->example_type,
  300. 'cv_id' => $type_cv->cv_id,
  301. ));
  302. if (!$type) {
  303. form_set_error('feature_type', t("The feature type is not a valid name from the Sequence Ontology."));
  304. }
  305. // TODO: also we should check that the unique constraint doesn't already exist
  306. }
  307. }
  308. /**
  309. * Implementation of hook_insert(). This function is called after the
  310. * node is inserted into the database. We need it so that we can insert
  311. * appropriate fields as provided by the user into the database. And so that
  312. * we can link the new Drupal node to the data in Chado via the chado_example
  313. * linking table. We can get to this function also during "syncing".
  314. * With syncing, however, the data already exists in Chado and we do not want
  315. * to try to re-add it. But we do need to add an entry to the chado_example table
  316. * to link the Drupal node with the data in the 'example' table of Chado.
  317. *
  318. * This function is not required if the hook_node_info() does not define
  319. * any custom node types.
  320. *
  321. * @ingroup tripal_example
  322. */
  323. function chado_example_insert($node) {
  324. // be sure to always trim text fields
  325. $node->uniquename = trim($node->uniquename);
  326. $node->description = trim($node->description);
  327. // if there is an example_id in the $node object then this must be a sync so
  328. // we can skip adding the example as it is already there, although
  329. // we do need to proceed with the rest of the insert
  330. if (!property_exists($node, 'example_id')) {
  331. // perform the insert using the tripal_core_chado_insert function();
  332. $values = array(
  333. 'uniquename' => $node->uniquename,
  334. 'description' => $node->description,
  335. 'type_id' => $node->example_type,
  336. 'organism_id' => $node->organism_id,
  337. );
  338. $example = chado_select_record('example', array('*'), $values);
  339. if (!$example) {
  340. drupal_set_message(t('Unable to add example.'), 'warning');
  341. tripal_report_error('tripal_example', TRIPAL_WARNING, 'Insert example: Unable to create example where values: %values',
  342. array('%values' => print_r($values, TRUE)));
  343. return;
  344. }
  345. // get the example_id for linking Drupal node with Chado data
  346. $example_id = $example->example_id;
  347. // Only add to other chado tables if the base record was inserted properly
  348. if ($example_id > 0) {
  349. // If you implemented the properties form in chado_example_form then you need to
  350. // handle inserting these properties into your chado prop table.
  351. $details = array(
  352. 'property_table' => 'exampleprop', // the name of the prop table
  353. 'base_table' => 'example', // the name of your chado base table
  354. 'foreignkey_name' => 'example_id', // the name of the key in your base table
  355. 'foreignkey_value' => $example_id // the value of the example_id key
  356. );
  357. chado_update_node_form_properties($node, $details);
  358. // If you implemented the dbxrefs form in chado_example_form then you need to
  359. // handle inserting these database references into your chado _dbxref table.
  360. $details = array(
  361. 'linking_table' => 'example_dbxref', // the name of your _dbxref table
  362. 'foreignkey_name' => 'example_id', // the name of the key in your base table
  363. 'foreignkey_value' => $example_id // the value of the example_id key
  364. );
  365. chado_update_node_form_dbxrefs($node, $details);
  366. // If you implemented the relationships form in chado_example_form then you need to
  367. // handle inserting these relationships into your chado _relationship table.
  368. $details = array(
  369. 'relationship_table' => 'example_relationship', // name of the _relationship table
  370. 'foreignkey_value' => $example_id // value of the example_id key
  371. );
  372. chado_update_node_form_relationships($node, $details);
  373. }
  374. }
  375. else {
  376. // the node has an example_id so get it for linking Drupal node with Chado data
  377. $example_id = $node->example_id;
  378. }
  379. // Make sure the entry for this example doesn't already exist in the
  380. // chado_example table if it doesn't exist then we want to add it.
  381. // $check_org_id = chado_get_id_from_nid('example', $node->nid);
  382. if (!$check_org_id) {
  383. $record = new stdClass();
  384. $record->nid = $node->nid;
  385. $record->vid = $node->vid;
  386. $record->example_id = $example_id;
  387. drupal_write_record('chado_example', $record);
  388. }
  389. }
  390. /**
  391. * Implementation of hook_update(). This function runs after the
  392. * node has been inserted into the Drupal schema and allows us to
  393. * update the record in Chado.
  394. *
  395. * This function is not required if the hook_node_info() does not define
  396. * any custom node types.
  397. *
  398. * @ingroup tripal_example
  399. */
  400. function chado_example_update($node) {
  401. // be sure to always trim text fields
  402. $node->uniquename = trim($node->uniquename);
  403. $node->description = trim($node->description);
  404. // use the chado_update_record() function to update the record
  405. $match = array(
  406. 'example_id' => $example_id,
  407. );
  408. $values = array(
  409. 'uniquename' => $node->uniquename,
  410. );
  411. $options = array('return_record' => TRUE);
  412. $status = chado_update_record('example', $match, $values, $options);
  413. if (!$status) {
  414. drupal_set_message(t('Unable to update example.'), 'warning');
  415. tripal_report_error('tripal_example', TRIPAL_WARNING, 'Update example: Unable to update example where values: %values',
  416. array('%values' => print_r($values, TRUE)));
  417. }
  418. // If you implemented the properties form in chado_example_form then you need to
  419. // handle updating these properties into your chado prop table.
  420. $details = array(
  421. 'property_table' => 'exampleprop', // the name of the prop table
  422. 'base_table' => 'example', // the name of your chado base table
  423. 'foreignkey_name' => 'example_id', // the name of the key in your base table
  424. 'foreignkey_value' => $example_id // the value of the example_id key
  425. );
  426. chado_update_node_form_properties($node, $details);
  427. // If you implemented the dbxrefs form in chado_example_form then you need to
  428. // handle updating these database references into your chado _dbxref table.
  429. $details = array(
  430. 'linking_table' => 'example_dbxref', // the name of your _dbxref table
  431. 'foreignkey_name' => 'example_id', // the name of the key in your base table
  432. 'foreignkey_value' => $example_id // the value of the example_id key
  433. );
  434. chado_update_node_form_dbxrefs($node, $details);
  435. // If you implemented the relationships form in chado_example_form then you need to
  436. // handle updating these relationships into your chado _relationship table.
  437. $details = array(
  438. 'relationship_table' => 'example_relationship', // name of the _relationship table
  439. 'foreignkey_value' => $example_id // value of the example_id key
  440. );
  441. chado_update_node_form_relationships($node, $details);
  442. }
  443. /**
  444. * Implementation of hook_delete(). This function runs after the
  445. * node has been deleted from the Drupal schema and allows us to
  446. * delete the corresponding recrod in Chado.
  447. *
  448. * This function is not required if the hook_node_info() does not define
  449. * any custom node types.
  450. *
  451. * @ingroup tripal_example
  452. */
  453. function chado_example_delete($node) {
  454. // get the example id from the node
  455. $example_id = chado_get_id_from_nid('example', $node->nid);
  456. // if we don't have a example id for this node then this isn't a node of
  457. // type chado_example or the entry in the chado_example table was lost.
  458. if (!$example_id) {
  459. return;
  460. }
  461. // remove the entry in the chado_exapmle table linking the deleted
  462. // Drupal node with the data in chado
  463. $sql_del = "DELETE FROM {chado_example} WHERE nid = :nid AND vid = :vid";
  464. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  465. // Remove data from example tables of chado database. This will
  466. // cause a cascade delete and remove all data in referencing tables
  467. // for this example
  468. chado_query("DELETE FROM {example} WHERE example_id = :example_id", array(':example_id' => $example_id));
  469. // inform the user that the data was deleted
  470. drupal_set_message(t("The example and all associated data were removed from Chado"));
  471. }
  472. /**
  473. * Implementation of hook_load(). This function is necessary to load
  474. * into the $node object the fields of the table form Chado. For example
  475. * for the example table, the chado_example_load() function adds in
  476. * a example object which contains all of the fields and sub objects
  477. * for data in tables with foreign key relationships.
  478. *
  479. * This function is not required if the hook_node_info() does not define
  480. * any custom node types.
  481. *
  482. * @ingroup tripal_example
  483. */
  484. function chado_example_load($nodes) {
  485. // EXPLANATION: when displaying or node or accessing the node in a template
  486. // we need the data from Chado. This fucntion finds the record in Chado that
  487. // this node belongs to and adds the record.
  488. // there may be multiple nodes that get passed in so we have to iterate through
  489. // them all
  490. foreach ($nodes as $nid => $node) {
  491. // find the example and add in the details
  492. $example_id = chado_get_id_from_nid('example', $nid);
  493. // if the nid does not have a matching record then skip this node.
  494. // this can happen with orphaned nodes.
  495. if (!$example_id) {
  496. continue;
  497. }
  498. // build the example variable by using the chado_generate_var() function
  499. $values = array('example_id' => $example_id);
  500. $example = chado_generate_var('example', $values);
  501. // for fields in the table that are of type 'text' you may want to include those
  502. // by default, the tripal_core_generate_chado_var does not include text fields as
  503. // they may be very large and including a large text field can slow the page load.
  504. // If you know a text field will never be large and it is important for the
  505. // other functions that will see the node to have access to a field you can
  506. // include it here using the chado_expand_var() function. In most
  507. // cases it is probably best to let the end-user decide if text fields should
  508. // be included by using this function in the templates.
  509. $example = chado_expand_var($example, 'field', 'example.residues');
  510. // add the new example object to this node.
  511. $nodes[$nid]->example = $example;
  512. }
  513. }
  514. /**
  515. * Implementation of hook_node_presave().
  516. *
  517. * Performs actions on a node object prior to it being saved
  518. *
  519. * @ingroup tripal_example
  520. */
  521. function tripal_example_node_presave($node) {
  522. // EXPLANATION: This node is useful for
  523. // making changes to the node prior to it being saved to the database.
  524. // One useful case for this is to set the title of a node. In some cases
  525. // such as for the organism module, the title will be set depending on
  526. // what genus and species is provided. This hook can allow the title to
  527. // be set using user supplied data before the node is saved. In practice
  528. // any change can be made to any fields in the node object.
  529. //
  530. // This function is not required. You probably won't need it if you
  531. // don't define a custom node type in the hook_node_info() function. But
  532. // it is node type agnostic, so you can use this function to change the
  533. // contents of any node regardless of it's type.
  534. // set the node title
  535. switch ($node->type) {
  536. case 'chado_example':
  537. // for a form submission the 'uniquename' field will be set,
  538. // for a sync, we must pull from the example object
  539. if (property_exists($node, 'uniquename')) {
  540. // set the title
  541. $node->title = $node->uniquename;
  542. }
  543. else if (property_exists($node, 'example')) {
  544. $node->title = $node->example->uniquename;
  545. }
  546. break;
  547. }
  548. }
  549. /**
  550. * Implementation of hook node_insert().
  551. *
  552. * Performs actions after any node has been inserted.
  553. *
  554. * @ingroup tripal_example
  555. */
  556. function tripal_example_node_insert($node) {
  557. // EXPLANATION: This function is used
  558. // after any a node is inserted into the database. It is different
  559. // from the hook_insert() function above in that it is called after
  560. // any node is saved, regardlesss of it's type. This function is useful
  561. // for making changes to the database after a node is inserted.
  562. // An example comes from the tripal_feature module where the URL alias
  563. // of a node cannot be set in the hook_insert() function. Therefore
  564. // the tripal_feature module uses this function to set the url path
  565. // of a newly inserted example node.
  566. //
  567. // This function is not required. You probably won't need it if you
  568. // don't define a custom node type in the hook_node_info() function. But
  569. // it is node type agnostic, so you can use this function to do any
  570. // activity after insert of any node.
  571. // the Example code below will set the URL path after inserting. We do it
  572. // here because we do not know the example_id in the presave and cannot do
  573. // it in the hook_insert()
  574. switch ($node->type) {
  575. case 'chado_example':
  576. // if we do not have an record ID for this node then get it
  577. if (!$node->example_id) {
  578. $node->example_id = chado_get_id_for_node('example', $node);
  579. }
  580. // set the URL for this example page
  581. // see the code in the tripal_feature/includes/tripal_feature.chado_node.inc file
  582. // in the function tripal_feature_node_insert for an example of how that
  583. // module sets the URL. It uses a configuration file to allow the user
  584. // to dynmically build a URL schema and then uses that schema to generate
  585. // a URL string.
  586. break;
  587. }
  588. }
  589. /**
  590. * Implementation of hook node_update().
  591. *
  592. * Performs actions after any node has been updated.
  593. *
  594. */
  595. function tripal_example_node_update($node) {
  596. // EXPLANATION: This function is used
  597. // after any a node is updated in the database. It is different
  598. // from the hook_update() function above in that it is called after
  599. // any node is updated, regardlesss of it's type.
  600. // An example comes from the tripal_feature module where the URL alias
  601. // of a node cannot be set in the hook_update() function. Therefore
  602. // the tripal_feature module uses this function to reset the url path
  603. // of an updated feature node.
  604. //
  605. // This function is not required. You probably won't need it if you
  606. // don't define a custom node type in the hook_node_info() function. But
  607. // it is node type agnostic, so you can use this function to do any
  608. // activity after insert of a node.
  609. // add items to other nodes, build index and search results
  610. switch ($node->type) {
  611. case 'chado_example':
  612. // set the URL for this example page
  613. // see the code in the tripal_feature/includes/tripal_feature.chado_node.inc file
  614. // in the function tripal_feature_node_insert for an example of how that
  615. // module sets the URL. It uses a configuration file to allow the user
  616. // to dynmically build a URL schema and then uses that schema to generate
  617. // a URL string.
  618. break;
  619. }
  620. }
  621. /**
  622. * Implementation of hook_node_view().
  623. *
  624. * @ingroup tripal_example
  625. */
  626. function tripal_example_node_view($node, $view_mode, $langcode) {
  627. // EXPLANATION: This function defines the content "blocks" that appear
  628. // when thhe node is displayed. It is node type agnostic so we can add
  629. // content to any node type. So, we use this function to add the content
  630. // from all of our theme templates onto our new node type. We will also
  631. // use this function to add content to other node types.
  632. switch ($node->type) {
  633. case 'chado_example':
  634. // there are different ways a node can be viewed. Primarily Tripal
  635. // supports full page view and teaser view.
  636. if ($view_mode == 'full') {
  637. // There is always a base template. This is the template that
  638. // is first shown when the example node type is first displayed.
  639. // if you are using the default Tripal node template, then you should
  640. // also set two additional items in each array: tripal_toc_id and
  641. // tripal_toc_title. The tripal_tock_id should be a single unqiue
  642. // world that is used to reference the template. This ID is used for
  643. // constructing URLs for the content. The tripal_toc_title contains
  644. // the title that should appear in the table of contents for this
  645. // content. You should only set the '#weight' element for the
  646. // base template (or Overview) to ensure that it appears at the top of
  647. // the list. Otherwise items are sorted alphabetically.
  648. $node->content['tripal_example_base'] = array(
  649. '#markup' => theme('tripal_example_base', array('node' => $node)),
  650. '#tripal_toc_id' => 'base',
  651. '#tripal_toc_title' => 'Overview',
  652. '#weight' => -100,
  653. );
  654. // we can add other templates as well for properties, publications,
  655. // dbxrefs, etc...
  656. $node->content['tripal_example_properties'] = array(
  657. '#markup' => theme('tripal_example_properties', array('node' => $node)),
  658. '#tripal_toc_id' => 'properties',
  659. '#tripal_toc_title' => 'Properties',
  660. );
  661. $node->content['tripal_example_references'] = array(
  662. '#markup' => theme('tripal_feature_references', array('node' => $node)),
  663. '#tripal_toc_id' => 'references',
  664. '#tripal_toc_title' => 'Cross References',
  665. );
  666. $node->content['tripal_example_relationships'] = array(
  667. '#markup' => theme('tripal_feature_relationships', array('node' => $node)),
  668. '#tripal_toc_id' => 'relationships',
  669. '#tripal_toc_title' => 'Relationships',
  670. );
  671. }
  672. // set the content for the teaser view
  673. if ($view_mode == 'teaser') {
  674. // The teaser is also a required template
  675. $node->content['tripal_example_teaser'] = array(
  676. '#value' => theme('tripal_example_teaser', array('node' => $node)),
  677. );
  678. }
  679. break;
  680. // you can add custom content to any node type by adding
  681. // content to the node in the same way as above.
  682. case 'chado_organism':
  683. break;
  684. case 'chado_library':
  685. break;
  686. case 'chado_stock':
  687. break;
  688. case 'chado_analysis':
  689. break;
  690. // ... etc
  691. }
  692. }