tripal_project.chado_node.inc 17 KB

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