tripal_featuremap.chado_node.inc 20 KB

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