tripal_featuremap.chado_node.inc 18 KB

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