tripal_featuremap.chado_node.inc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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' => 'textarea',
  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. // if this is a delete then don't validate
  145. if($node->op == 'Delete') {
  146. return;
  147. }
  148. // we are syncing if we do not have a node ID but we do have a featuremap_id. We don't
  149. // need to validate during syncing so just skip it.
  150. if (is_null($node->nid) and property_exists($node, 'featuremap_id') and $node->featuremap_id != 0) {
  151. return;
  152. }
  153. // trim white space from text fields
  154. $node->fmapname = trim($node->fmapname);
  155. $node->description = trim($node->description);
  156. $featuremap = 0;
  157. // check to make sure the unique name on the map is unique
  158. // before we try to insert into chado. If this is an update then we will
  159. // have a featuremap_id, therefore we want to look for another map with this
  160. // name but with a different featuremap_id. If this is an insert, just look
  161. // for a case where the name already exists.
  162. if (property_exists($node, 'featuremap_id')) {
  163. $sql = "
  164. SELECT * FROM {featuremap}
  165. WHERE name = :name AND NOT featuremap_id = :featuremap_id
  166. ";
  167. $featuremap = chado_query($sql, array(':name' => $node->fmapname, ':featuremap_id' => $node->featuremap_id))->fetchObject();
  168. }
  169. else {
  170. $sql = "SELECT * FROM {featuremap} WHERE name = :name";
  171. $featuremap = chado_query($sql, array(':name' => $node->fmapname))->fetchObject();
  172. }
  173. if ($featuremap) {
  174. form_set_error('fmapname', t('The unique map name already exists. Please choose another'));
  175. }
  176. }
  177. /**
  178. * Implement hook_node_access().
  179. *
  180. * This hook allows node modules to limit access to the node types they define.
  181. *
  182. * @param $node
  183. * The node on which the operation is to be performed, or, if it does not yet exist, the
  184. * type of node to be created
  185. *
  186. * @param $op
  187. * The operation to be performed
  188. *
  189. * @param $account
  190. * A user object representing the user for whom the operation is to be performed
  191. *
  192. * @return
  193. * If the permission for the specified operation is not set then return FALSE. If the
  194. * permission is set then return NULL as this allows other modules to disable
  195. * access. The only exception is when the $op == 'create'. We will always
  196. * return TRUE if the permission is set.
  197. *
  198. * @ingroup tripal_featuremap
  199. */
  200. function chado_featuremap_node_access($node, $op, $account) {
  201. $node_type = $node;
  202. if (is_object($node)) {
  203. $node_type = $node->type;
  204. }
  205. if($node_type == 'chado_featuremap') {
  206. if ($op == 'create') {
  207. if (!user_access('create chado_featuremap content', $account)) {
  208. return NODE_ACCESS_DENY;
  209. }
  210. return NODE_ACCESS_ALLOW;
  211. }
  212. if ($op == 'update') {
  213. if (!user_access('edit chado_featuremap content', $account)) {
  214. return NODE_ACCESS_DENY;
  215. }
  216. }
  217. if ($op == 'delete') {
  218. if (!user_access('delete chado_featuremap content', $account)) {
  219. return NODE_ACCESS_DENY;
  220. }
  221. }
  222. if ($op == 'view') {
  223. if (!user_access('access chado_featuremap content', $account)) {
  224. return NODE_ACCESS_DENY;
  225. }
  226. }
  227. return NODE_ACCESS_IGNORE;
  228. }
  229. }
  230. /**
  231. * Implements hook_insert().
  232. *
  233. * When a new chado_featuremap node is created we also need to add information
  234. * to our chado_featuremap table. This function is called on insert of a new node
  235. * of type 'chado_featuremap' and inserts the necessary information.
  236. *
  237. * @ingroup tripal_featuremap
  238. */
  239. function chado_featuremap_insert($node) {
  240. $node->fmapname = trim($node->fmapname);
  241. $node->description = trim($node->description);
  242. // if there is an featuremap_id in the $node object then this must be a sync so
  243. // we can skip adding the featuremap as it is already there, although
  244. // we do need to proceed with the rest of the insert
  245. if (!property_exists($node, 'featuremap_id')) {
  246. $values = array(
  247. 'name' => $node->fmapname,
  248. 'description' => $node->description,
  249. 'unittype_id' => $node->unittype_id
  250. );
  251. $featuremap = chado_insert_record('featuremap', $values);
  252. if(!$featuremap) {
  253. drupal_set_message(t('Unable to add featuremap.', 'warning'));
  254. tripal_report_error('tripal_featuremap', TRIPAL_WARNING, 'Unable to create feature map where values: %values',
  255. array('%values' => print_r($values, TRUE)));
  256. return;
  257. }
  258. $featuremap_id = $featuremap['featuremap_id'];
  259. // now add in the properties
  260. $properties = chado_retrieve_node_form_properties($node);
  261. // We need to deal with the 'Map Dbxref' property specially
  262. $cvterm = chado_select_record(
  263. 'cvterm',
  264. array('cvterm_id'),
  265. array('name' => 'Map Dbxref', 'cv_id' => array('name' => 'featuremap_property'))
  266. );
  267. $map_dbxref_cvterm_id = $cvterm[0]->cvterm_id;
  268. if (isset($properties[$map_dbxref_cvterm_id])) {
  269. foreach ($properties[$map_dbxref_cvterm_id] as $rank => $value) {
  270. $featuremap_dbxref = tripal_featuremap_add_featuremap_dbxref($featuremap_id, $value);
  271. if (!$featuremap_dbxref) {
  272. drupal_set_message("Error cannot add featuremap cross reference: $value", "error");
  273. tripal_report_error('t_featuremap', TRIPAL_ERROR, "Error cannot add featuremap cross reference: %ref",
  274. array('%ref' => $value));
  275. }
  276. }
  277. unset($properties[$map_dbxref_cvterm_id]);
  278. }
  279. $details = array(
  280. 'property_table' => 'featuremapprop',
  281. 'base_table' => 'featuremap',
  282. 'foreignkey_name' => 'featuremap_id',
  283. 'foreignkey_value' => $featuremap_id
  284. );
  285. chado_update_node_form_properties($node, $details, $properties);
  286. // * Additional DBxrefs Form *
  287. $details = array(
  288. 'linking_table' => 'featuremap_dbxref', // the name of your _dbxref table
  289. 'foreignkey_name' => 'featuremap_id', // the name of the key in your base table
  290. 'foreignkey_value' => $featuremap_id // the value of the featuremap_id key
  291. );
  292. chado_update_node_form_dbxrefs($node, $details);
  293. }
  294. else {
  295. $featuremap_id = $node->featuremap_id;
  296. }
  297. // Make sure the entry for this featuremap doesn't already exist in the
  298. // chado_featuremap table if it doesn't exist then we want to add it.
  299. $check_org_id = chado_get_id_from_nid('featuremap', $node->nid);
  300. if (!$check_org_id) {
  301. $record = new stdClass();
  302. $record->nid = $node->nid;
  303. $record->vid = $node->vid;
  304. $record->featuremap_id = $featuremap_id;
  305. drupal_write_record('chado_featuremap', $record);
  306. }
  307. }
  308. /**
  309. * Implements hook_update(). Update nodes
  310. *
  311. * @ingroup tripal_featuremap
  312. */
  313. function chado_featuremap_update($node) {
  314. $node->fmapname = trim($node->fmapname);
  315. $node->description = trim($node->description);
  316. $featuremap_id = chado_get_id_from_nid('featuremap', $node->nid) ;
  317. // update the map record
  318. $match = array(
  319. 'featuremap_id' => $featuremap_id,
  320. );
  321. $values = array(
  322. 'name' => $node->fmapname,
  323. 'description' => $node->description,
  324. 'unittype_id' => $node->unittype_id
  325. );
  326. $status = chado_update_record('featuremap', $match, $values);
  327. if (!$status) {
  328. drupal_set_message("Error updating map", "error");
  329. tripal_report_error('t_featuremap', TRIPAL_ERROR, "Error updating map", array());
  330. return;
  331. }
  332. // Update the properties
  333. $properties = chado_retrieve_node_form_properties($node);
  334. // We need to deal with the 'Map Dbxref' property specially
  335. $cvterm = chado_select_record(
  336. 'cvterm',
  337. array('cvterm_id'),
  338. array('name' => 'Map Dbxref', 'cv_id' => array('name' => 'featuremap_property'))
  339. );
  340. $map_dbxref_cvterm_id = $cvterm[0]->cvterm_id;
  341. if (isset($properties[$map_dbxref_cvterm_id])) {
  342. foreach ($properties[$map_dbxref_cvterm_id] as $rank => $value) {
  343. $featuremap_dbxref = tripal_featuremap_add_featuremap_dbxref($featuremap_id, $value);
  344. if (!$featuremap_dbxref) {
  345. drupal_set_message("Error cannot add featuremap cross reference: $value", "error");
  346. tripal_report_error('t_featuremap', TRIPAL_ERROR, "Error cannot add featuremap cross reference: %ref",
  347. array('%ref' => $value));
  348. }
  349. }
  350. unset($properties[$map_dbxref_cvterm_id]);
  351. }
  352. $details = array(
  353. 'property_table' => 'featuremapprop',
  354. 'base_table' => 'featuremap',
  355. 'foreignkey_name' => 'featuremap_id',
  356. 'foreignkey_value' => $featuremap_id
  357. );
  358. chado_update_node_form_properties($node, $details, $properties);
  359. // * Additional DBxrefs Form *
  360. $details = array(
  361. 'linking_table' => 'featuremap_dbxref', // the name of your _dbxref table
  362. 'foreignkey_name' => 'featuremap_id', // the name of the key in your base table
  363. 'foreignkey_value' => $featuremap_id // the value of the featuremap_id key
  364. );
  365. chado_update_node_form_dbxrefs($node, $details);
  366. }
  367. /**
  368. * Implements hook_load().
  369. *
  370. * When a node is requested by the user this function is called to allow us
  371. * to add auxiliary data to the node object.
  372. *
  373. * @ingroup tripal_featuremap
  374. */
  375. function chado_featuremap_load($nodes) {
  376. foreach ($nodes as $nid => $node) {
  377. // get the feature details from chado
  378. $featuremap_id = chado_get_id_from_nid('featuremap', $node->nid);
  379. // if the nid does not have a matching record then skip this node.
  380. // this can happen with orphaned nodes.
  381. if (!$featuremap_id) {
  382. continue;
  383. }
  384. $values = array('featuremap_id' => $featuremap_id);
  385. $featuremap = chado_generate_var('featuremap', $values);
  386. // expand the description field as it is needed by the form
  387. $featuremap = chado_expand_var($featuremap, 'field', 'featuremap.description');
  388. $nodes[$nid]->featuremap = $featuremap;
  389. // Now get the title
  390. $node->title = chado_get_node_title($node);
  391. }
  392. }
  393. /**
  394. * Implements hook_delete().
  395. *
  396. * Delete data from drupal and chado databases when a node is deleted
  397. * @ingroup tripal_featuremap
  398. */
  399. function chado_featuremap_delete(&$node) {
  400. $featuremap_id = chado_get_id_from_nid('featuremap', $node->nid);
  401. // if we don't have a map id for this node then this isn't a node of
  402. // type chado_featuremap or the entry in the chado_featuremap table was lost.
  403. if (!$featuremap_id) {
  404. return;
  405. }
  406. // Remove data from {chado_featuremap}, {node} and {node_revisions} tables of
  407. // drupal database
  408. $sql_del = "DELETE FROM {chado_featuremap} WHERE nid = :nid AND vid = :vid";
  409. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  410. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  411. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  412. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  413. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  414. // Remove data from map and mapprop tables of chado database as well
  415. chado_query("DELETE FROM {featuremapprop} WHERE featuremap_id = :featuremap_id", array(':featuremap_id' => $featuremap_id));
  416. chado_query("DELETE FROM {featuremap_dbxref} WHERE featuremap_id = :featuremap_id", array(':featuremap_id' => $featuremap_id));
  417. chado_query("DELETE FROM {featuremap} WHERE featuremap_id = :featuremap_id", array(':featuremap_id' => $featuremap_id));
  418. }
  419. /**
  420. * Implements hook_node_presave(). Acts on all content types.
  421. *
  422. * @ingroup tripal_featuremap
  423. */
  424. function tripal_featuremap_node_presave($node) {
  425. switch ($node->type) {
  426. // This step is for setting the title for the Drupal node. This title
  427. // is permanent and thus is created to be unique. Title changes provided
  428. // by tokens are generated on the fly dynamically, but the node title
  429. // seen in the content listing needs to be set here. Do not call
  430. // the chado_get_node_title() function here to set the title as the node
  431. // object isn't properly filled out and the function will fail.
  432. case 'chado_featuremap':
  433. // for a form submission the 'fmapname' field will be set,
  434. // for a sync, we must pull from the featuremap object
  435. if (property_exists($node, 'fmapname')) {
  436. // set the title
  437. $node->title = $node->fmapname;
  438. }
  439. else if (property_exists($node, 'featuremap')) {
  440. $node->title = $node->featuremap->name;
  441. }
  442. break;
  443. }
  444. }
  445. /**
  446. * Implements hook_node_view(). Acts on all content types.
  447. *
  448. * @ingroup tripal_feature
  449. */
  450. function tripal_featuremap_node_view($node, $view_mode, $langcode) {
  451. switch ($node->type) {
  452. case 'chado_featuremap':
  453. // Show feature browser and counts
  454. if ($view_mode == 'full') {
  455. $node->content['tripal_featuremap_base'] = array(
  456. '#theme' => 'tripal_featuremap_base',
  457. '#node' => $node,
  458. '#tripal_toc_id' => 'base',
  459. '#tripal_toc_title' => 'Overview',
  460. '#weight' => -100,
  461. );
  462. $node->content['tripal_featuremap_featurepos'] = array(
  463. '#theme' => 'tripal_featuremap_featurepos',
  464. '#node' => $node,
  465. '#tripal_toc_id' => 'featurepos',
  466. '#tripal_toc_title' => 'Map Features',
  467. );
  468. $node->content['tripal_featuremap_properties'] = array(
  469. '#theme' => 'tripal_featuremap_properties',
  470. '#node' => $node,
  471. '#tripal_toc_id' => 'properties',
  472. '#tripal_toc_title' => 'Properties',
  473. );
  474. $node->content['tripal_featuremap_publication'] = array(
  475. '#theme' => 'tripal_featuremap_publication',
  476. '#node' => $node,
  477. '#tripal_toc_id' => 'publications',
  478. '#tripal_toc_title' => 'Publications',
  479. );
  480. $node->content['tripal_featuremap_references'] = array(
  481. '#theme' => 'tripal_featuremap_references',
  482. '#node' => $node,
  483. '#tripal_toc_id' => 'references',
  484. '#tripal_toc_title' => 'Cross References',
  485. );
  486. }
  487. if ($view_mode == 'teaser') {
  488. $node->content['tripal_featuremap_teaser'] = array(
  489. '#theme' => 'tripal_featuremap_teaser',
  490. '#node' => $node,
  491. );
  492. }
  493. break;
  494. case 'chado_feature':
  495. if ($view_mode == 'full') {
  496. $node->content['tripal_feature_featurepos'] = array(
  497. '#theme' => 'tripal_feature_featurepos',
  498. '#node' => $node,
  499. '#tripal_toc_id' => 'featurepos',
  500. '#tripal_toc_title' => 'Maps',
  501. );
  502. }
  503. break;
  504. }
  505. }
  506. /**
  507. * Implements hook_node_insert().
  508. * Acts on all content types.
  509. *
  510. * @ingroup tripal_featuremap
  511. */
  512. function tripal_featuremap_node_insert($node) {
  513. switch ($node->type) {
  514. case 'chado_featuremap':
  515. // get the feature details from chado
  516. $featuremap_id = chado_get_id_from_nid('featuremap', $node->nid);
  517. $values = array('featuremap_id' => $featuremap_id);
  518. $featuremap = chado_generate_var('featuremap', $values);
  519. $node->featuremap = $featuremap;
  520. // Now get the title
  521. $node->title = chado_get_node_title($node);
  522. // Now use the API to set the path.
  523. chado_set_node_url($node);
  524. break;
  525. }
  526. }
  527. /**
  528. * Implements hook_node_update().
  529. * Acts on all content types.
  530. *
  531. * @ingroup tripal_featuremap
  532. */
  533. function tripal_featuremap_node_update($node) {
  534. switch ($node->type) {
  535. case 'chado_featuremap':
  536. // Now get the title
  537. $node->title = chado_get_node_title($node);
  538. // Now use the API to set the path.
  539. chado_set_node_url($node);
  540. break;
  541. }
  542. }
  543. /**
  544. * Implements [content_type]_chado_node_default_title_format().
  545. *
  546. * Defines a default title format for the Chado Node API to set the titles on
  547. * Chado featuremap nodes based on chado fields.
  548. */
  549. function chado_featuremap_chado_node_default_title_format() {
  550. return '[featuremap.name]';
  551. }
  552. /**
  553. * Implements hook_chado_node_default_url_format().
  554. *
  555. * Designates a default URL format for featuremap nodes.
  556. */
  557. function chado_featuremap_chado_node_default_url_format() {
  558. return '/featuremap/[featuremap.featuremap_id]';
  559. }