PageRenderTime 54ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/system/application/controllers/cloud.php

https://bitbucket.org/zhemel/cloudengine
PHP | 954 lines | 590 code | 123 blank | 241 comment | 78 complexity | dc7d1caedb7b277aaaf2728c70da66cb MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * Controller for cloud-related functionality
  4. *
  5. * @copyright 2009, 2010 The Open University. See CREDITS.txt
  6. * @license http://gnu.org/licenses/gpl-2.0.html GNU GPL v2
  7. * @package Cloud
  8. */
  9. class Cloud extends MY_Controller {
  10. function Cloud() {
  11. parent::MY_Controller();
  12. $this->load->helper('url');
  13. $this->load->helper('form');
  14. $this->load->helper('format');
  15. $this->load->model('cloud_model');
  16. $this->load->model('tag_model');
  17. $this->load->library('layout', 'layout_main');
  18. $this->load->model('user_model');
  19. }
  20. /**
  21. * Display a paged list of all the clouds on the site starting with
  22. * a specified letter of the alphabet
  23. *
  24. * @param string $alpha The letter of the alphabet
  25. */
  26. function cloud_list($alpha = 'A') {
  27. $data['title'] = 'Clouds';
  28. $data['navigation'] = 'clouds';
  29. $data['alpha'] = $alpha;
  30. $data['clouds'] = $this->cloud_model->get_clouds_alpha($alpha);
  31. $data['new_clouds'] = $this->cloud_model->get_new_clouds(15);
  32. $this->layout->view('cloud/list', $data);
  33. }
  34. /**
  35. * View a specific cloud
  36. *
  37. * @param mixed $cloud Either the ID of the cloud or the title of the cloud
  38. * (URL-encoded)
  39. * @param string $view The type of items to display in the tabbed section
  40. * i.e. 'comments', 'links' or 'references'.
  41. */
  42. function view($cloud = 0, $view = 'comments') {
  43. // The URL may specify either the cloud ID or cloud name, find out which
  44. $user_id = $this->db_session->userdata('id');
  45. $this->load->model('comment_model');
  46. $this->load->model('content_model');
  47. $this->load->model('link_model');
  48. $this->load->model('embed_model');
  49. $this->load->model('favourite_model');
  50. // Get the cloud
  51. if (is_numeric($cloud)) {
  52. $data['cloud'] = $this->cloud_model->get_cloud($cloud);
  53. $cloud_id = $cloud;
  54. } else {
  55. $data['cloud'] = $this->cloud_model->get_cloud_by_title(urldecode($cloud));
  56. $cloud_id = $data['cloud']->cloud_id;
  57. }
  58. $cloud = $data['cloud'];
  59. // Set up form validation for the comment form
  60. $this->load->library('form_validation');
  61. ///Translators: Form field names.
  62. $this->form_validation->set_rules('body', t("Comment"), 'required');
  63. // See if the comment form has been submitted
  64. if ($this->input->post('submit')) {
  65. $this->auth_lib->check_logged_in();
  66. $cloud_id = $this->input->post('cloud_id');
  67. if (!is_numeric($cloud_id)) {
  68. show_error(t("An error occurred."));
  69. }
  70. if ($this->form_validation->run()) {
  71. // Get the form data
  72. $body = $this->input->post('body');
  73. // Moderate for spam
  74. $moderate = $this->_moderate_comment($body, $user_id);
  75. // Add the comment
  76. $comment_id = $this->comment_model->insert_comment($cloud_id, $user_id, $body, $moderate);
  77. // If moderated, tell the user, otherwise send notifications and redirect
  78. if (config_item('x_moderation') && $moderate) {
  79. $data['item'] = 'comment';
  80. $data['continuelink'] = '/cloud/view/'.$cloud_id;
  81. $this->layout->view('moderate', $data);
  82. return;
  83. } else {
  84. redirect('/cloud/view/'.$cloud_id); // Return to the main cloud view page
  85. }
  86. }
  87. }
  88. // If the cloud exists, get all the other information needed for the page
  89. if ($data['cloud']->cloud_id) {
  90. // For each comment figure out if the current user has edit permission for that
  91. // comment and add the information to the comment
  92. $comments = $this->comment_model->get_comments($cloud_id);
  93. if ($comments) {
  94. foreach ($comments as $comment) {
  95. $comment->edit_permission =
  96. $this->comment_model->has_edit_permission($user_id,
  97. $comment->comment_id);
  98. $modified_comments[] = $comment;
  99. }
  100. }
  101. // Do the same for content
  102. $contents = $this->content_model->get_content($cloud_id);
  103. if ($contents) {
  104. foreach ($contents as $content) {
  105. $content->edit_permission =
  106. $this->content_model->has_edit_permission($user_id,
  107. $content->content_id);
  108. $modified_contents[] = $content;
  109. }
  110. }
  111. // Do the same for links
  112. $links = $this->link_model->get_links($cloud_id);
  113. if ($links) {
  114. foreach ($links as $link) {
  115. $link->edit_permission =
  116. $this->link_model->has_edit_permission($user_id, $link->link_id);
  117. $link->show_favourite_link =
  118. $this->favourite_model->can_favourite_item($user_id,
  119. $link->link_id, 'link');
  120. $modified_links[] = $link;
  121. }
  122. }
  123. // Do the same for links
  124. $embeds = $this->embed_model->get_embeds($cloud_id);
  125. if ($embeds) {
  126. foreach ($embeds as $embed) {
  127. $embed->edit_permission = $this->embed_model->has_edit_permission($user_id, $embed->embed_id);
  128. $modified_embeds[] = $embed;
  129. }
  130. }
  131. // Get the data for gadgets for this cloud
  132. if ($this->config->item('x_gadgets')) {
  133. $this->load->model('gadget_model');
  134. // Get the gadgets added to this cloud, and the gadgets added to all the cloud
  135. // owner's clouds - we can't combine the lists as the links for deleting the
  136. // gadgets are slightly different for each
  137. $data['gadgets_cloud'] = $this->gadget_model->get_gadgets_for_cloud($cloud_id);
  138. $data['gadgets_user'] = $this->gadget_model->get_gadgets_for_user(
  139. $cloud->user_id);
  140. $data['current_user_id'] = $user_id;
  141. }
  142. // We need to log the view before we calculate the number of views
  143. $this->cloud_model->log_view($cloud_id);
  144. $data['total_views'] = $this->cloud_model->get_total_views($cloud_id);
  145. $data['comments'] = $modified_comments;
  146. $data['total_comments'] = $this->comment_model->get_total_comments($cloud_id);
  147. $data['tags'] = $this->tag_model->get_tags('cloud', $cloud_id);
  148. $data['links'] = $modified_links;
  149. $data['references'] = $this->cloud_model->get_references($cloud_id);
  150. $data['contents'] = $modified_contents;
  151. $data['embeds'] = $modified_embeds;
  152. $data['cloudscapes'] = $this->cloud_model->get_cloudscapes($cloud_id);
  153. $data['total_favourites'] = $this->favourite_model->get_total_favourites($cloud_id, 'cloud');
  154. $data['favourite'] = $this->favourite_model->is_favourite($user_id,
  155. $cloud_id, 'cloud');
  156. $data['show_favourite_link'] = $this->favourite_model->can_favourite_item(
  157. $user_id, $cloud_id, 'cloud');
  158. $data['title'] = $data['cloud']->title;
  159. $data['navigation'] = 'clouds';
  160. $data['edit_permission'] = $this->cloud_model->has_edit_permission($user_id,
  161. $cloud_id);
  162. $data['admin'] = $this->auth_lib->is_admin();
  163. $data['following'] = $this->cloud_model->is_following($cloud_id, $user_id);
  164. $data['view'] = $view;
  165. $this->layout->view('cloud/view', $data);
  166. } else {
  167. // If invalid cloud id, display error page
  168. show_error(t("An error occurred."));
  169. }
  170. }
  171. /**
  172. * Hide a cloud from the main site cloudstream and from the list of new clouds
  173. *
  174. * @param integer $cloud_id The ID of the cloud
  175. */
  176. function hide($cloud_id = 0) {
  177. $this->auth_lib->check_is_admin();
  178. $this->cloud_model->hide_cloud($cloud_id);
  179. redirect('/cloud/view/'.$cloud_id);
  180. }
  181. /**
  182. * Remove a cloud from the events diary
  183. */
  184. function remove_diary($cloud_id = 0) {
  185. $this->auth_lib->check_is_admin();
  186. $this->cloud_model->remove_diary($cloud_id);
  187. redirect('/cloud/view/'.$cloud_id);
  188. }
  189. /**
  190. * Add a cloud to the events diary
  191. */
  192. function add_diary($cloud_id = 0) {
  193. $this->auth_lib->check_is_admin();
  194. $this->cloud_model->add_diary($cloud_id);
  195. redirect('/cloud/view/'.$cloud_id);
  196. }
  197. /**
  198. * Add a new cloud, either standalone or also add it to a cloudscape if specified
  199. *
  200. * @param integer $cloudscape_id The ID of the cloudscape to add the new cloud to,
  201. * or FALSE if it should not be added to a cloudscape
  202. */
  203. function add($cloudscape_id = 0) {
  204. $this->auth_lib->check_logged_in();
  205. $user_id = $this->db_session->userdata('id');
  206. $this->load->model('cloudscape_model');
  207. $this->load->model('link_model');
  208. // Set up form validation
  209. $this->load->library('form_validation');
  210. $this->form_validation->set_rules('title', t("Title"), 'required');
  211. // If add cloud form has been submitted then process it
  212. if ($this->input->post('submit')) {
  213. // Get the information from the form and add the cloud
  214. $cloud->title = $this->input->post('title');
  215. $cloud->body = $this->input->post('body');
  216. $cloud->primary_url = $this->input->post('url');
  217. $cloud->user_id = $user_id;
  218. if ($this->form_validation->run()) {
  219. // Moderate for spam
  220. $cloud->moderate = $this->_moderate_cloud($cloud, $user_id);
  221. // Add the cloud
  222. $cloud_id = $this->cloud_model->insert_cloud($cloud);
  223. if (config_item('x_moderation') && $cloud->moderate) {
  224. $data['title']= t('Your !item is being moderated', array('!item'=>'cloud'));
  225. $data['item'] = 'cloud';
  226. $data['continuelink'] = site_url('cloud/cloud_list');
  227. $this->layout->view('moderate', $data);
  228. return;
  229. }
  230. if ($cloud_id) {
  231. // If a cloudscape has been specified also add the cloud to that
  232. // cloudscape
  233. $cloudscape_id = $this->input->post('cloudscape_id');
  234. if ($cloudscape_id) {
  235. $this->cloudscape_model->check_post_permission($cloudscape_id,
  236. $user_id);
  237. $this->cloudscape_model->add_cloud($cloudscape_id, $cloud_id);
  238. }
  239. // Redirect to the page for the new cloud
  240. $cloud->cloud_id = $cloud_id;
  241. $data['cloud'] = $cloud;
  242. $this->layout->view('cloud/cloud_added', $data);
  243. return;
  244. } else {
  245. show_error(t("An error occurred adding the new cloud."));
  246. }
  247. } else {
  248. $data['cloud'] = $cloud;
  249. }
  250. }
  251. $data['cloudscape_id'] = $cloudscape_id;
  252. // Check to see if a cloudscape id has been specified
  253. if ($data["cloudscape_id"]) {
  254. $data["cloudscape"] = $this->cloudscape_model->get_cloudscape(
  255. $data["cloudscape_id"]);
  256. }
  257. $data['new'] = true;
  258. $data['title'] = t("Create Cloud");
  259. $data['navigation'] = 'clouds';
  260. // Display the form for creating a cloud
  261. $this->layout->view('cloud/edit', $data);
  262. }
  263. /**
  264. * Move a link in the links section for a cloud to the main link for a cloud
  265. *
  266. * @param integer $cloud_id The ID of the cloud
  267. * @param integer $link_id The ID of the link
  268. */
  269. function make_link_primary($cloud_id = 0, $link_id = 0) {
  270. $this->auth_lib->check_is_admin();
  271. $this->load->model('link_model');
  272. $this->link_model->make_link_primary($cloud_id, $link_id);
  273. redirect(base_url().'cloud/view/'.$cloud_id.'/links#contribute');
  274. }
  275. /**
  276. * Edit an existing cloud
  277. *
  278. * @param integer $cloud_id The ID of the cloud
  279. */
  280. function edit($cloud_id = 0) {
  281. $user_id = $this->db_session->userdata('id');
  282. $this->cloud_model->check_edit_permission($user_id, $cloud_id);
  283. // Set up form validation rules (empty rules needed for set_value()
  284. $this->load->library('form_validation');
  285. $this->form_validation->set_rules('title', t("Title"), 'required');
  286. if ($this->input->post('submit')) {
  287. // Get the form data
  288. $cloud->cloud_id = $cloud_id;
  289. $cloud->title = $this->input->post('title');
  290. $cloud->body = $this->input->post('body');
  291. $cloud->summary = $this->input->post('summary');
  292. $cloud->primary_url = $this->input->post('url');
  293. $event_date_str = trim($this->input->post('event_date'));
  294. if ($event_date_str) {
  295. $cloud->event_date = strtotime($event_date_str);
  296. } else {
  297. $cloud->event_date = null;
  298. }
  299. $call_deadline_str = trim($this->input->post('call_deadline'));
  300. if ($call_deadline_str) {
  301. $cloud->call_deadline = strtotime($call_deadline_str);
  302. } else {
  303. $cloud->call_deadline = null;
  304. }
  305. // Validate the data, if fine, update the cloud and redirect to the cloud page,
  306. // otherwise keep the submitted ata to repopulate the form
  307. if ($this->form_validation->run()) {
  308. // Moderate for spam
  309. $cloud->moderate = $this->_moderate_cloud($cloud, $user_id);
  310. $this->cloud_model->update_cloud($cloud);
  311. if (config_item('x_moderation') && $cloud->moderate) {
  312. $data['title']= t('Your !item is being moderated', array('!item'=>'cloud'));
  313. $data['item'] = 'cloud';
  314. $data['continuelink'] = site_url('cloud/cloud_list');
  315. $this->layout->view('moderate', $data);
  316. return;
  317. }
  318. redirect('/cloud/view/'.$cloud_id);
  319. } else {
  320. $data['cloud'] = $cloud;
  321. }
  322. }
  323. // If no data already set from invalid form submission, get the data for the cloud
  324. if (!isset($data['cloud'])) {
  325. $data['cloud'] = $this->cloud_model->get_cloud($cloud_id);
  326. }
  327. $data['new'] = false;
  328. $data['title'] = t("Edit Cloud");
  329. $data['navigation'] = 'clouds';
  330. // Display the edit form
  331. $this->layout->view('cloud/edit', $data);
  332. }
  333. /**
  334. * Delete a cloud
  335. *
  336. * @param integer $cloud_id The ID of the cloud
  337. */
  338. function delete($cloud_id = 0) {
  339. // Check permissions
  340. $this->auth_lib->check_logged_in();
  341. $user_id = $this->db_session->userdata('id');
  342. $this->cloud_model->check_edit_permission( $user_id, $cloud_id);
  343. // If confirmation form submitted delete the cloudscape, otherwise display
  344. // the confirmation form
  345. if ($this->input->post('submit')) {
  346. $data['cloud'] = $this->cloud_model->get_cloud($cloud_id);
  347. $this->cloud_model->delete_cloud($cloud_id);
  348. $this->layout->view('cloud/delete_success', $data);
  349. } else {
  350. $data['title'] = t("Delete Cloud");
  351. $data['navigation'] = 'clouds';
  352. $data['cloud_id'] = $cloud_id;
  353. $data['cloud'] = $this->cloud_model->get_cloud($cloud_id);
  354. $this->layout->view('cloud/delete_confirm', $data);
  355. }
  356. }
  357. /**
  358. * Add a link to a cloud
  359. *
  360. * @param integer $cloud_id The ID of the cloud
  361. */
  362. function add_link($cloud_id = 0) {
  363. $this->auth_lib->check_logged_in();
  364. $user_id = $this->db_session->userdata('id');
  365. $this->load->library('form_validation');
  366. $this->form_validation->set_rules('title', t("Title"), 'required');
  367. $this->form_validation->set_rules('url', t("URL"),
  368. 'valid_url|callback_does_not_use_url_shortener');
  369. $this->load->model('link_model');
  370. if ($this->input->post('submit')) {
  371. if ($this->form_validation->run()) {
  372. // Get the form data
  373. $url = $this->input->post('url');
  374. $title = $this->input->post('title');
  375. // Moderate for spam
  376. $moderate = $this->_moderate_link($title, $url, $user_id);
  377. $duplicate = $this->link_model->is_duplicate($cloud_id, $url);
  378. if (!$duplicate) {
  379. $this->link_model->add_link($cloud_id, $url, $title, $user_id, $moderate);
  380. // If moderated, tell the user
  381. // Otherwise redirect
  382. if (config_item('x_moderation') && $moderate) {
  383. $data['item'] = 'link';
  384. $data['continuelink'] = '/cloud/view/'.$cloud_id;
  385. $this->layout->view('moderate', $data);
  386. return;
  387. } else {
  388. // Return to the main cloud view page
  389. redirect('/cloud/view/'.$cloud_id.'/links#contribute');
  390. }
  391. }
  392. }
  393. }
  394. // Display the add link form
  395. $data['cloud'] = $this->cloud_model->get_cloud($cloud_id);
  396. $data['duplicate'] = $duplicate;
  397. $data['title'] = t("Add Link");
  398. $data['new'] = true;
  399. $this->layout->view('link/edit', $data);
  400. }
  401. /**
  402. * Determines if a URL uses a URL shortener
  403. *
  404. * @param string $url The URL ot check
  405. * @return boolean TRUE if does not use a URL shortner, FALSE otherwise
  406. */
  407. function does_not_use_url_shortener($url) {
  408. $real_url = true;
  409. $regex = "/.*(bit\.ly|tinyurl\.com|is\.gd|tr\.im|ow\.ly|twurl\.nl).*/";
  410. if (preg_match($regex, $url)) {
  411. $real_url = false;
  412. $this->form_validation->set_message('does_not_use_url_shortener',
  413. t("The URL you have specified uses a URL shortener. Please give the original URL
  414. instead since URLs from URL shorteners may not exist forever."));
  415. }
  416. return $real_url;
  417. }
  418. /**
  419. * Delete a link from a cloud
  420. *
  421. * @param integer $link_id The ID of the link
  422. */
  423. function delete_link($link_id = 0) {
  424. $this->load->model('link_model');
  425. // Check if the user has edit permission for the cloud that this link belongs to
  426. $this->auth_lib->check_logged_in();
  427. $link = $this->link_model->get_link($link_id);
  428. $user_id = $this->db_session->userdata('id');
  429. $this->cloud_model->check_edit_permission($user_id, $link->cloud_id);
  430. // If confirmation form submitted delete the link, otherwise display
  431. // the confirmation form
  432. if ($this->input->post('submit')) {
  433. $this->load->model('link_model');
  434. $this->link_model->delete_link($link_id);
  435. redirect('/cloud/view/'.$link->cloud_id.'/links#contribute');
  436. } else {
  437. $data['title'] = t("Delete Link");
  438. $data['link'] = $link;
  439. $data['cloud'] = $this->cloud_model->get_cloud($link->cloud_id);
  440. $this->layout->view('link/delete_confirm', $data);
  441. }
  442. }
  443. /**
  444. * Edit a link for a cloud
  445. *
  446. * @param integer $link_id The ID of the link
  447. */
  448. function edit_link($link_id = 0) {
  449. $this->load->model('link_model');
  450. // Check if the user has edit permission for the cloud that this link belongs to
  451. $this->auth_lib->check_logged_in();
  452. $link = $this->link_model->get_link($link_id);
  453. if ($this->input->post('submit')) {
  454. $title = $this->input->post('title');
  455. $url = $this->input->post('url');
  456. $this->link_model->update_link($link_id, $url, $title);
  457. redirect('/cloud/view/'.$link->cloud_id.'/links#contribute');
  458. } else {
  459. $user_id = $this->db_session->userdata('id');
  460. $data['cloud'] = $this->cloud_model->get_cloud($link->cloud_id);
  461. $data['title'] = t("Edit Link");
  462. $data['link'] = $link;
  463. $this->layout->view('link/edit', $data);
  464. }
  465. }
  466. /**
  467. * Add a reference to a cloud
  468. *
  469. * @param integer $cloud_id The ID of the cloud
  470. */
  471. function add_reference($cloud_id = 0) {
  472. $this->auth_lib->check_logged_in();
  473. $user_id = $this->db_session->userdata('id');
  474. $this->load->library('form_validation');
  475. $this->form_validation->set_rules('reference_text', t("Reference"), 'required');
  476. if ($this->input->post('submit')) {
  477. if ($this->form_validation->run()) {
  478. $reference_text = $this->input->post('reference_text');
  479. // Moderate for spam and then add
  480. $moderate = $this->_moderate_reference($reference_text, $user_id);
  481. $this->cloud_model->add_reference($cloud_id, $reference_text, $user_id,
  482. $moderate);
  483. // If moderated, tell the user, otherwise redirect
  484. if ($moderate) {
  485. $data['item'] = 'link';
  486. $data['continuelink'] = '/cloud/view/'.$cloud_id;
  487. $this->layout->view('moderate', $data);
  488. return;
  489. } else {
  490. // Return to the main cloud view page
  491. redirect('/cloud/view/'.$cloud_id.'/references#contribute');
  492. }
  493. }
  494. }
  495. // Display the add link form
  496. $data['cloud'] = $this->cloud_model->get_cloud($cloud_id);
  497. $data['title'] = t("Add Reference");
  498. $this->layout->view('reference/add', $data);
  499. }
  500. /**
  501. * Delete a reference from a cloud
  502. *
  503. * @param integer $reference_id The ID of the reference
  504. */
  505. function delete_reference($reference_id = 0) {
  506. // Check if the user has edit permission for the cloud that this link belongs to
  507. $this->auth_lib->check_logged_in();
  508. $user_id = $this->db_session->userdata('id');
  509. $this->cloud_model->check_edit_permission($user_id, $reference->cloud_id);
  510. // If confirmation form submitted delete the link, otherwise display
  511. // the confirmation form
  512. if ($this->input->post('submit')) {
  513. $this->cloud_model->delete_reference($reference_id);
  514. redirect('/cloud/view/'.$reference->cloud_id.'/references#contribute');
  515. } else {
  516. $data['title'] = t("Delete Reference");
  517. $data['reference'] = $reference;
  518. $data['cloud'] = $this->cloud_model->get_cloud($reference->cloud_id);
  519. $this->layout->view('reference/delete_confirm', $data);
  520. }
  521. }
  522. /**
  523. * Follow a cloud
  524. *
  525. * @param integer $cloud_id The ID of the cloud
  526. */
  527. function follow($cloud_id = 0) {
  528. // Check logged in
  529. $this->auth_lib->check_logged_in();
  530. // Follow the cloudscape
  531. $user_id = $this->db_session->userdata('id');
  532. $this->cloud_model->follow($cloud_id, $user_id);
  533. // Redirect to the cloud page
  534. redirect('/cloud/view/'.$cloud_id);
  535. }
  536. /**
  537. * Unfollow a cloud
  538. *
  539. * @param integer $cloud_id The ID of the cloud
  540. */
  541. function unfollow($cloud_id = 0) {
  542. // Check logged in
  543. $this->auth_lib->check_logged_in();
  544. $user_id = $this->db_session->userdata('id');
  545. // Unfollow the cloudscape
  546. $this->cloud_model->unfollow($cloud_id, $user_id);
  547. // Redirect to the cloudscape page
  548. redirect('/cloud/view/'.$cloud_id);
  549. }
  550. /**
  551. * Add this cloud as a favourite for this user
  552. *
  553. * @param integer $cloud_id The ID of the cloud
  554. */
  555. function favourite($cloud_id = 0) {
  556. $this->load->model('favourite_model');
  557. $this->auth_lib->check_logged_in();
  558. $user_id = $this->db_session->userdata('id');
  559. $can_favourite = $this->favourite_model->can_favourite($user_id, $cloud_id, 'cloud');
  560. if (!$can_favourite) {
  561. show_error(t(
  562. 'You cannot add this item as a favourite - either you created the item yourself, you have
  563. already favourited it or you do not have high enough reputation on the site to add favourites.'));
  564. }
  565. $this->favourite_model->add_favourite($user_id, $cloud_id, 'cloud');
  566. redirect('/cloud/view/'.$cloud_id);
  567. }
  568. /**
  569. * Remove this cloud as a favourite for this user.
  570. *
  571. * @param integer $cloud_id The ID of the cloud
  572. */
  573. function unfavourite($cloud_id = 0) {
  574. $this->load->model('favourite_model');
  575. $this->auth_lib->check_logged_in();
  576. $user_id = $this->db_session->userdata('id');
  577. $this->favourite_model->remove_favourite($user_id, $cloud_id, 'cloud');
  578. redirect('/cloud/view/'.$cloud_id);
  579. }
  580. /**
  581. * Add a link on a cloud as a favourite for this user
  582. *
  583. * @param integer $cloud_id The ID of the cloud
  584. * @param integer $link_id The ID of the link
  585. */
  586. function link_favourite($cloud_id = 0, $link_id = 0) {
  587. $this->load->model('favourite_model');
  588. $this->auth_lib->check_logged_in();
  589. $user_id = $this->db_session->userdata('id');
  590. $this->favourite_model->add_favourite($user_id, $link_id, 'link');
  591. redirect('/cloud/view/'.$cloud_id.'/links#contribute');
  592. }
  593. /**
  594. * Display the people who have favourited this cloud
  595. *
  596. * @param integer $cloud_id The ID of the cloud
  597. */
  598. function favourited($cloud_id) {
  599. $this->load->model('favourite_model');
  600. $data['cloud'] = $this->cloud_model->get_cloud($cloud_id);
  601. $data['users'] = $this->favourite_model->get_users_favourited($cloud_id, 'cloud');
  602. $this->layout->view('cloud/favourited', $data);
  603. }
  604. /**
  605. * Check if a cloud has a high likelihood of containing spam
  606. *
  607. * @param object $cloud The cloud
  608. * @param integer $user_id The id of the user adding or editting the cloud
  609. * @return boolean TRUE if the cloud is likely to contain spam and should be moderated,
  610. * FALSE otherwise
  611. */
  612. function _moderate_cloud($cloud, $user_id) {
  613. $item_id = isset($cloud->cloud_id) ? $cloud->cloud_id : 0;
  614. $summary = isset($cloud->summary) ? $cloud->summary : NULL;
  615. $item_url= isset($cloud->url) ? $cloud->url : NULL;
  616. return $this->_moderate(__CLASS__, $item_id, $user_id, $cloud->title, "$summary $cloud->body", '', $item_url);
  617. }
  618. /**
  619. * Check if a comment has a high likelihood of containing spam
  620. *
  621. * @param string $body The body of the comment
  622. * @param integer $user_id The id of the user adding or editting the comment
  623. * @return boolean TRUE if the commentis likely to contain spam and should be moderated,
  624. * FALSE otherwise
  625. */
  626. function _moderate_comment($body, $user_id) {
  627. $moderate = FALSE;
  628. if (config_item('x_moderation')) {
  629. $user = $this->user_model->get_user($user_id);
  630. if (!$user->whitelist) {
  631. $this->load->library('mollom');
  632. try {
  633. $spam_status = $this->mollom->checkContent(null, $body);
  634. if ($spam_status['quality'] < 0.5) {
  635. $moderate = TRUE;
  636. }
  637. } catch (Exception $e) {
  638. }
  639. }
  640. }
  641. return $moderate;
  642. }
  643. /**
  644. * Check if a link has a high likelihood of containing spam
  645. *
  646. * @param string $title The link title
  647. * @param string $url The URL of the link
  648. * @param integer $user_id The id of the user adding or editting the link
  649. * @return boolean TRUE if the link is likely to contain spam and should be moderated,
  650. * FALSE otherwise
  651. */
  652. function _moderate_link($title, $url, $user_id) {
  653. $moderate = FALSE;
  654. if (config_item('x_moderation')) {
  655. $user = $this->user_model->get_user($user_id);
  656. if (!$user->whitelist) {
  657. $this->load->library('mollom');
  658. try {
  659. $spam_status = $this->mollom->checkContent($title, false, false, $url);
  660. if ($spam_status['quality'] < 0.5) {
  661. $moderate = TRUE;
  662. }
  663. } catch (Exception $e) {
  664. }
  665. }
  666. }
  667. return $moderate;
  668. }
  669. /**
  670. * Check if a reference has a high likelihood of containing spam
  671. *
  672. * @param string $reference_text The reference text
  673. * @param integer $user_id The id of the user adding or editting the reference
  674. * @return boolean TRUE if the reference is likely to contain spam and should be
  675. * moderated, FALSE otherwise
  676. */
  677. function _moderate_reference($reference_text, $user_id) {
  678. $moderate = FALSE;
  679. if (config_item('x_moderation')) {
  680. $user = $this->user_model->get_user($user_id);
  681. if (!$user->whitelist) {
  682. $this->load->library('mollom');
  683. try {
  684. $spam_status = $this->mollom->checkContent($reference_text);
  685. if ($spam_status['quality'] < 0.5) {
  686. $moderate = TRUE;
  687. }
  688. } catch (Exception $e) {
  689. }
  690. }
  691. }
  692. return $moderate;
  693. }
  694. /**
  695. * View a specific cloud (search layout)
  696. *
  697. * @param mixed $cloud Either the ID of the cloud or the title of the cloud (URL-encoded)
  698. * @param string $view The type of items to display in the tabbed section i.e. 'comments',
  699. * 'links' or 'references'.
  700. */
  701. function search_view($cloud = 0, $view = 'comments') {
  702. // The URL may specify either the cloud ID or cloud name, find out which
  703. $user_id = $this->db_session->userdata('id');
  704. $this->load->model('comment_model');
  705. $this->load->model('content_model');
  706. $this->load->model('link_model');
  707. $this->load->model('embed_model');
  708. $this->load->model('favourite_model');
  709. // Get the cloud
  710. if (is_numeric($cloud)) {
  711. $data['cloud'] = $this->cloud_model->get_cloud($cloud);
  712. $cloud_id = $cloud;
  713. } else {
  714. $data['cloud'] = $this->cloud_model->get_cloud_by_title(urldecode($cloud));
  715. $cloud_id = $data['cloud']->cloud_id;
  716. }
  717. $cloud = $data['cloud'];
  718. // Set up form validation for the comment form
  719. $this->load->library('form_validation');
  720. ///Translators: Form field names.
  721. $this->form_validation->set_rules('body', t("Comment"), 'required');
  722. // See if the comment form has been submitted
  723. if ($this->input->post('submit')) {
  724. $this->auth_lib->check_logged_in();
  725. $cloud_id = $this->input->post('cloud_id');
  726. if (!is_numeric($cloud_id)) {
  727. show_error(t("An error occurred."));
  728. }
  729. if ($this->form_validation->run()) {
  730. // Get the form data
  731. $body = $this->input->post('body');
  732. // Moderate for spam
  733. $moderate = $this->_moderate_comment($body, $user_id);
  734. // Add the comment
  735. $comment_id = $this->comment_model->insert_comment($cloud_id, $user_id, $body, $moderate);
  736. // If moderated, tell the user, otherwise send notifications and redirect
  737. if (config_item('x_moderation') && $moderate) {
  738. $data['item'] = 'comment';
  739. $data['continuelink'] = '/cloud/view/'.$cloud_id;
  740. $this->layout->view('moderate', $data);
  741. return;
  742. } else {
  743. redirect('/cloud/view/'.$cloud_id); // Return to the main cloud view page
  744. }
  745. }
  746. }
  747. // If the cloud exists, get all the other information needed for the page
  748. if ($data['cloud']->cloud_id) {
  749. // For each comment figure out if the current user has edit permission for that
  750. // comment and add the information to the comment
  751. $comments = $this->comment_model->get_comments($cloud_id);
  752. if ($comments) {
  753. foreach ($comments as $comment) {
  754. $comment->edit_permission =
  755. $this->comment_model->has_edit_permission($user_id,
  756. $comment->comment_id);
  757. $modified_comments[] = $comment;
  758. }
  759. }
  760. // Do the same for content
  761. $contents = $this->content_model->get_content($cloud_id);
  762. if ($contents) {
  763. foreach ($contents as $content) {
  764. $content->edit_permission =
  765. $this->content_model->has_edit_permission($user_id,
  766. $content->content_id);
  767. $modified_contents[] = $content;
  768. }
  769. }
  770. // Do the same for links
  771. $links = $this->link_model->get_links($cloud_id);
  772. if ($links) {
  773. foreach ($links as $link) {
  774. $link->edit_permission =
  775. $this->link_model->has_edit_permission($user_id, $link->link_id);
  776. $link->show_favourite_link =
  777. $this->favourite_model->can_favourite_item($user_id,
  778. $link->link_id, 'link');
  779. $modified_links[] = $link;
  780. }
  781. }
  782. // Do the same for links
  783. $embeds = $this->embed_model->get_embeds($cloud_id);
  784. if ($embeds) {
  785. foreach ($embeds as $embed) {
  786. $embed->edit_permission = $this->embed_model->has_edit_permission($user_id, $embed->embed_id);
  787. $modified_embeds[] = $embed;
  788. }
  789. }
  790. // Get the data for gadgets for this cloud
  791. if ($this->config->item('x_gadgets')) {
  792. $this->load->model('gadget_model');
  793. // Get the gadgets added to this cloud, and the gadgets added to all the cloud
  794. // owner's clouds - we can't combine the lists as the links for deleting the
  795. // gadgets are slightly different for each
  796. $data['gadgets_cloud'] = $this->gadget_model->get_gadgets_for_cloud($cloud_id);
  797. $data['gadgets_user'] = $this->gadget_model->get_gadgets_for_user(
  798. $cloud->user_id);
  799. $data['current_user_id'] = $user_id;
  800. }
  801. // We need to log the view before we calculate the number of views
  802. $this->cloud_model->log_view($cloud_id);
  803. $data['total_views'] = $this->cloud_model->get_total_views($cloud_id);
  804. $data['comments'] = $modified_comments;
  805. $data['total_comments'] = $this->comment_model->get_total_comments($cloud_id);
  806. $data['tags'] = $this->tag_model->get_tags('cloud', $cloud_id);
  807. $data['links'] = $modified_links;
  808. $data['references'] = $this->cloud_model->get_references($cloud_id);
  809. $data['contents'] = $modified_contents;
  810. $data['embeds'] = $modified_embeds;
  811. $data['cloudscapes'] = $this->cloud_model->get_cloudscapes($cloud_id);
  812. $data['total_favourites'] = $this->favourite_model->get_total_favourites($cloud_id, 'cloud');
  813. $data['favourite'] = $this->favourite_model->is_favourite($user_id,
  814. $cloud_id, 'cloud');
  815. $data['show_favourite_link'] = $this->favourite_model->can_favourite_item(
  816. $user_id, $cloud_id, 'cloud');
  817. $data['title'] = $data['cloud']->title;
  818. $data['navigation'] = 'clouds';
  819. $data['edit_permission'] = $this->cloud_model->has_edit_permission($user_id,
  820. $cloud_id);
  821. $data['admin'] = $this->auth_lib->is_admin();
  822. $data['following'] = $this->cloud_model->is_following($cloud_id, $user_id);
  823. $data['view'] = $view;
  824. $this->layout->layout('layout_search_view');
  825. $this->layout->view('cloud/search_view', $data);
  826. } else {
  827. // If invalid cloud id, display error page
  828. show_error(t("An error occurred."));
  829. }
  830. }
  831. }