tripal_project.chado_node.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. <?php
  2. /**
  3. * Implementation of hook_node_info().
  4. *
  5. * This node_info, is a simple node that describes the functionallity of the module. It specifies
  6. * that the title(Project Name) and body(Description) set to true so that they information can be
  7. * entered
  8. *
  9. *
  10. * @ingroup tripal_project
  11. */
  12. function tripal_project_node_info() {
  13. return array(
  14. 'chado_project' => array(
  15. 'name' => t('Project'),
  16. 'base' => 'chado_project',
  17. 'description' => t('A project from the Chado database'),
  18. 'has_title' => TRUE,
  19. 'title_label' => t('Project Name'),
  20. 'had_body' => TRUE,
  21. 'body_label' => t('Full Description'),
  22. 'chado_node_api' => array(
  23. 'base_table' => 'project',
  24. 'hook_prefix' => 'chado_project',
  25. 'record_type_title' => array(
  26. 'singular' => t('Project'),
  27. 'plural' => t('Projects')
  28. ),
  29. 'sync_filters' => array(
  30. 'type_id' => FALSE,
  31. 'organism_id' => FALSE
  32. ),
  33. ),
  34. ),
  35. );
  36. }
  37. /**
  38. * Implementation of hook_form().
  39. *
  40. * This form takes the Project Title information and description from the user.
  41. *
  42. * @parm $node
  43. * The initialized node
  44. *
  45. * @parm $form_state
  46. * The state of the form, that has the user entered information that is neccessary for adding
  47. * information to the project
  48. *
  49. * @return $form
  50. * An array as described by the Drupal Form API
  51. *
  52. *
  53. * @ingroup tripal_project
  54. */
  55. function chado_project_form(&$node, $form_state) {
  56. $form = array();
  57. // Default values can come in the following ways:
  58. //
  59. // 1) as elements of the $node object. This occurs when editing an existing project
  60. // 2) in the $form_state['values'] array which occurs on a failed validation or
  61. // ajax callbacks from non submit form elements
  62. // 3) in the $form_state['input'[ array which occurs on ajax callbacks from submit
  63. // form elements and the form is being rebuilt
  64. //
  65. // set form field defaults
  66. $project_id = null;
  67. $title = '';
  68. $description = '';
  69. // if we are editing an existing node then the project is already part of the node
  70. if (property_exists($node, 'project')) {
  71. $project = $node->project;
  72. // get the project default values. When this module was first created
  73. // the project description was incorrectly stored in the $node->body field.
  74. // It is better to store it in the Chado tables. However, the 'description'
  75. // field of the project table is only 255 characters. So, we are going
  76. // to follow the same as the project module and store the description in
  77. // the projectprop table and leave the project.description field blank.
  78. // however, for backwards compatibitily, we check to see if the description
  79. // is in the $node->body field. If it is we'll use that. When the node is
  80. // edited the text will be moved out of the body and into the projectprop
  81. // table where it should belong.
  82. if (property_exists($node, 'body')) {
  83. $description = $node->body;
  84. }
  85. else {
  86. $description = $project->description;
  87. }
  88. if (!$description) {
  89. $projectprop = tripal_project_get_property($project->project_id, 'Project Description');
  90. $description = $projectprop->value;
  91. }
  92. $title = $project->name;
  93. $project_id = $project->project_id;
  94. // keep track of the project id if we have. If we do have one then
  95. // this is an update as opposed to an insert.
  96. $form['project_id'] = array(
  97. '#type' => 'value',
  98. '#value' => $project_id,
  99. );
  100. }
  101. // if we are re constructing the form from a failed validation or ajax callback
  102. // then use the $form_state['values'] values
  103. if (array_key_exists('values', $form_state)) {
  104. $title = $form_state['values']['title'];
  105. $description = $form_state['values']['description'];
  106. }
  107. // if we are re building the form from after submission (from ajax call) then
  108. // the values are in the $form_state['input'] array
  109. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  110. $title = $form_state['input']['title'];
  111. $description = $form_state['input']['description'];
  112. }
  113. $form['title']= array(
  114. '#type' => 'textfield',
  115. '#title' => t('Project Title'),
  116. '#description' => t('Please enter the title for this project. This appears at the top of the project page.'),
  117. '#required' => TRUE,
  118. '#default_value' => $node->title,
  119. );
  120. $form['description']= array(
  121. '#type' => 'textarea',
  122. '#title' => t('Project Description'),
  123. '#description' => t('A brief description of the project'),
  124. '#required' => TRUE,
  125. '#default_value' => $description,
  126. );
  127. // Properties Form
  128. // ----------------------------------
  129. // we want to exclude the project description from being loaded as a stored property
  130. // because we want to use the property to replace the project.description field as it is
  131. // only 255 characters which isn't large enough. We don't want the user to set it
  132. // as a property even though it will be stored as a property.
  133. $cv_result = chado_select_record('cv',array('cv_id'),array('name' => 'project_property'));
  134. $cv_id = $cv_result[0]->cv_id;
  135. $select_options = tripal_cv_get_cvterm_options($cv_id);
  136. $descrip_id = array_search('Project Description', $select_options);
  137. unset($select_options[$descrip_id]);
  138. $instructions = t('To add properties to the drop down list, you must ' . l("add terms to the project_property vocabulary", "admin/tripal/chado/tripal_cv/cvterm/add") . ".");
  139. $details = array(
  140. 'property_table' => 'projectprop',
  141. 'base_foreign_key' => 'project_id',
  142. 'base_key_value' => $project_id,
  143. 'cv_name' => 'project_property',
  144. 'additional_instructions' => $instructions,
  145. 'select_options' => $select_options
  146. );
  147. chado_add_node_form_properties($form, $form_state, $details);
  148. // RELATIONSHIPS FORM
  149. //---------------------------------------------
  150. // We want to use the contact_relationship_types cv if there are any terms available
  151. // and if not, to default to the relationship ontology
  152. $cv_result = chado_select_record('cv',array('cv_id'),array('name' => 'project_relationship_types'));
  153. $cv_id = $cv_result[0]->cv_id;
  154. $select_options = tripal_cv_get_cvterm_options($cv_id);
  155. if (empty($select_options)) {
  156. $cv_result = chado_select_record('cv',array('cv_id'),array('name' => 'relationship'));
  157. $cv_id = $cv_result[0]->cv_id;
  158. $select_options = tripal_cv_get_cvterm_options($cv_id);
  159. }
  160. // D7 @TODO: tell tripal admin's about this
  161. $details = array(
  162. 'relationship_table' => 'project_relationship', // the name of the _relationship table
  163. 'base_table' => 'project', // the name of your chado base table
  164. 'base_foreign_key' => 'project_id', // the name of the key in your base chado table
  165. 'base_key_value' => $project_id, // the value of example_id for this record
  166. 'nodetype' => 'project', // the human-readable name of your node type
  167. 'cv_name' => 'project_relationship_types', // the cv.name of the cv governing example_relationship.type_id
  168. 'base_name_field' => 'name', // the base table field you want to be used as the name
  169. 'subject_field_name' => 'subject_project_id',
  170. 'object_field_name' => 'object_project_id',
  171. 'select_options' => $select_options
  172. );
  173. // Adds the form elements to your current form
  174. chado_add_node_form_relationships($form, $form_state, $details);
  175. return $form;
  176. }
  177. /**
  178. * validates submission of form when adding or updating a project node
  179. *
  180. * @ingroup tripal_project
  181. */
  182. function chado_project_validate($node, $form, &$form_state) {
  183. $node->title = trim($node->title);
  184. $node->description = trim($node->description);
  185. // if this is a delete then don't validate
  186. if($node->op == 'Delete') {
  187. return;
  188. }
  189. // we are syncing if we do not have a node ID but we do have a project_id. We don't
  190. // need to validate during syncing so just skip it.
  191. if (is_null($node->nid) and property_exists($node, 'project_id') and $node->project_id != 0) {
  192. return;
  193. }
  194. $project = 0;
  195. // check to make sure the name on the project is unique
  196. // before we try to insert into chado.
  197. if (property_exists($node, 'project_id')) {
  198. $sql = "SELECT * FROM {project} WHERE name = :name AND NOT project_id = :project_id";
  199. $project = chado_query($sql, array(':name' => $node->title, ':project_id' => $node->project_id))->fetchObject();
  200. }
  201. else {
  202. $sql = "SELECT * FROM {project} WHERE name = :name";
  203. $project = chado_query($sql, array(':name' => $node->title))->fetchObject();
  204. }
  205. if ($project) {
  206. form_set_error('title', t('The unique project name already exists. Please choose another'));
  207. }
  208. }
  209. /**
  210. * Implementation of hook_insert().
  211. *
  212. * @parm $node
  213. * Then node that has the information stored within, accessed given the nid
  214. *
  215. *
  216. * @ingroup tripal_project
  217. */
  218. function chado_project_insert($node) {
  219. $node->title = trim($node->title);
  220. $node->description = trim($node->description);
  221. // if there is an project_id in the $node object then this must be a sync so
  222. // we can skip adding the project as it is already there, although
  223. // we do need to proceed with the rest of the insert
  224. if (!property_exists($node, 'project_id')) {
  225. $values = array(
  226. 'name' => $node->title,
  227. 'description' => '',
  228. );
  229. $project = chado_insert_record('project', $values);
  230. if (!$project) {
  231. drupal_set_message(t('Unable to add project.', 'warning'));
  232. watchdog('tripal_project', 'Insert project: Unable to create project where values:%values',
  233. array('%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
  234. return;
  235. }
  236. $project_id = $project['project_id'];
  237. // * Properties Form *
  238. // Add the description property
  239. $properties = chado_retrieve_node_form_properties($node);
  240. $descrip_id = tripal_cv_get_cvterm_by_name('Project Description', NULL, 'project_property');
  241. $properties[$descrip_id->cvterm_id][0] = $node->description;
  242. $details = array(
  243. 'property_table' => 'projectprop',
  244. 'base_table' => 'project',
  245. 'foreignkey_name' => 'project_id',
  246. 'foreignkey_value' => $project_id
  247. );
  248. chado_update_node_form_properties($node, $details, $properties);
  249. // * Relationships Form *
  250. $details = array(
  251. 'relationship_table' => 'project_relationship', // name of the _relationship table
  252. 'foreignkey_value' => $project_id // value of the example_id key
  253. );
  254. chado_update_node_form_relationships($node, $details);
  255. }
  256. else {
  257. $project_id = $node->project_id;
  258. }
  259. // Make sure the entry for this project doesn't already exist in the
  260. // chado_project table if it doesn't exist then we want to add it.
  261. $check_org_id = chado_get_id_from_nid('project', $node->nid);
  262. if (!$check_org_id) {
  263. $record = new stdClass();
  264. $record->nid = $node->nid;
  265. $record->vid = $node->vid;
  266. $record->project_id = $project_id;
  267. drupal_write_record('chado_project', $record);
  268. }
  269. }
  270. /**
  271. *
  272. * Implementation of hook_delete().
  273. *
  274. * @param $node
  275. * The node which is to be deleted, only chado project and chado_project need to be dealt with
  276. * since the drupal node is deleted automagically
  277. *
  278. *
  279. * @ingroup tripal_project
  280. */
  281. function chado_project_delete($node) {
  282. $project_id = chado_get_id_from_nid('project', $node->nid);
  283. // if we don't have a project id for this node then this isn't a node of
  284. // type chado_project or the entry in the chado_project table was lost.
  285. if (!$project_id) {
  286. return;
  287. }
  288. // Remove data from {chado_project}, {node} and {node_revisions} tables of
  289. // drupal database
  290. $sql_del = "DELETE FROM {chado_project} WHERE nid = :nid AND vid = :vid";
  291. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  292. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vod";
  293. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  294. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  295. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  296. // Remove data from project and projectprop tables of chado database as well
  297. chado_query("DELETE FROM {projectprop} WHERE project_id = :project_id", array(':project_id' => $project_id));
  298. chado_query("DELETE FROM {project} WHERE project_id = :project_id", array(':project_id' => $project_id));
  299. }
  300. /**
  301. * Implements hook_update().
  302. *
  303. * @param $node
  304. * The node which is to have its containing information updated when the user modifies information
  305. * pertaining to the specific project
  306. *
  307. *
  308. * @ingroup tripal_project
  309. */
  310. function chado_project_update($node) {
  311. $node->title = trim($node->title);
  312. $node->description = trim($node->description);
  313. // update the project and the description
  314. $project_id = chado_get_id_from_nid('project', $node->nid) ;
  315. $match = array('project_id' => $project_id);
  316. $values = array(
  317. 'name' => $node->title,
  318. 'description' => '',
  319. );
  320. $status = chado_update_record('project', $match, $values);
  321. if (!$status) {
  322. drupal_set_message(t('Unable to update project.', 'warning'));
  323. watchdog('tripal_project', 'Update project: Unable to update project where values: %values',
  324. array('%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
  325. }
  326. // * Properties Form *
  327. // Add the description property
  328. $properties = chado_retrieve_node_form_properties($node);
  329. $descrip_id = tripal_cv_get_cvterm_by_name('Project Description', NULL, 'project_property');
  330. $properties[$descrip_id->cvterm_id][0] = $node->description;
  331. $details = array(
  332. 'property_table' => 'projectprop',
  333. 'base_table' => 'project',
  334. 'foreignkey_name' => 'project_id',
  335. 'foreignkey_value' => $project_id
  336. );
  337. chado_update_node_form_properties($node, $details, $properties);
  338. // * Relationships Form *
  339. $details = array(
  340. 'relationship_table' => 'project_relationship', // name of the _relationship table
  341. 'foreignkey_value' => $project_id // value of the example_id key
  342. );
  343. chado_update_node_form_relationships($node, $details);
  344. }
  345. /**
  346. * Implementation of node_load().
  347. *
  348. * @param $node
  349. * The node that is to have its containing information loaded
  350. *
  351. * @return $node
  352. * The node, containing the loaded project with the current nid
  353. *
  354. *
  355. * @ingroup tripal_project
  356. */
  357. function chado_project_load($nodes) {
  358. foreach ($nodes as $nid => $node) {
  359. // get the feature details from chado
  360. $project_id = chado_get_id_from_nid('project', $node->nid);
  361. $values = array('project_id' => $project_id);
  362. $project = chado_generate_var('project', $values);
  363. $nodes[$nid]->project = $project;
  364. }
  365. }
  366. /**
  367. * Implement hook_access().
  368. *
  369. * This hook allows node modules to limit access to the node types they define.
  370. *
  371. * @param $node
  372. * The node on which the operation is to be performed, or, if it does not yet exist, the
  373. * type of node to be created
  374. *
  375. * @param $op
  376. * The operation to be performed
  377. *
  378. *
  379. * @param $account
  380. * A user object representing the user for whom the operation is to be performed
  381. *
  382. * @return
  383. * If the permission for the specified operation is not set then return FALSE. If the
  384. * permission is set then return NULL as this allows other modules to disable
  385. * access. The only exception is when the $op == 'create'. We will always
  386. * return TRUE if the permission is set.
  387. *
  388. * @ingroup tripal_project
  389. */
  390. function chado_project_node_access($node, $op, $account) {
  391. if ($op == 'create') {
  392. if (!user_access('create chado_projects content', $account)) {
  393. return FALSE;
  394. }
  395. return TRUE;
  396. }
  397. if ($op == 'update') {
  398. if (!user_access('edit chado_projects content', $account)) {
  399. return FALSE;
  400. }
  401. }
  402. if ($op == 'delete') {
  403. if (!user_access('delete chado_projects content', $account)) {
  404. return FALSE;
  405. }
  406. }
  407. if ($op == 'view') {
  408. if (!user_access('access chado_projects content', $account)) {
  409. return FALSE;
  410. }
  411. }
  412. return NULL;
  413. }
  414. /**
  415. *
  416. * @ingroup tripal_feature
  417. */
  418. function tripal_project_node_view($node, $view_mode, $langcode) {
  419. switch ($node->type) {
  420. case 'chado_project':
  421. // Show feature browser and counts
  422. if ($view_mode == 'full') {
  423. $node->content['tripal_project_base'] = array(
  424. '#markup' => theme('tripal_project_base', array('node' => $node)),
  425. '#tripal_toc_id' => 'base',
  426. '#tripal_toc_title' => 'Overview',
  427. '#weight' => -100,
  428. );
  429. $node->content['tripal_project_contact'] = array(
  430. '#markup' => theme('tripal_project_contact', array('node' => $node)),
  431. '#tripal_toc_id' => 'contacts',
  432. '#tripal_toc_title' => 'Contacts',
  433. );
  434. $node->content['tripal_project_properties'] = array(
  435. '#markup' => theme('tripal_project_properties', array('node' => $node)),
  436. '#tripal_toc_id' => 'properties',
  437. '#tripal_toc_title' => 'Properties',
  438. );
  439. $node->content['tripal_project_publications'] = array(
  440. '#markup' => theme('tripal_project_publications', array('node' => $node)),
  441. '#tripal_toc_id' => 'publications',
  442. '#tripal_toc_title' => 'Publications',
  443. );
  444. $node->content['tripal_project_relationships'] = array(
  445. '#markup' => theme('tripal_project_relationships', array('node' => $node)),
  446. '#tripal_toc_id' => 'relationships',
  447. '#tripal_toc_title' => 'Relationships',
  448. );
  449. }
  450. if ($view_mode == 'teaser') {
  451. $node->content['tripal_project_teaser'] = array(
  452. '#markup' => theme('tripal_project_teaser', array('node' => $node)),
  453. );
  454. }
  455. break;
  456. }
  457. }