tripal_featuremap.chado_node.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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' => FALSE,
  21. 'title_label' => t('Feature Map'),
  22. 'has_body' => FALSE,
  23. 'body_label' => t('Feature Map Description'),
  24. 'locked' => TRUE,
  25. 'chado_node_api' => array(
  26. 'base_table' => 'featuremap',
  27. 'hook_prefix' => 'chado_featuremap',
  28. 'record_type_title' => array(
  29. 'singular' => t('Feature Map'),
  30. 'plural' => t('Feature Maps')
  31. ),
  32. 'sync_filters' => array(
  33. 'type_id' => FALSE,
  34. 'organism_id' => FALSE
  35. ),
  36. )
  37. );
  38. return $nodes;
  39. }
  40. /**
  41. * When editing or creating a new node of type 'chado_featuremap' we need
  42. * a form. This function creates the form that will be used for this.
  43. *
  44. * @ingroup tripal_featuremap
  45. */
  46. function chado_featuremap_form($node, &$form_state) {
  47. $form = array();
  48. // Default values can come in the following ways:
  49. //
  50. // 1) as elements of the $node object. This occurs when editing an existing library
  51. // 2) in the $form_state['values'] array which occurs on a failed validation or
  52. // ajax callbacks from non submit form elements
  53. // 3) in the $form_state['input'[ array which occurs on ajax callbacks from submit
  54. // form elements and the form is being rebuilt
  55. //
  56. // set form field defaults
  57. $featuremap_id = NULL;
  58. $fmapname = '';
  59. $description = '';
  60. $unittype_id = '';
  61. // if we are editing an existing node then the featuremap is already part of the node
  62. if (property_exists($node, 'featuremap')) {
  63. $featuremap = $node->featuremap;
  64. $featuremap = chado_expand_var($featuremap, 'field', 'featuremap.description');
  65. $featuremap_id = $featuremap->featuremap_id;
  66. // get form defaults
  67. $fmapname = $featuremap->name;
  68. $description = $featuremap->description;
  69. $unittype_id = $featuremap->unittype_id->cvterm_id;
  70. // set the featuremap_id in the form
  71. $form['featuremap_id'] = array(
  72. '#type' => 'hidden',
  73. '#value' => $featuremap_id,
  74. );
  75. }
  76. // if we are re constructing the form from a failed validation or ajax callback
  77. // then use the $form_state['values'] values
  78. if (array_key_exists('values', $form_state)) {
  79. $fmapname = $form_state['values']['fmapname'];
  80. $description = $form_state['values']['description'];
  81. $unittype_id = $form_state['values']['unittype_id'];
  82. }
  83. // if we are re building the form from after submission (from ajax call) then
  84. // the values are in the $form_state['input'] array
  85. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  86. $fmapname = $form_state['input']['fmapname'];
  87. $description = $form_state['input']['description'];
  88. $unittype_id = $form_state['input']['unittype_id'];
  89. }
  90. $form['fmapname']= array(
  91. '#type' => 'textfield',
  92. '#title' => t('Map Name'),
  93. '#description' => t('Please enter a name for this map'),
  94. '#required' => TRUE,
  95. '#default_value' => $fmapname,
  96. '#maxlength' => 255
  97. );
  98. $form['description']= array(
  99. '#type' => 'textarea',
  100. '#title' => t('Map Description'),
  101. '#description' => t('A description of the map.'),
  102. '#required' => TRUE,
  103. '#default_value' => $description,
  104. );
  105. // get the list of unit types
  106. $values = array(
  107. 'cv_id' => array(
  108. 'name' => 'featuremap_units',
  109. )
  110. );
  111. $columns = array('cvterm_id','name');
  112. $options = array('order_by' => array('name' => 'ASC'));
  113. $featuremap_units = chado_select_record('cvterm', $columns, $values, $options);
  114. $units = array();
  115. $units[''] = '';
  116. foreach($featuremap_units as $unit) {
  117. $units[$unit->cvterm_id] = $unit->name;
  118. }
  119. $form['unittype_id'] = array(
  120. '#title' => t('Map Units'),
  121. '#type' => t('select'),
  122. '#description' => t("Chose the units for this map"),
  123. '#required' => TRUE,
  124. '#default_value' => $unittype_id,
  125. '#options' => $units,
  126. );
  127. // Properties Form
  128. // ----------------------------------
  129. $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") . ".");
  130. $details = array(
  131. 'property_table' => 'featuremapprop',
  132. 'chado_id' => $featuremap_id,
  133. 'cv_name' => 'featuremap_property',
  134. 'fieldset_name' => 'Additional Details',
  135. 'additional_instructions' => $instructions
  136. );
  137. chado_add_node_form_properties($form, $form_state, $details);
  138. // ADDITIONAL DBXREFS FORM
  139. //---------------------------------------------
  140. $details = array(
  141. 'linking_table' => 'featuremap_dbxref', // the name of the _dbxref table
  142. 'base_foreign_key' => 'featuremap_id', // the name of the key in your base chado table
  143. 'base_key_value' => $featuremap_id // the value of featuremap_id for this record
  144. );
  145. // Adds the form elements to your current form
  146. chado_add_node_form_dbxrefs($form, $form_state, $details);
  147. return $form;
  148. }
  149. /**
  150. * Validates submission of form when adding or updating a map node
  151. *
  152. * @ingroup tripal_featuremap
  153. */
  154. function chado_featuremap_validate($node, $form, &$form_state) {
  155. $node->fmapname = trim($node->fmapname);
  156. $node->description = trim($node->description);
  157. // if this is a delete then don't validate
  158. if($node->op == 'Delete') {
  159. return;
  160. }
  161. // we are syncing if we do not have a node ID but we do have a featuremap_id. We don't
  162. // need to validate during syncing so just skip it.
  163. if (is_null($node->nid) and property_exists($node, 'featuremap_id') and $node->featuremap_id != 0) {
  164. return;
  165. }
  166. $featuremap = 0;
  167. // check to make sure the unique name on the map is unique
  168. // before we try to insert into chado. If this is an update then we will
  169. // have a featuremap_id, therefore we want to look for another map with this
  170. // name but with a different featuremap_id. If this is an insert, just look
  171. // for a case where the name already exists.
  172. if (property_exists($node, 'featuremap_id')) {
  173. $sql = "
  174. SELECT * FROM {featuremap}
  175. WHERE name = :name AND NOT featuremap_id = :featuremap_id
  176. ";
  177. $featuremap = chado_query($sql, array(':name' => $node->fmapname, ':featuremap_id' => $node->featuremap_id))->fetchObject();
  178. }
  179. else {
  180. $sql = "SELECT * FROM {featuremap} WHERE name = :name";
  181. $featuremap = chado_query($sql, array(':name' => $node->fmapname))->fetchObject();
  182. }
  183. if ($featuremap) {
  184. form_set_error('fmapname', t('The unique map name already exists. Please choose another'));
  185. }
  186. }
  187. /**
  188. * Implement hook_node_access().
  189. *
  190. * This hook allows node modules to limit access to the node types they define.
  191. *
  192. * @param $node
  193. * The node on which the operation is to be performed, or, if it does not yet exist, the
  194. * type of node to be created
  195. *
  196. * @param $op
  197. * The operation to be performed
  198. *
  199. * @param $account
  200. * A user object representing the user for whom the operation is to be performed
  201. *
  202. * @return
  203. * If the permission for the specified operation is not set then return FALSE. If the
  204. * permission is set then return NULL as this allows other modules to disable
  205. * access. The only exception is when the $op == 'create'. We will always
  206. * return TRUE if the permission is set.
  207. *
  208. * @ingroup tripal_featuremap
  209. */
  210. function chado_featuremap_node_access($node, $op, $account) {
  211. if ($op == 'create') {
  212. if (!user_access('create chado_featuremap content', $account)) {
  213. return FALSE;
  214. }
  215. return TRUE;
  216. }
  217. if ($op == 'update') {
  218. if (!user_access('edit any chado_featuremap content', $account) &&
  219. !user_access('edit own chado_featuremap content', $account)) {
  220. return FALSE;
  221. }
  222. if (user_access('edit own chado_featuremap content', $account) &&
  223. $account->uid != $node->uid) {
  224. return FALSE;
  225. }
  226. }
  227. if ($op == 'delete') {
  228. if (!user_access('delete any chado_featuremap content', $account) &&
  229. !user_access('delete own chado_featuremap content', $account)) {
  230. return FALSE;
  231. }
  232. if (user_access('delete own chado_featuremap content', $account) &&
  233. $account->uid != $node->uid) {
  234. return FALSE;
  235. }
  236. }
  237. return NULL;
  238. }
  239. /**
  240. * Implements hook_insert().
  241. *
  242. * When a new chado_featuremap node is created we also need to add information
  243. * to our chado_featuremap table. This function is called on insert of a new node
  244. * of type 'chado_featuremap' and inserts the necessary information.
  245. *
  246. * @ingroup tripal_featuremap
  247. */
  248. function chado_featuremap_insert($node) {
  249. $node->fmapname = trim($node->fmapname);
  250. $node->description = trim($node->description);
  251. // if there is an featuremap_id in the $node object then this must be a sync so
  252. // we can skip adding the featuremap as it is already there, although
  253. // we do need to proceed with the rest of the insert
  254. if (!property_exists($node, 'featuremap_id')) {
  255. $values = array(
  256. 'name' => $node->fmapname,
  257. 'description' => $node->description,
  258. 'unittype_id' => $node->unittype_id
  259. );
  260. $featuremap = chado_insert_record('featuremap', $values);
  261. if(!$featuremap) {
  262. drupal_set_message(t('Unable to add featuremap.', 'warning'));
  263. tripal_report_error('tripal_featuremap', TRIPAL_WARNING, 'Unable to create feature map where values: %values',
  264. array('%values' => print_r($values, TRUE)));
  265. return;
  266. }
  267. $featuremap_id = $featuremap['featuremap_id'];
  268. // now add in the properties
  269. $properties = chado_retrieve_node_form_properties($node);
  270. // We need to deal with the 'Map Dbxref' property specially
  271. $cvterm = chado_select_record(
  272. 'cvterm',
  273. array('cvterm_id'),
  274. array('name' => 'Map Dbxref', 'cv_id' => array('name' => 'featuremap_property'))
  275. );
  276. $map_dbxref_cvterm_id = $cvterm[0]->cvterm_id;
  277. if (isset($properties[$map_dbxref_cvterm_id])) {
  278. foreach ($properties[$map_dbxref_cvterm_id] as $rank => $value) {
  279. $featuremap_dbxref = tripal_featuremap_add_featuremap_dbxref($featuremap_id, $value);
  280. if (!$featuremap_dbxref) {
  281. drupal_set_message("Error cannot add featuremap cross reference: $value", "error");
  282. tripal_report_error('t_featuremap', TRIPAL_ERROR, "Error cannot add featuremap cross reference: %ref",
  283. array('%ref' => $value));
  284. }
  285. }
  286. unset($properties[$map_dbxref_cvterm_id]);
  287. }
  288. $details = array(
  289. 'property_table' => 'featuremapprop',
  290. 'base_table' => 'featuremap',
  291. 'foreignkey_name' => 'featuremap_id',
  292. 'foreignkey_value' => $featuremap_id
  293. );
  294. chado_update_node_form_properties($node, $details, $properties);
  295. // * Additional DBxrefs Form *
  296. $details = array(
  297. 'linking_table' => 'featuremap_dbxref', // the name of your _dbxref table
  298. 'foreignkey_name' => 'featuremap_id', // the name of the key in your base table
  299. 'foreignkey_value' => $featuremap_id // the value of the featuremap_id key
  300. );
  301. chado_update_node_form_dbxrefs($node, $details);
  302. }
  303. else {
  304. $featuremap_id = $node->featuremap_id;
  305. }
  306. // Make sure the entry for this featuremap doesn't already exist in the
  307. // chado_featuremap table if it doesn't exist then we want to add it.
  308. $check_org_id = chado_get_id_from_nid('featuremap', $node->nid);
  309. if (!$check_org_id) {
  310. $record = new stdClass();
  311. $record->nid = $node->nid;
  312. $record->vid = $node->vid;
  313. $record->featuremap_id = $featuremap_id;
  314. drupal_write_record('chado_featuremap', $record);
  315. }
  316. }
  317. /**
  318. * Implements hook_update(). Update nodes
  319. *
  320. * @ingroup tripal_featuremap
  321. */
  322. function chado_featuremap_update($node) {
  323. $node->fmapname = trim($node->fmapname);
  324. $node->description = trim($node->description);
  325. $featuremap_id = chado_get_id_from_nid('featuremap', $node->nid) ;
  326. // update the map record
  327. $match = array(
  328. 'featuremap_id' => $featuremap_id,
  329. );
  330. $values = array(
  331. 'name' => $node->fmapname,
  332. 'description' => $node->description,
  333. 'unittype_id' => $node->unittype_id
  334. );
  335. $status = chado_update_record('featuremap', $match, $values);
  336. if (!$status) {
  337. drupal_set_message("Error updating map", "error");
  338. tripal_report_error('t_featuremap', TRIPAL_ERROR, "Error updating map", array());
  339. return;
  340. }
  341. // Update the properties
  342. $properties = chado_retrieve_node_form_properties($node);
  343. // We need to deal with the 'Map Dbxref' property specially
  344. $cvterm = chado_select_record(
  345. 'cvterm',
  346. array('cvterm_id'),
  347. array('name' => 'Map Dbxref', 'cv_id' => array('name' => 'featuremap_property'))
  348. );
  349. $map_dbxref_cvterm_id = $cvterm[0]->cvterm_id;
  350. if (isset($properties[$map_dbxref_cvterm_id])) {
  351. foreach ($properties[$map_dbxref_cvterm_id] as $rank => $value) {
  352. $featuremap_dbxref = tripal_featuremap_add_featuremap_dbxref($featuremap_id, $value);
  353. if (!$featuremap_dbxref) {
  354. drupal_set_message("Error cannot add featuremap cross reference: $value", "error");
  355. tripal_report_error('t_featuremap', TRIPAL_ERROR, "Error cannot add featuremap cross reference: %ref",
  356. array('%ref' => $value));
  357. }
  358. }
  359. unset($properties[$map_dbxref_cvterm_id]);
  360. }
  361. $details = array(
  362. 'property_table' => 'featuremapprop',
  363. 'base_table' => 'featuremap',
  364. 'foreignkey_name' => 'featuremap_id',
  365. 'foreignkey_value' => $featuremap_id
  366. );
  367. chado_update_node_form_properties($node, $details, $properties);
  368. // * Additional DBxrefs Form *
  369. $details = array(
  370. 'linking_table' => 'featuremap_dbxref', // the name of your _dbxref table
  371. 'foreignkey_name' => 'featuremap_id', // the name of the key in your base table
  372. 'foreignkey_value' => $featuremap_id // the value of the featuremap_id key
  373. );
  374. chado_update_node_form_dbxrefs($node, $details);
  375. }
  376. /**
  377. * Implements hook_load().
  378. *
  379. * When a node is requested by the user this function is called to allow us
  380. * to add auxiliary data to the node object.
  381. *
  382. * @ingroup tripal_featuremap
  383. */
  384. function chado_featuremap_load($nodes) {
  385. foreach ($nodes as $nid => $node) {
  386. // get the feature details from chado
  387. $featuremap_id = chado_get_id_from_nid('featuremap', $node->nid);
  388. $values = array('featuremap_id' => $featuremap_id);
  389. $featuremap = chado_generate_var('featuremap', $values);
  390. // expand the description field as it is needed by the form
  391. $featuremap = chado_expand_var($featuremap, 'field', 'featuremap.description');
  392. $nodes[$nid]->featuremap = $featuremap;
  393. }
  394. }
  395. /**
  396. * Implements hook_delete().
  397. *
  398. * Delete data from drupal and chado databases when a node is deleted
  399. * @ingroup tripal_featuremap
  400. */
  401. function chado_featuremap_delete(&$node) {
  402. $featuremap_id = chado_get_id_from_nid('featuremap', $node->nid);
  403. // if we don't have a map id for this node then this isn't a node of
  404. // type chado_featuremap or the entry in the chado_featuremap table was lost.
  405. if (!$featuremap_id) {
  406. return;
  407. }
  408. // Remove data from {chado_featuremap}, {node} and {node_revisions} tables of
  409. // drupal database
  410. $sql_del = "DELETE FROM {chado_featuremap} 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. $sql_del = "DELETE FROM {node_revisions} WHERE nid = :nid AND vid = :vid";
  415. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  416. // Remove data from map and mapprop tables of chado database as well
  417. chado_query("DELETE FROM {featuremapprop} WHERE featuremap_id = :featuremap_id", array(':featuremap_id' => $featuremap_id));
  418. chado_query("DELETE FROM {featuremap_dbxref} WHERE featuremap_id = :featuremap_id", array(':featuremap_id' => $featuremap_id));
  419. chado_query("DELETE FROM {featuremap} WHERE featuremap_id = :featuremap_id", array(':featuremap_id' => $featuremap_id));
  420. }
  421. /**
  422. * Implements hook_node_presave(). Acts on all content types.
  423. *
  424. * @ingroup tripal_featuremap
  425. */
  426. function tripal_featuremap_node_presave($node) {
  427. switch ($node->type) {
  428. case 'chado_featuremap':
  429. // for a form submission the 'fmapname' field will be set,
  430. // for a sync, we must pull from the featuremap object
  431. if(property_exists($node, 'fmapname')) {
  432. // set the title
  433. $node->title = $node->fmapname;
  434. }
  435. else {
  436. $node->title = $node->featuremap->name;
  437. }
  438. break;
  439. }
  440. }
  441. /**
  442. * Implements hook_node_view(). Acts on all content types.
  443. *
  444. * @ingroup tripal_feature
  445. */
  446. function tripal_featuremap_node_view($node, $view_mode, $langcode) {
  447. switch ($node->type) {
  448. case 'chado_featuremap':
  449. // Show feature browser and counts
  450. if ($view_mode == 'full') {
  451. $node->content['tripal_featuremap_base'] = array(
  452. '#markup' => theme('tripal_featuremap_base', array('node' => $node)),
  453. '#tripal_toc_id' => 'base',
  454. '#tripal_toc_title' => 'Overview',
  455. '#weight' => -100,
  456. );
  457. $node->content['tripal_featuremap_featurepos'] = array(
  458. '#markup' => theme('tripal_featuremap_featurepos', array('node' => $node)),
  459. '#tripal_toc_id' => 'featurepos',
  460. '#tripal_toc_title' => 'Map Features',
  461. );
  462. $node->content['tripal_featuremap_properties'] = array(
  463. '#markup' => theme('tripal_featuremap_properties', array('node' => $node)),
  464. '#tripal_toc_id' => 'properties',
  465. '#tripal_toc_title' => 'Properties',
  466. );
  467. $node->content['tripal_featuremap_publication'] = array(
  468. '#markup' => theme('tripal_featuremap_publication', array('node' => $node)),
  469. '#tripal_toc_id' => 'publications',
  470. '#tripal_toc_title' => 'Publications',
  471. );
  472. $node->content['tripal_featuremap_references'] = array(
  473. '#markup' => theme('tripal_featuremap_references', array('node' => $node)),
  474. '#tripal_toc_id' => 'references',
  475. '#tripal_toc_title' => 'Cross References',
  476. );
  477. }
  478. if ($view_mode == 'teaser') {
  479. $node->content['tripal_featuremap_teaser'] = array(
  480. '#markup' => theme('tripal_featuremap_teaser', array('node' => $node)),
  481. );
  482. }
  483. break;
  484. }
  485. }