tripal_example.chado_node.inc 18 KB

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