tripal_featuremap.chado_node.inc 18 KB

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