tripal_example.chado_node.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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(). This hook provides information to drupal
  9. * about any node types that are being created by this module. If your module
  10. * does not create any node types then this function is not required.
  11. *
  12. * @ingroup tripal_example
  13. */
  14. function tripal_example_node_info() {
  15. $nodes = array();
  16. //$nodes['chado_example'] = array(
  17. // 'name' => t('example'),
  18. // 'base' => 'chado_example',
  19. // 'description' => t('A example from the chado database'),
  20. // 'title_label' => t('example'),
  21. // 'has_title' => FALSE,
  22. // 'has_body' => FALSE,
  23. // 'locked' => TRUE
  24. // This section of the node type array specifies how Tripal will sync the node
  25. // types with data in Chado. When Drupal creates a node it has no way of
  26. // coordinating which node belongs to which record in Chado. Therefore,
  27. // Tripal maintains tables in the Drupal schema that maps Drupal nodes
  28. // to recrords in Chado. Syncing is the process of creating Drupal nodes
  29. // and linking them to the appropriate record.
  30. // 'chado_node_api' => array(
  31. // 'base_table' => 'example', // the base table name (e.g. feature, library, contact)
  32. // 'hook_prefix' => 'chado_example',// the node type hook prefix
  33. // 'record_type_title' => array(
  34. // 'singular' => t('Library'), // how to refer to the record
  35. // 'plural' => t('Libraries') // how to refer to the record in plurals
  36. // ),
  37. // 'sync_filters' => array(
  38. // 'type_id' => TRUE, // if the record has a type_id set to TRUE
  39. // 'organism_id' => TRUE // if the record has an organism_id set to TRUE
  40. // ),
  41. // )
  42. //);
  43. return $nodes;
  44. }
  45. /**
  46. * Implement hook_access(). This hook provides instructions to
  47. * drupal for which users can access the custom content types
  48. * created in the function above. The available permissions
  49. * are set in the chado_example_permissions() hook in the
  50. * tripal_example.module file. This hook is not needed
  51. * if no node types were defined in the hook_node_info() hook.
  52. *
  53. * @return
  54. * This function should return null if it does not specificially
  55. * deny access. This allows for other mechanisms to to deny
  56. * or reject access. If the return value is TRUE then access
  57. * is granted regardless of any other rules that might be implemented
  58. * by other modules.
  59. */
  60. function chado_example_node_access($node, $op, $account) {
  61. if ($op == 'create') {
  62. if (!user_access('create chado_example content', $account)) {
  63. return FALSE;
  64. }
  65. return TRUE;
  66. }
  67. if ($op == 'update') {
  68. if (!user_access('edit chado_example content', $account)) {
  69. return FALSE;
  70. }
  71. }
  72. if ($op == 'delete') {
  73. if (!user_access('delete chado_example content', $account)) {
  74. return FALSE;
  75. }
  76. }
  77. if ($op == 'view') {
  78. if (!user_access('access chado_example content', $account)) {
  79. return FALSE;
  80. }
  81. }
  82. return NULL;
  83. }
  84. /**
  85. * Implementation of hook_form() when a node type of chado_example is defined.
  86. * If a node type is not defined then this function is not needed. The table
  87. * name in chado for this example module is named 'example' so there is a
  88. * corresponding example_id in that table (similar to feature.feature_id,
  89. * contact.contact_id, etc).
  90. *
  91. * @ingroup tripal_example
  92. */
  93. function chado_example_form($node, &$form_state) {
  94. $form = array();
  95. // Default values can come in the following ways:
  96. //
  97. // 1) as elements of the $node object. This occurs when editing an existing example
  98. // 2) in the $form_state['values'] array which occurs on a failed validation or
  99. // ajax callbacks from non submit form elements
  100. // 3) in the $form_state['input'[ array which occurs on ajax callbacks from submit
  101. // form elements and the form is being rebuilt
  102. //
  103. // set form field defaults
  104. // if we are editing an existing node then the example is already part of the node
  105. if (property_exists($node, 'example')) {
  106. // $example = $node->example;
  107. // $example = tripal_core_expand_chado_vars($example, 'field', 'example.residues');
  108. // $example_id = $example->example_id;
  109. // $uniquename = $example->uniquename;
  110. // keep track of the example id
  111. //$form['example_id'] = array(
  112. // '#type' => 'value',
  113. // '#value' => $example_id,
  114. //);
  115. }
  116. // if we are re constructing the form from a failed validation or ajax callback
  117. // then use the $form_state['values'] values
  118. if (array_key_exists('values', $form_state)) {
  119. // $uniquename = $form_state['values']['uniquename'];
  120. }
  121. // if we are re building the form from after submission (from ajax call) then
  122. // the values are in the $form_state['input'] array
  123. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  124. // $uniquename = $form_state['input']['uniquename'];
  125. }
  126. // add form elements here.
  127. // return the form
  128. return $form;
  129. }
  130. /**
  131. * Implementation of hook_validate
  132. *
  133. * This validation is being used for three activities:
  134. * CASE A: Update a node that exists in both drupal and chado
  135. * CASE B: Synchronizing a node from chado to drupal
  136. * CASE C: Inserting a new node that exists in niether drupal nor chado
  137. *
  138. * @param $node
  139. *
  140. *
  141. * @ingroup tripal_example
  142. */
  143. function chado_example_validate($node, $form, &$form_state) {
  144. // be sure to always trim text fields
  145. // $node->uniquename = trim($node->uniquename);
  146. // if this is a delete then don't validate
  147. if($node->op == 'Delete') {
  148. return;
  149. }
  150. // we are syncing if we do not have a node ID but we do have a example_id. We don't
  151. // need to validate during syncing so just skip it.
  152. if (is_null($node->nid) and property_exists($node, 'example_id') and $node->example_id != 0) {
  153. return;
  154. }
  155. // Validating for an update. If the 'nid' property is present in the node then
  156. // this is an update and validation can be different for updates
  157. if (property_exists($node, 'nid')) {
  158. // if there is a problem with a field then you can set an error on the form
  159. // form_set_error('uniquename', t("example update cannot proceed. The example name '$node->uniquename' is not unique for this organism. Please provide a unique name for this example."));
  160. }
  161. // Validating for an insert
  162. else {
  163. // if there is a problem with a field then you can set an error on the form
  164. // form_set_error('uniquename', t("example insert cannot proceed. The example name '$node->uniquename' already exists for this organism. Please provide a unique name for this example."));
  165. }
  166. }
  167. /**
  168. * Implementation of hook_insert(). This function is called after the
  169. * node is inserted into the database. We need it so that we can insert
  170. * appropriate fields as provided by the user into the database. And so that
  171. * we can link the new Drupal node to the data in Chado via the chado_example
  172. * linking table. We can get to this function also during "syncing".
  173. * With syncing, however, the data already exists in Chado and we do not want
  174. * to try to re-add it. But we do need to add an entry to the chado_example table
  175. * to link the Drupal node with the data in the 'example' table of Chado.
  176. *
  177. * This function is not required if the hook_node_info() does not define
  178. * any custom node types.
  179. *
  180. * @ingroup tripal_example
  181. */
  182. function chado_example_insert($node) {
  183. // be sure to always trim text fields
  184. //$node->uniquename = trim($node->uniquename);
  185. // if there is an example_id in the $node object then this must be a sync so
  186. // we can skip adding the example as it is already there, although
  187. // we do need to proceed with the rest of the insert
  188. if (!property_exists($node, 'example_id')) {
  189. // perform the insert using the tripal_core_chado_insert function();
  190. //$values = array(
  191. // 'uniquename' => $node->uniquename,
  192. // 'residues' => $residues,
  193. //);
  194. //$example = tripal_core_chado_select('example', array('*'), $values);
  195. //if (!$example) {
  196. // drupal_set_message(t('Unable to add example.'), 'warning');
  197. // watchdog('tripal_example', 'Insert example: Unable to create example where values: %values',
  198. // array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
  199. // return;
  200. //}
  201. // get the example_id for linking Drupal node with Chado data
  202. // $example_id = $example->example_id;
  203. }
  204. else {
  205. // the node has an example_id so get it for linking Drupal node with Chado data
  206. // $example_id = $node->example_id;
  207. }
  208. // Make sure the entry for this example doesn't already exist in the
  209. // chado_example table if it doesn't exist then we want to add it.
  210. // $check_org_id = chado_get_id_for_node('example', $node->nid);
  211. //if (!$check_org_id) {
  212. // $record = new stdClass();
  213. // $record->nid = $node->nid;
  214. // $record->vid = $node->vid;
  215. // $record->example_id = $example_id;
  216. // drupal_write_record('chado_example', $record);
  217. //}
  218. }
  219. /**
  220. * Implementation of hook_update(). This function runs after the
  221. * node has been inserted into the Drupal schema and allows us to
  222. * update the record in Chado.
  223. *
  224. * This function is not required if the hook_node_info() does not define
  225. * any custom node types.
  226. *
  227. * @ingroup tripal_example
  228. */
  229. function chado_example_update($node) {
  230. // be sure to always trim text fields
  231. // $node->uniquename = trim($node->uniquename);
  232. // use the tripal_core_chado_update() function to update the record
  233. //$match = array(
  234. //'example_id' => $example_id,
  235. //);
  236. //$values = array(
  237. // 'uniquename' => $node->uniquename,
  238. //);
  239. //$options = array('return_record' => TRUE);
  240. //$status = tripal_core_chado_update('example', $match, $values, $options);
  241. //if (!$status) {
  242. // drupal_set_message(t('Unable to update example.'), 'warning');
  243. // watchdog('tripal_example', 'Update example: Unable to update example where values: %values',
  244. // array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
  245. //}
  246. }
  247. /**
  248. * Implementation of hook_delete(). This function runs after the
  249. * node has been deleted from the Drupal schema and allows us to
  250. * delete the corresponding recrod in Chado.
  251. *
  252. * This function is not required if the hook_node_info() does not define
  253. * any custom node types.
  254. *
  255. * @ingroup tripal_example
  256. */
  257. function chado_example_delete($node) {
  258. // get the example id from the node
  259. //$example_id = chado_get_id_for_node('example', $node->nid);
  260. // if we don't have a example id for this node then this isn't a node of
  261. // type chado_example or the entry in the chado_example table was lost.
  262. if (!$example_id) {
  263. return;
  264. }
  265. // remove the entry in the chado_exapmle table linking the deleted
  266. // Drupal node with the data in chado
  267. // $sql_del = "DELETE FROM {chado_example} WHERE nid = :nid AND vid = :vid";
  268. // db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  269. // Remove data from example tables of chado database. This will
  270. // cause a cascade delete and remove all data in referencing tables
  271. // for this example
  272. // chado_query("DELETE FROM {example} WHERE example_id = :example_id", array(':example_id' => $example_id));
  273. // inform the user that the data was deleted
  274. drupal_set_message(t("The example and all associated data were removed from Chado"));
  275. }
  276. /**
  277. * Implementation of hook_load(). This function is necessary to load
  278. * into the $node object the fields of the table form Chado. For example
  279. * for the feature table, the chado_feature_load() function adds in
  280. * a feature object which contains all of the fields and sub objects
  281. * for data in tables with foreign key relationships.
  282. *
  283. * This function is not required if the hook_node_info() does not define
  284. * any custom node types.
  285. *
  286. * @ingroup tripal_example
  287. */
  288. function chado_example_load($nodes) {
  289. // there may be multiple nodes that get passed in so we have to iterate through
  290. // them all
  291. foreach ($nodes as $nid => $node) {
  292. // find the example and add in the details
  293. //$example_id = chado_get_id_for_node('example', $nid);
  294. // build the example variable by using the tripal_core_generate_chado_var() function
  295. //$values = array('example_id' => $example_id);
  296. //$example = tripal_core_generate_chado_var('example', $values);
  297. // for fields in the table that are of type 'text' you may want to include those
  298. // by default, the tripal_core_generate_chado_var does not include text fields as
  299. // they may be very large and including a large text field can slow the page load.
  300. // If you know a text field will never be large and it is important for the
  301. // other functions that will see the node to have access to a field you can
  302. // include it here using the tripal_core_expand_chado_vars() function. In most
  303. // cases it is probably best to let the end-user decide if text fields should
  304. // be included by using this function in the templates.
  305. //$example = tripal_core_expand_chado_vars($example, 'field', 'example.residues');
  306. // add the new example object to this node.
  307. //$nodes[$nid]->example = $example;
  308. }
  309. }
  310. /**
  311. * Implementation of hook_node_presave(). This node is useful for
  312. * making changes to the node prior to it being saved to the database.
  313. * One useful case for this is to set the title of a node. In some cases
  314. * such as for the organism module, the title will be set depending on
  315. * what genus and species is provided. This hook can allow the title to
  316. * be set using user supplied data before the node is saved. In practice
  317. * any change can be made to any fields in the node object.
  318. *
  319. * This function is not required. You probably won't need it if you
  320. * don't define a custom node type in the hook_node_info() function. But
  321. * it is node type agnostic, so you can use this function to change the
  322. * contents of any node regardless of it's type.
  323. *
  324. * @ingroup tripal_example
  325. */
  326. function tripal_example_node_presave($node) {
  327. // set the title to ensure it is always unique
  328. //switch ($node->type) {
  329. // case 'chado_example':
  330. // $node->title = $node->uniquename;
  331. // break;
  332. //}
  333. }
  334. /**
  335. * Implementation of hook node_insert(). This function is used
  336. * after any a node is inserted into the database. It is different
  337. * from the hook_insert() function above in that it is called after
  338. * any node is saved, regardlesss of it's type. This function is useful
  339. * for making changes to the database after a node is inserted when you
  340. * can't edit the hook_insert() function of a node not defined by this
  341. * module, or to access values of a node when have not yet been saved.
  342. * An example comes from the tripal_feature module where the URL alias
  343. * of a node cannot be set in the hook_insert() function. Therefore
  344. * the tripal_feature module uses this function to set the url path
  345. * of a newly inserted feature node.
  346. *
  347. * This function is not required. You probably won't need it if you
  348. * don't define a custom node type in the hook_node_info() function. But
  349. * it is node type agnostic, so you can use this function to do any
  350. * activity after insert of a node.
  351. *
  352. * @ingroup tripal_example
  353. */
  354. function tripal_example_node_insert($node) {
  355. // set the URL path after inserting. We do it here because we do not know the
  356. // example_id in the presave and cannot do it in the hook_insert()
  357. //switch ($node->type) {
  358. // case 'chado_example':
  359. // if (!$node->example_id) {
  360. // $sql = "SELECT * FROM {chado_example} WHERE nid = :nid";
  361. // $chado_example = db_query($sql, array(':nid' => $node->nid))->fetchObject();
  362. // $node->example_id = $chado_example->example_id;
  363. // }
  364. // // remove any previous alias
  365. // db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  366. // // set the URL for this example page
  367. // $url_alias = tripal_example_get_example_url($node);
  368. // $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  369. // path_save($path_alias);
  370. // break;
  371. //}
  372. }
  373. /**
  374. * Implementation of hook node_update(). This function is used
  375. * after any a node is updated in the database. It is different
  376. * from the hook_update() function above in that it is called after
  377. * any node is updated, regardlesss of it's type. This function is useful
  378. * for making changes to the database after a node is updated when you
  379. * can't perform changes in the hook_upate() function of a node not defined by this
  380. * module, or to access values of a node when have not yet been updated.
  381. * An example comes from the tripal_feature module where the URL alias
  382. * of a node cannot be set in the hook_update() function. Therefore
  383. * the tripal_feature module uses this function to reset the url path
  384. * of an updated feature node.
  385. *
  386. * This function is not required. You probably won't need it if you
  387. * don't define a custom node type in the hook_node_info() function. But
  388. * it is node type agnostic, so you can use this function to do any
  389. * activity after insert of a node.
  390. *
  391. */
  392. function tripal_example_node_update($node) {
  393. // add items to other nodes, build index and search results
  394. switch ($node->type) {
  395. case 'chado_example':
  396. // remove any previous alias
  397. //db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  398. // set the URL for this example page
  399. //$url_alias = tripal_example_get_example_url($node);
  400. //$path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  401. //path_save($path_alias);
  402. break;
  403. }
  404. }
  405. /**
  406. * Implementation of hook_node_view(). This function allows you to
  407. * add custom content to any node page. It is node type agnostic.
  408. * Here we typically use it to add content to our custom node type or
  409. * to other Tripal node types. Typically for Tripal, a content "block"
  410. * (e.g. feature properties, feature dbxref, feature pub) has a corresponding
  411. * template file. Those template files are first defined to Drupal using
  412. * the hook_theme() function defined in the tripal_example.module file. Here
  413. * we can add items to a node's content by calling those templates as needed.
  414. *
  415. * @ingroup tripal_example
  416. */
  417. function tripal_example_node_view($node, $view_mode, $langcode) {
  418. switch ($node->type) {
  419. case 'chado_example':
  420. // there are different ways a node can be viewed. Primarily Tripal
  421. // supports full page view and teaser view.
  422. if ($view_mode == 'full') {
  423. // there is always a base template. This is the template that
  424. // is first shown when the example node type is first displayed
  425. //$node->content['tripal_example_base'] = array(
  426. // '#value' => theme('tripal_example_base', array('node' => $node)),
  427. //);
  428. // we can add other templates as well.
  429. //$node->content['tripal_example_properties'] = array(
  430. // '#value' => theme('tripal_example_properties', array('node' => $node)),
  431. //);
  432. //$node->content['tripal_example_publications'] = array(
  433. // '#value' => theme('tripal_example_publications', array('node' => $node)),
  434. //);
  435. //$node->content['tripal_example_references'] = array(
  436. // '#value' => theme('tripal_example_references', array('node' => $node)),
  437. //);
  438. }
  439. // set the content for the teaser view
  440. if ($view_mode == 'teaser') {
  441. //$node->content['tripal_example_teaser'] = array(
  442. // '#value' => theme('tripal_example_teaser', array('node' => $node)),
  443. //);
  444. }
  445. break;
  446. // you can add custom content to any tripal node type by adding
  447. // content to the node in the same way as above.
  448. case 'chado_organism':
  449. break;
  450. case 'chado_library':
  451. break;
  452. case 'chado_stock':
  453. break;
  454. case 'chado_analysis':
  455. break;
  456. // ... etc
  457. }
  458. }