tripal_featuremap.chado_node.inc

  1. 2.x tripal_featuremap/includes/tripal_featuremap.chado_node.inc
  2. 3.x legacy/tripal_featuremap/includes/tripal_featuremap.chado_node.inc

Hooks implementing the feature map node content type

File

tripal_featuremap/includes/tripal_featuremap.chado_node.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Hooks implementing the feature map node content type
  5. */
  6. /**
  7. * Implements hook_node_info().
  8. *
  9. * Provide information to drupal about the node types that we're creating
  10. * in this module
  11. *
  12. * @ingroup tripal_featuremap
  13. */
  14. function tripal_featuremap_node_info() {
  15. $nodes = array();
  16. $nodes['chado_featuremap'] = array(
  17. 'name' => t('Feature Map'),
  18. 'base' => 'chado_featuremap',
  19. 'description' => t('A map of features from the chado database (e.g. genetic map)'),
  20. 'has_title' => TRUE,
  21. 'locked' => TRUE,
  22. 'chado_node_api' => array(
  23. 'base_table' => 'featuremap',
  24. 'hook_prefix' => 'chado_featuremap',
  25. 'record_type_title' => array(
  26. 'singular' => t('Feature Map'),
  27. 'plural' => t('Feature Maps')
  28. ),
  29. 'sync_filters' => array(
  30. 'type_id' => FALSE,
  31. 'organism_id' => FALSE
  32. ),
  33. )
  34. );
  35. return $nodes;
  36. }
  37. /**
  38. * When editing or creating a new node of type 'chado_featuremap' we need
  39. * a form. This function creates the form that will be used for this.
  40. *
  41. * @ingroup tripal_featuremap
  42. */
  43. function chado_featuremap_form($node, &$form_state) {
  44. $form = array();
  45. // Default values can come in the following ways:
  46. //
  47. // 1) as elements of the $node object. This occurs when editing an existing library
  48. // 2) in the $form_state['values'] array which occurs on a failed validation or
  49. // ajax callbacks from non submit form elements
  50. // 3) in the $form_state['input'[ array which occurs on ajax callbacks from submit
  51. // form elements and the form is being rebuilt
  52. //
  53. // set form field defaults
  54. $featuremap_id = NULL;
  55. $fmapname = '';
  56. $description = '';
  57. $unittype_id = '';
  58. // if we are editing an existing node then the featuremap is already part of the node
  59. if (property_exists($node, 'featuremap')) {
  60. $featuremap = $node->featuremap;
  61. $featuremap = chado_expand_var($featuremap, 'field', 'featuremap.description');
  62. $featuremap_id = $featuremap->featuremap_id;
  63. // get form defaults
  64. $fmapname = $featuremap->name;
  65. $description = $featuremap->description;
  66. $unittype_id = $featuremap->unittype_id->cvterm_id;
  67. // set the featuremap_id in the form
  68. $form['featuremap_id'] = array(
  69. '#type' => 'hidden',
  70. '#value' => $featuremap_id,
  71. );
  72. }
  73. // if we are re constructing the form from a failed validation or ajax callback
  74. // then use the $form_state['values'] values
  75. if (array_key_exists('values', $form_state)) {
  76. $fmapname = $form_state['values']['fmapname'];
  77. $description = $form_state['values']['description'];
  78. $unittype_id = $form_state['values']['unittype_id'];
  79. }
  80. // if we are re building the form from after submission (from ajax call) then
  81. // the values are in the $form_state['input'] array
  82. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  83. $fmapname = $form_state['input']['fmapname'];
  84. $description = $form_state['input']['description'];
  85. $unittype_id = $form_state['input']['unittype_id'];
  86. }
  87. $form['fmapname']= array(
  88. '#type' => 'textfield',
  89. '#title' => t('Map Name'),
  90. '#description' => t('Please enter a name for this map'),
  91. '#required' => TRUE,
  92. '#default_value' => $fmapname,
  93. '#maxlength' => 255
  94. );
  95. $form['description']= array(
  96. '#type' => 'text_format',
  97. '#title' => t('Map Description'),
  98. '#description' => t('A description of the map.'),
  99. '#required' => TRUE,
  100. '#default_value' => $description,
  101. );
  102. // get the list of unit types
  103. $units = tripal_get_cvterm_default_select_options('featuremap', 'unittype_id', 'map unit types');
  104. $form['unittype_id'] = array(
  105. '#title' => t('Map Units'),
  106. '#type' => t('select'),
  107. '#description' => t("Chose the units for this map"),
  108. '#required' => TRUE,
  109. '#default_value' => $unittype_id,
  110. '#options' => $units,
  111. );
  112. // Properties Form
  113. // ----------------------------------
  114. $prop_cv = tripal_get_default_cv('featuremap_property', 'type_id');
  115. $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
  116. $instructions = t('To add additional properties to the drop down. ' . l("Add terms to the featuremap_property vocabulary", "admin/tripal/chado/tripal_cv/cvterm/add") . ".");
  117. $details = array(
  118. 'property_table' => 'featuremapprop',
  119. 'chado_id' => $featuremap_id,
  120. 'cv_id' => $cv_id,
  121. 'fieldset_name' => 'Additional Details',
  122. 'additional_instructions' => $instructions
  123. );
  124. // TODO: remove the 'Map Dbxref' from the list as that should now be handled
  125. // by the dbxref interface below
  126. chado_add_node_form_properties($form, $form_state, $details);
  127. // ADDITIONAL DBXREFS FORM
  128. //---------------------------------------------
  129. $details = array(
  130. 'linking_table' => 'featuremap_dbxref', // the name of the _dbxref table
  131. 'base_foreign_key' => 'featuremap_id', // the name of the key in your base chado table
  132. 'base_key_value' => $featuremap_id // the value of featuremap_id for this record
  133. );
  134. // Adds the form elements to your current form
  135. chado_add_node_form_dbxrefs($form, $form_state, $details);
  136. return $form;
  137. }
  138. /**
  139. * Validates submission of form when adding or updating a map node
  140. *
  141. * @ingroup tripal_featuremap
  142. */
  143. function chado_featuremap_validate($node, $form, &$form_state) {
  144. // We only want to validate when the node is saved.
  145. // Since this validate can be called on AJAX and Deletion of the node
  146. // we need to make this check to ensure queries are not executed
  147. // without the proper values.
  148. if(property_exists($node, "op") and $node->op != 'Save') {
  149. return;
  150. }
  151. // we are syncing if we do not have a node ID but we do have a featuremap_id. We don't
  152. // need to validate during syncing so just skip it.
  153. if (!property_exists($node, 'nid') and property_exists($node, 'featuremap_id') and $node->featuremap_id != 0) {
  154. return;
  155. }
  156. if ($node->unittype_id == 0) {
  157. form_set_error('unittype_id', 'Please provide a unit type for this map.');
  158. }
  159. // trim white space from text fields
  160. $node->fmapname = property_exists($node, 'fmapname') ? trim($node->fmapname) : '';
  161. $featuremap = 0;
  162. // check to make sure the unique name on the map is unique
  163. // before we try to insert into chado. If this is an update then we will
  164. // have a featuremap_id, therefore we want to look for another map with this
  165. // name but with a different featuremap_id. If this is an insert, just look
  166. // for a case where the name already exists.
  167. if (property_exists($node, 'featuremap_id')) {
  168. $sql = "
  169. SELECT * FROM {featuremap}
  170. WHERE name = :name AND NOT featuremap_id = :featuremap_id
  171. ";
  172. $featuremap = chado_query($sql, array(':name' => $node->fmapname, ':featuremap_id' => $node->featuremap_id))->fetchObject();
  173. }
  174. else {
  175. $sql = "SELECT * FROM {featuremap} WHERE name = :name";
  176. $featuremap = chado_query($sql, array(':name' => $node->fmapname))->fetchObject();
  177. }
  178. if ($featuremap) {
  179. form_set_error('fmapname', t('The unique map name already exists. Please choose another'));
  180. }
  181. }
  182. /**
  183. * Implement hook_node_access().
  184. *
  185. * This hook allows node modules to limit access to the node types they define.
  186. *
  187. * @param $node
  188. * The node on which the operation is to be performed, or, if it does not yet exist, the
  189. * type of node to be created
  190. *
  191. * @param $op
  192. * The operation to be performed
  193. *
  194. * @param $account
  195. * A user object representing the user for whom the operation is to be performed
  196. *
  197. * @return
  198. * If the permission for the specified operation is not set then return FALSE. If the
  199. * permission is set then return NULL as this allows other modules to disable
  200. * access. The only exception is when the $op == 'create'. We will always
  201. * return TRUE if the permission is set.
  202. *
  203. * @ingroup tripal_featuremap
  204. */
  205. function tripal_featuremap_node_access($node, $op, $account) {
  206. $node_type = $node;
  207. if (is_object($node)) {
  208. $node_type = $node->type;
  209. }
  210. if($node_type == 'chado_featuremap') {
  211. if ($op == 'create') {
  212. if (!user_access('create chado_featuremap content', $account)) {
  213. return NODE_ACCESS_DENY;
  214. }
  215. return NODE_ACCESS_ALLOW;
  216. }
  217. if ($op == 'update') {
  218. if (!user_access('edit chado_featuremap content', $account)) {
  219. return NODE_ACCESS_DENY;
  220. }
  221. }
  222. if ($op == 'delete') {
  223. if (!user_access('delete chado_featuremap content', $account)) {
  224. return NODE_ACCESS_DENY;
  225. }
  226. }
  227. if ($op == 'view') {
  228. if (!user_access('access chado_featuremap content', $account)) {
  229. return NODE_ACCESS_DENY;
  230. }
  231. }
  232. return NODE_ACCESS_IGNORE;
  233. }
  234. }
  235. /**
  236. * Implements hook_insert().
  237. *
  238. * When a new chado_featuremap node is created we also need to add information
  239. * to our chado_featuremap table. This function is called on insert of a new node
  240. * of type 'chado_featuremap' and inserts the necessary information.
  241. *
  242. * @ingroup tripal_featuremap
  243. */
  244. function chado_featuremap_insert($node) {
  245. $featuremap_id = '';
  246. // if there is an featuremap_id in the $node object then this must be a sync so
  247. // we can skip adding the featuremap as it is already there, although
  248. // we do need to proceed with insertion into the chado/drupal linking table.
  249. if (!property_exists($node, 'featuremap_id')) {
  250. $node->fmapname = trim($node->fmapname);
  251. $node->description = trim($node->description['value']);
  252. $values = array(
  253. 'name' => $node->fmapname,
  254. 'description' => $node->description,
  255. 'unittype_id' => $node->unittype_id
  256. );
  257. $featuremap = chado_insert_record('featuremap', $values);
  258. if(!$featuremap) {
  259. drupal_set_message(t('Unable to add featuremap.', 'warning'));
  260. tripal_report_error('tripal_featuremap', TRIPAL_WARNING, 'Unable to create feature map where values: %values',
  261. array('%values' => print_r($values, TRUE)));
  262. return;
  263. }
  264. $featuremap_id = $featuremap['featuremap_id'];
  265. // now add in the properties
  266. $properties = chado_retrieve_node_form_properties($node);
  267. // We need to deal with the 'Map Dbxref' property specially
  268. $cvterm = chado_select_record(
  269. 'cvterm',
  270. array('cvterm_id'),
  271. array('name' => 'Map Dbxref', 'cv_id' => array('name' => 'featuremap_property'))
  272. );
  273. $map_dbxref_cvterm_id = $cvterm[0]->cvterm_id;
  274. if (isset($properties[$map_dbxref_cvterm_id])) {
  275. foreach ($properties[$map_dbxref_cvterm_id] as $rank => $value) {
  276. $featuremap_dbxref = tripal_featuremap_add_featuremap_dbxref($featuremap_id, $value);
  277. if (!$featuremap_dbxref) {
  278. drupal_set_message("Error cannot add featuremap cross reference: $value", "error");
  279. tripal_report_error('t_featuremap', TRIPAL_ERROR, "Error cannot add featuremap cross reference: %ref",
  280. array('%ref' => $value));
  281. }
  282. }
  283. unset($properties[$map_dbxref_cvterm_id]);
  284. }
  285. $details = array(
  286. 'property_table' => 'featuremapprop',
  287. 'base_table' => 'featuremap',
  288. 'foreignkey_name' => 'featuremap_id',
  289. 'foreignkey_value' => $featuremap_id
  290. );
  291. chado_update_node_form_properties($node, $details, $properties);
  292. // * Additional DBxrefs Form *
  293. $details = array(
  294. 'linking_table' => 'featuremap_dbxref', // the name of your _dbxref table
  295. 'foreignkey_name' => 'featuremap_id', // the name of the key in your base table
  296. 'foreignkey_value' => $featuremap_id // the value of the featuremap_id key
  297. );
  298. chado_update_node_form_dbxrefs($node, $details);
  299. }
  300. else {
  301. $featuremap_id = $node->featuremap_id;
  302. }
  303. // Make sure the entry for this featuremap doesn't already exist in the
  304. // chado_featuremap table if it doesn't exist then we want to add it.
  305. $check_org_id = chado_get_id_from_nid('featuremap', $node->nid);
  306. if (!$check_org_id) {
  307. $record = new stdClass();
  308. $record->nid = $node->nid;
  309. $record->vid = $node->vid;
  310. $record->featuremap_id = $featuremap_id;
  311. drupal_write_record('chado_featuremap', $record);
  312. }
  313. }
  314. /**
  315. * Implements hook_update(). Update nodes
  316. *
  317. * @ingroup tripal_featuremap
  318. */
  319. function chado_featuremap_update($node) {
  320. $node->fmapname = trim($node->fmapname);
  321. $node->description = trim($node->description['value']);
  322. $featuremap_id = chado_get_id_from_nid('featuremap', $node->nid) ;
  323. // update the map record
  324. $match = array(
  325. 'featuremap_id' => $featuremap_id,
  326. );
  327. $values = array(
  328. 'name' => $node->fmapname,
  329. 'description' => $node->description,
  330. 'unittype_id' => $node->unittype_id
  331. );
  332. $status = chado_update_record('featuremap', $match, $values);
  333. if (!$status) {
  334. drupal_set_message("Error updating map", "error");
  335. tripal_report_error('t_featuremap', TRIPAL_ERROR, "Error updating map", array());
  336. return;
  337. }
  338. // Update the properties
  339. $properties = chado_retrieve_node_form_properties($node);
  340. // We need to deal with the 'Map Dbxref' property specially
  341. $cvterm = chado_select_record(
  342. 'cvterm',
  343. array('cvterm_id'),
  344. array('name' => 'Map Dbxref', 'cv_id' => array('name' => 'featuremap_property'))
  345. );
  346. $map_dbxref_cvterm_id = $cvterm[0]->cvterm_id;
  347. if (isset($properties[$map_dbxref_cvterm_id])) {
  348. foreach ($properties[$map_dbxref_cvterm_id] as $rank => $value) {
  349. $featuremap_dbxref = tripal_featuremap_add_featuremap_dbxref($featuremap_id, $value);
  350. if (!$featuremap_dbxref) {
  351. drupal_set_message("Error cannot add featuremap cross reference: $value", "error");
  352. tripal_report_error('t_featuremap', TRIPAL_ERROR, "Error cannot add featuremap cross reference: %ref",
  353. array('%ref' => $value));
  354. }
  355. }
  356. unset($properties[$map_dbxref_cvterm_id]);
  357. }
  358. $details = array(
  359. 'property_table' => 'featuremapprop',
  360. 'base_table' => 'featuremap',
  361. 'foreignkey_name' => 'featuremap_id',
  362. 'foreignkey_value' => $featuremap_id
  363. );
  364. chado_update_node_form_properties($node, $details, $properties);
  365. // * Additional DBxrefs Form *
  366. $details = array(
  367. 'linking_table' => 'featuremap_dbxref', // the name of your _dbxref table
  368. 'foreignkey_name' => 'featuremap_id', // the name of the key in your base table
  369. 'foreignkey_value' => $featuremap_id // the value of the featuremap_id key
  370. );
  371. chado_update_node_form_dbxrefs($node, $details);
  372. }
  373. /**
  374. * Implements hook_load().
  375. *
  376. * When a node is requested by the user this function is called to allow us
  377. * to add auxiliary data to the node object.
  378. *
  379. * @ingroup tripal_featuremap
  380. */
  381. function chado_featuremap_load($nodes) {
  382. foreach ($nodes as $nid => $node) {
  383. // get the feature details from chado
  384. $featuremap_id = chado_get_id_from_nid('featuremap', $node->nid);
  385. // if the nid does not have a matching record then skip this node.
  386. // this can happen with orphaned nodes.
  387. if (!$featuremap_id) {
  388. continue;
  389. }
  390. $values = array('featuremap_id' => $featuremap_id);
  391. $featuremap = chado_generate_var('featuremap', $values);
  392. // expand the description field as it is needed by the form
  393. $featuremap = chado_expand_var($featuremap, 'field', 'featuremap.description');
  394. $nodes[$nid]->featuremap = $featuremap;
  395. // Now get the title
  396. $node->title = chado_get_node_title($node);
  397. }
  398. }
  399. /**
  400. * Implements hook_delete().
  401. *
  402. * Delete data from drupal and chado databases when a node is deleted
  403. * @ingroup tripal_featuremap
  404. */
  405. function chado_featuremap_delete(&$node) {
  406. $featuremap_id = chado_get_id_from_nid('featuremap', $node->nid);
  407. // if we don't have a map id for this node then this isn't a node of
  408. // type chado_featuremap or the entry in the chado_featuremap table was lost.
  409. if (!$featuremap_id) {
  410. return;
  411. }
  412. // Remove data from {chado_featuremap}, {node} and {node_revisions} tables of
  413. // drupal database
  414. $sql_del = "DELETE FROM {chado_featuremap} WHERE nid = :nid AND vid = :vid";
  415. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  416. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  417. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  418. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  419. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  420. // Remove data from map and mapprop tables of chado database as well
  421. chado_query("DELETE FROM {featuremapprop} WHERE featuremap_id = :featuremap_id", array(':featuremap_id' => $featuremap_id));
  422. chado_query("DELETE FROM {featuremap_dbxref} WHERE featuremap_id = :featuremap_id", array(':featuremap_id' => $featuremap_id));
  423. chado_query("DELETE FROM {featuremap} WHERE featuremap_id = :featuremap_id", array(':featuremap_id' => $featuremap_id));
  424. }
  425. /**
  426. * Implements hook_node_presave(). Acts on all content types.
  427. *
  428. * @ingroup tripal_featuremap
  429. */
  430. function tripal_featuremap_node_presave($node) {
  431. switch ($node->type) {
  432. // This step is for setting the title for the Drupal node. This title
  433. // is permanent and thus is created to be unique. Title changes provided
  434. // by tokens are generated on the fly dynamically, but the node title
  435. // seen in the content listing needs to be set here. Do not call
  436. // the chado_get_node_title() function here to set the title as the node
  437. // object isn't properly filled out and the function will fail.
  438. case 'chado_featuremap':
  439. // for a form submission the 'fmapname' field will be set,
  440. // for a sync, we must pull from the featuremap object
  441. if (property_exists($node, 'fmapname')) {
  442. // set the title
  443. $node->title = $node->fmapname;
  444. }
  445. else if (property_exists($node, 'featuremap')) {
  446. $node->title = $node->featuremap->name;
  447. }
  448. break;
  449. }
  450. }
  451. /**
  452. * Implements hook_node_view(). Acts on all content types.
  453. *
  454. * @ingroup tripal_feature
  455. */
  456. function tripal_featuremap_node_view($node, $view_mode, $langcode) {
  457. switch ($node->type) {
  458. case 'chado_featuremap':
  459. // Show feature browser and counts
  460. if ($view_mode == 'full') {
  461. $node->content['tripal_featuremap_base'] = array(
  462. '#theme' => 'tripal_featuremap_base',
  463. '#node' => $node,
  464. '#tripal_toc_id' => 'base',
  465. '#tripal_toc_title' => 'Overview',
  466. '#weight' => -100,
  467. );
  468. $node->content['tripal_featuremap_featurepos'] = array(
  469. '#theme' => 'tripal_featuremap_featurepos',
  470. '#node' => $node,
  471. '#tripal_toc_id' => 'featurepos',
  472. '#tripal_toc_title' => 'Map Features',
  473. );
  474. $node->content['tripal_featuremap_properties'] = array(
  475. '#theme' => 'tripal_featuremap_properties',
  476. '#node' => $node,
  477. '#tripal_toc_id' => 'properties',
  478. '#tripal_toc_title' => 'Properties',
  479. );
  480. $node->content['tripal_featuremap_publication'] = array(
  481. '#theme' => 'tripal_featuremap_publication',
  482. '#node' => $node,
  483. '#tripal_toc_id' => 'publications',
  484. '#tripal_toc_title' => 'Publications',
  485. );
  486. $node->content['tripal_featuremap_references'] = array(
  487. '#theme' => 'tripal_featuremap_references',
  488. '#node' => $node,
  489. '#tripal_toc_id' => 'references',
  490. '#tripal_toc_title' => 'Cross References',
  491. );
  492. }
  493. if ($view_mode == 'teaser') {
  494. $node->content['tripal_featuremap_teaser'] = array(
  495. '#theme' => 'tripal_featuremap_teaser',
  496. '#node' => $node,
  497. );
  498. }
  499. break;
  500. case 'chado_feature':
  501. if ($view_mode == 'full') {
  502. $node->content['tripal_feature_featurepos'] = array(
  503. '#theme' => 'tripal_feature_featurepos',
  504. '#node' => $node,
  505. '#tripal_toc_id' => 'featurepos',
  506. '#tripal_toc_title' => 'Maps',
  507. );
  508. }
  509. break;
  510. }
  511. }
  512. /**
  513. * Implements hook_node_insert().
  514. * Acts on all content types.
  515. *
  516. * @ingroup tripal_featuremap
  517. */
  518. function tripal_featuremap_node_insert($node) {
  519. switch ($node->type) {
  520. case 'chado_featuremap':
  521. // get the feature details from chado
  522. $featuremap_id = chado_get_id_from_nid('featuremap', $node->nid);
  523. $values = array('featuremap_id' => $featuremap_id);
  524. $featuremap = chado_generate_var('featuremap', $values);
  525. $node->featuremap = $featuremap;
  526. // Now get the title
  527. $node->title = chado_get_node_title($node);
  528. // Now use the API to set the path.
  529. chado_set_node_url($node);
  530. break;
  531. }
  532. }
  533. /**
  534. * Implements hook_node_update().
  535. * Acts on all content types.
  536. *
  537. * @ingroup tripal_featuremap
  538. */
  539. function tripal_featuremap_node_update($node) {
  540. switch ($node->type) {
  541. case 'chado_featuremap':
  542. // Now get the title
  543. $node->title = chado_get_node_title($node);
  544. // Now use the API to set the path.
  545. chado_set_node_url($node);
  546. break;
  547. }
  548. }
  549. /**
  550. * Implements [content_type]_chado_node_default_title_format().
  551. *
  552. * Defines a default title format for the Chado Node API to set the titles on
  553. * Chado featuremap nodes based on chado fields.
  554. */
  555. function chado_featuremap_chado_node_default_title_format() {
  556. return '[featuremap.name]';
  557. }
  558. /**
  559. * Implements hook_chado_node_default_url_format().
  560. *
  561. * Designates a default URL format for featuremap nodes.
  562. */
  563. function chado_featuremap_chado_node_default_url_format() {
  564. return '/featuremap/[featuremap.featuremap_id]';
  565. }