tripal_project.chado_node.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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_legacy_project
  14. */
  15. function tripal_project_node_info() {
  16. return array(
  17. 'chado_project' => array(
  18. 'name' => t('Project (Tripal v2 legacy)'),
  19. 'base' => 'chado_project',
  20. 'description' => t('A project from the Chado database'),
  21. 'has_title' => TRUE,
  22. 'locked' => TRUE,
  23. 'chado_node_api' => array(
  24. 'base_table' => 'project',
  25. 'hook_prefix' => 'chado_project',
  26. 'record_type_title' => array(
  27. 'singular' => t('Project'),
  28. 'plural' => t('Projects')
  29. ),
  30. 'sync_filters' => array(
  31. 'type_id' => FALSE,
  32. 'organism_id' => FALSE
  33. ),
  34. ),
  35. ),
  36. );
  37. }
  38. /**
  39. * Implementation of hook_form().
  40. *
  41. * This form takes the Project Title information and description from the user.
  42. *
  43. * @parm $node
  44. * The initialized node
  45. *
  46. * @parm $form_state
  47. * The state of the form, that has the user entered information that is neccessary for adding
  48. * information to the project
  49. *
  50. * @return $form
  51. * An array as described by the Drupal Form API
  52. *
  53. *
  54. * @ingroup tripal_legacy_project
  55. */
  56. function chado_project_form(&$node, $form_state) {
  57. $form = array();
  58. // Default values can come in the following ways:
  59. //
  60. // 1) as elements of the $node object. This occurs when editing an existing project
  61. // 2) in the $form_state['values'] array which occurs on a failed validation or
  62. // ajax callbacks from non submit form elements
  63. // 3) in the $form_state['input'[ array which occurs on ajax callbacks from submit
  64. // form elements and the form is being rebuilt
  65. //
  66. // set form field defaults
  67. $project_id = null;
  68. $title = '';
  69. $description = '';
  70. // if we are editing an existing node then the project is already part of the node
  71. if (property_exists($node, 'project')) {
  72. $project = $node->project;
  73. // get the project default values. When this module was first created
  74. // the project description was incorrectly stored in the $node->body field.
  75. // It is better to store it in the Chado tables. However, the 'description'
  76. // field of the project table is only 255 characters. So, we are going
  77. // to follow the same as the project module and store the description in
  78. // the projectprop table and leave the project.description field blank.
  79. // however, for backwards compatibitily, we check to see if the description
  80. // is in the $node->body field. If it is we'll use that. When the node is
  81. // edited the text will be moved out of the body and into the projectprop
  82. // table where it should belong.
  83. if (property_exists($node, 'body')) {
  84. $description = $node->body;
  85. }
  86. else {
  87. $description = $project->description;
  88. }
  89. if (!$description) {
  90. $projectprop = chado_get_property(
  91. array('table' => 'project', 'id' => $project->project_id),
  92. array('type_name' => 'Project Description', 'cv_name' =>'project_property')
  93. );
  94. $description = $projectprop->value;
  95. }
  96. $title = $project->name;
  97. $project_id = $project->project_id;
  98. // keep track of the project id if we have. If we do have one then
  99. // this is an update as opposed to an insert.
  100. $form['project_id'] = array(
  101. '#type' => 'value',
  102. '#value' => $project_id,
  103. );
  104. }
  105. // if we are re constructing the form from a failed validation or ajax callback
  106. // then use the $form_state['values'] values
  107. if (array_key_exists('values', $form_state)) {
  108. $title = $form_state['values']['title'];
  109. $description = $form_state['values']['description'];
  110. }
  111. // if we are re building the form from after submission (from ajax call) then
  112. // the values are in the $form_state['input'] array
  113. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  114. $title = $form_state['input']['title'];
  115. $description = $form_state['input']['description'];
  116. }
  117. $form['title']= array(
  118. '#type' => 'textfield',
  119. '#title' => t('Project Title'),
  120. '#description' => t('Please enter the title for this project. This appears at the top of the project page.'),
  121. '#required' => TRUE,
  122. '#default_value' => $node->title,
  123. );
  124. $form['description']= array(
  125. '#type' => 'text_format',
  126. '#title' => t('Project Description'),
  127. '#description' => t('A brief description of the project'),
  128. '#required' => TRUE,
  129. '#default_value' => $description,
  130. );
  131. // Properties Form
  132. // ----------------------------------
  133. $select_options = array();
  134. $prop_cv = tripal_get_default_cv('projectprop', 'type_id');
  135. $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
  136. if ($prop_cv = 'project_property') {
  137. // if this is the project_property CV then
  138. // we want to exclude the project description from being loaded as a stored property
  139. // because we want to use the property to replace the project.description field as it is
  140. // only 255 characters which isn't large enough. We don't want the user to set it
  141. // as a property even though it will be stored as a property.
  142. $cv_result = chado_select_record('cv',array('cv_id'),array('name' => 'project_property'));
  143. $cv_id = $cv_result[0]->cv_id;
  144. $select_options = tripal_get_cvterm_select_options($cv_id);
  145. $descrip_id = array_search('Project Description', $select_options);
  146. unset($select_options[$descrip_id]);
  147. }
  148. $instructions = t('To add properties to the drop down list, you must ' . l("add terms to the project_property vocabulary", "admin/tripal/vocab/cvterm/add") . ".");
  149. $details = array(
  150. 'property_table' => 'projectprop',
  151. 'chado_id' => $project_id,
  152. 'cv_id' => $cv_id,
  153. 'additional_instructions' => $instructions,
  154. 'select_options' => $select_options
  155. );
  156. chado_add_node_form_properties($form, $form_state, $details);
  157. // RELATIONSHIPS FORM
  158. //---------------------------------------------
  159. $relationship_cv = tripal_get_default_cv('project_relationship', 'type_id');
  160. $cv_id = $relationship_cv ? $relationship_cv->cv_id : NULL;
  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_id' => $cv_id, // the cv.cv_id 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. );
  172. // Adds the form elements to your current form
  173. chado_add_node_form_relationships($form, $form_state, $details);
  174. return $form;
  175. }
  176. /**
  177. * Implements hook_validate().
  178. * Validates submission of form when adding or updating a project node
  179. *
  180. * @ingroup tripal_legacy_project
  181. */
  182. function chado_project_validate($node, $form, &$form_state) {
  183. // We only want to validate when the node is saved.
  184. // Since this validate can be called on AJAX and Deletion of the node
  185. // we need to make this check to ensure queries are not executed
  186. // without the proper values.
  187. if(property_exists($node, "op") and $node->op != 'Save') {
  188. return;
  189. }
  190. // we are syncing if we do not have a node ID but we do have a project_id. We don't
  191. // need to validate during syncing so just skip it.
  192. if (!property_exists($node, 'nid') and property_exists($node, 'project_id') and $node->project_id != 0) {
  193. return;
  194. }
  195. // trim white space from text fields
  196. $node->title = property_exists($node, 'title') ? trim($node->title) : '';
  197. $project = 0;
  198. // check to make sure the name on the project is unique
  199. // before we try to insert into chado.
  200. if (property_exists($node, 'project_id')) {
  201. $sql = "SELECT * FROM {project} WHERE name = :name AND NOT project_id = :project_id";
  202. $project = chado_query($sql, array(':name' => $node->title, ':project_id' => $node->project_id))->fetchObject();
  203. }
  204. else {
  205. $sql = "SELECT * FROM {project} WHERE name = :name";
  206. $project = chado_query($sql, array(':name' => $node->title))->fetchObject();
  207. }
  208. if ($project) {
  209. form_set_error('title', t('The unique project name already exists. Please choose another'));
  210. }
  211. }
  212. /**
  213. * Implementation of hook_insert().
  214. *
  215. * @parm $node
  216. * Then node that has the information stored within, accessed given the nid
  217. *
  218. * @ingroup tripal_legacy_project
  219. */
  220. function chado_project_insert($node) {
  221. $project_id = '';
  222. // if there is an project_id in the $node object then this must be a sync so
  223. // we can skip adding the project as it is already there, although
  224. // we do need to proceed with insertion into the chado/drupal linking table.
  225. if (!property_exists($node, 'project_id')) {
  226. $node->title = trim($node->title);
  227. $node->description = trim($node->description['value']);
  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_get_cvterm(array(
  244. 'name' => 'Project Description',
  245. 'cv_id' => array('name' => 'project_property')
  246. ));
  247. $properties[$descrip_id->cvterm_id][0] = $node->description;
  248. $details = array(
  249. 'property_table' => 'projectprop',
  250. 'base_table' => 'project',
  251. 'foreignkey_name' => 'project_id',
  252. 'foreignkey_value' => $project_id
  253. );
  254. chado_update_node_form_properties($node, $details, $properties);
  255. // * Relationships Form *
  256. $details = array(
  257. 'relationship_table' => 'project_relationship', // name of the _relationship table
  258. 'foreignkey_value' => $project_id // value of the example_id key
  259. );
  260. chado_update_node_form_relationships($node, $details);
  261. }
  262. else {
  263. $project_id = $node->project_id;
  264. }
  265. // Make sure the entry for this project doesn't already exist in the
  266. // chado_project table if it doesn't exist then we want to add it.
  267. $check_org_id = chado_get_id_from_nid('project', $node->nid);
  268. if (!$check_org_id) {
  269. $record = new stdClass();
  270. $record->nid = $node->nid;
  271. $record->vid = $node->vid;
  272. $record->project_id = $project_id;
  273. drupal_write_record('chado_project', $record);
  274. }
  275. }
  276. /**
  277. * Implementation of hook_delete().
  278. *
  279. * @param $node
  280. * The node which is to be deleted, only chado project and chado_project need to be dealt with
  281. * since the drupal node is deleted automagically
  282. *
  283. * @ingroup tripal_legacy_project
  284. */
  285. function chado_project_delete($node) {
  286. $project_id = chado_get_id_from_nid('project', $node->nid);
  287. // if we don't have a project id for this node then this isn't a node of
  288. // type chado_project or the entry in the chado_project table was lost.
  289. if (!$project_id) {
  290. return;
  291. }
  292. // Remove data from {chado_project}, {node} and {node_revisions} tables of
  293. // drupal database
  294. $sql_del = "DELETE FROM {chado_project} WHERE nid = :nid AND vid = :vid";
  295. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  296. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  297. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  298. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  299. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  300. // Remove data from project and projectprop tables of chado database as well
  301. chado_query("DELETE FROM {projectprop} WHERE project_id = :project_id", array(':project_id' => $project_id));
  302. chado_query("DELETE FROM {project} WHERE project_id = :project_id", array(':project_id' => $project_id));
  303. }
  304. /**
  305. * Implements hook_update().
  306. *
  307. * @param $node
  308. * The node which is to have its containing information updated when the user modifies information
  309. * pertaining to the specific project
  310. *
  311. * @ingroup tripal_legacy_project
  312. */
  313. function chado_project_update($node) {
  314. $node->title = trim($node->title);
  315. $node->description = trim($node->description['value']);
  316. // update the project and the description
  317. $project_id = chado_get_id_from_nid('project', $node->nid) ;
  318. $match = array('project_id' => $project_id);
  319. $values = array(
  320. 'name' => $node->title,
  321. 'description' => '',
  322. );
  323. $status = chado_update_record('project', $match, $values);
  324. if (!$status) {
  325. drupal_set_message(t('Unable to update project.', 'warning'));
  326. watchdog('tripal_project', 'Update project: Unable to update project where values: %values',
  327. array('%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
  328. }
  329. // * Properties Form *
  330. // Add the description property
  331. $properties = chado_retrieve_node_form_properties($node);
  332. $descrip_id = tripal_get_cvterm(array(
  333. 'name' => 'Project Description',
  334. 'cv_id' => array('name' => 'project_property')
  335. ));
  336. $properties[$descrip_id->cvterm_id][0] = $node->description;
  337. $details = array(
  338. 'property_table' => 'projectprop',
  339. 'base_table' => 'project',
  340. 'foreignkey_name' => 'project_id',
  341. 'foreignkey_value' => $project_id
  342. );
  343. chado_update_node_form_properties($node, $details, $properties);
  344. // * Relationships Form *
  345. $details = array(
  346. 'relationship_table' => 'project_relationship', // name of the _relationship table
  347. 'foreignkey_value' => $project_id // value of the example_id key
  348. );
  349. chado_update_node_form_relationships($node, $details);
  350. }
  351. /**
  352. * Implementation of hook_load().
  353. *
  354. * @param $node
  355. * The node that is to have its containing information loaded
  356. *
  357. * @ingroup tripal_legacy_project
  358. */
  359. function chado_project_load($nodes) {
  360. foreach ($nodes as $nid => $node) {
  361. // get the feature details from chado
  362. $project_id = chado_get_id_from_nid('project', $node->nid);
  363. // if the nid does not have a matching record then skip this node.
  364. // this can happen with orphaned nodes.
  365. if (!$project_id) {
  366. continue;
  367. }
  368. $values = array('project_id' => $project_id);
  369. $project = chado_generate_var('project', $values);
  370. $nodes[$nid]->project = $project;
  371. // Now get the title
  372. $node->title = chado_get_node_title($node);
  373. }
  374. }
  375. /**
  376. * Implement hook_node_access().
  377. *
  378. * This hook allows node modules to limit access to the node types they define.
  379. *
  380. * @param $node
  381. * The node on which the operation is to be performed, or, if it does not yet exist, the
  382. * type of node to be created
  383. *
  384. * @param $op
  385. * The operation to be performed
  386. *
  387. *
  388. * @param $account
  389. * A user object representing the user for whom the operation is to be performed
  390. *
  391. * @return
  392. * If the permission for the specified operation is not set then return FALSE. If the
  393. * permission is set then return NULL as this allows other modules to disable
  394. * access. The only exception is when the $op == 'create'. We will always
  395. * return TRUE if the permission is set.
  396. *
  397. * @ingroup tripal_legacy_project
  398. */
  399. function tripal_project_node_access($node, $op, $account) {
  400. $node_type = $node;
  401. if (is_object($node)) {
  402. $node_type = $node->type;
  403. }
  404. if($node_type == 'chado_project') {
  405. if ($op == 'create') {
  406. if (!user_access('create chado_project content', $account)) {
  407. return NODE_ACCESS_DENY;
  408. }
  409. return NODE_ACCESS_ALLOW;
  410. }
  411. if ($op == 'update') {
  412. if (!user_access('edit chado_project content', $account)) {
  413. return NODE_ACCESS_DENY;
  414. }
  415. }
  416. if ($op == 'delete') {
  417. if (!user_access('delete chado_project content', $account)) {
  418. return NODE_ACCESS_DENY;
  419. }
  420. }
  421. if ($op == 'view') {
  422. if (!user_access('access chado_project content', $account)) {
  423. return NODE_ACCESS_DENY;
  424. }
  425. }
  426. return NODE_ACCESS_IGNORE;
  427. }
  428. }
  429. /**
  430. * Implements hook_node_view().
  431. *
  432. * @ingroup tripal_legacy_project
  433. */
  434. function tripal_project_node_view($node, $view_mode, $langcode) {
  435. switch ($node->type) {
  436. case 'chado_project':
  437. // Show feature browser and counts
  438. if ($view_mode == 'full') {
  439. $node->content['tripal_project_base'] = array(
  440. '#theme' => 'tripal_project_base',
  441. '#node' => $node,
  442. '#tripal_toc_id' => 'base',
  443. '#tripal_toc_title' => 'Overview',
  444. '#weight' => -100,
  445. );
  446. $node->content['tripal_project_contact'] = array(
  447. '#theme' => 'tripal_project_contact',
  448. '#node' => $node,
  449. '#tripal_toc_id' => 'contacts',
  450. '#tripal_toc_title' => 'Contacts',
  451. );
  452. $node->content['tripal_project_properties'] = array(
  453. '#theme' => 'tripal_project_properties',
  454. '#node' => $node,
  455. '#tripal_toc_id' => 'properties',
  456. '#tripal_toc_title' => 'Properties',
  457. );
  458. $node->content['tripal_project_publications'] = array(
  459. '#theme' => 'tripal_project_publications',
  460. '#node' => $node,
  461. '#tripal_toc_id' => 'publications',
  462. '#tripal_toc_title' => 'Publications',
  463. );
  464. $node->content['tripal_project_relationships'] = array(
  465. '#theme' => 'tripal_project_relationships',
  466. '#node' => $node,
  467. '#tripal_toc_id' => 'relationships',
  468. '#tripal_toc_title' => 'Relationships',
  469. );
  470. }
  471. if ($view_mode == 'teaser') {
  472. $node->content['tripal_project_teaser'] = array(
  473. '#theme' => 'tripal_project_teaser',
  474. '#node' => $node,
  475. );
  476. }
  477. break;
  478. }
  479. }
  480. /**
  481. * Implements hook_node_insert().
  482. * Acts on all content types.
  483. *
  484. * @ingroup tripal_legacy_project
  485. */
  486. function tripal_project_node_insert($node) {
  487. // set the URL path after inserting. We do it here because we do not
  488. // know the project_id in the presave
  489. switch ($node->type) {
  490. case 'chado_project':
  491. // We still don't have a fully loaded node object in this hook. Therefore,
  492. // we need to simulate one so that the right values are available for
  493. // the URL to be determined.
  494. $project_id = chado_get_id_from_nid('project', $node->nid);
  495. $node->project = chado_generate_var('project', array('project_id' => $project_id));
  496. // Now get the title.
  497. $node->title = chado_get_node_title($node);
  498. // Now use the API to set the path.
  499. chado_set_node_url($node);
  500. break;
  501. }
  502. }
  503. /**
  504. * Implements hook_node_update().
  505. * Acts on all content types.
  506. *
  507. * @ingroup tripal_legacy_project
  508. */
  509. function tripal_project_node_update($node) {
  510. // add items to other nodes, build index and search results
  511. switch ($node->type) {
  512. case 'chado_project':
  513. // Now get the title
  514. $node->title = chado_get_node_title($node);
  515. // Now use the API to set the path.
  516. chado_set_node_url($node);
  517. break;
  518. }
  519. }
  520. /**
  521. * Implements [content_type]_chado_node_default_title_format().
  522. *
  523. * Defines a default title format for the Chado Node API to set the titles on
  524. * Chado project nodes based on chado fields.
  525. */
  526. function chado_project_chado_node_default_title_format() {
  527. return '[project.name]';
  528. }
  529. /**
  530. * Implements hook_chado_node_default_url_format().
  531. *
  532. * Designates a default URL format for project nodes.
  533. */
  534. function chado_project_chado_node_default_url_format() {
  535. return '/project/[project.project_id]';
  536. }