PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/application/controllers/scheduler/s_feeds.php

http://github.com/ushahidi/Ushahidi_Web
PHP | 147 lines | 98 code | 15 blank | 34 comment | 13 complexity | f1850823dc846405b55655d0e52af2a7 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Feeds Scheduler Controller
  4. *
  5. * PHP version 5
  6. * LICENSE: This source file is subject to LGPL license
  7. * that is available through the world-wide-web at the following URI:
  8. * http://www.gnu.org/copyleft/lesser.html
  9. * @author Ushahidi Team <team@ushahidi.com>
  10. * @package Ushahidi - http://source.ushahididev.com
  11. * @subpackage Scheduler
  12. * @copyright Ushahidi - http://www.ushahidi.com
  13. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
  14. */
  15. class S_Feeds_Controller extends Controller {
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. }
  20. /**
  21. * parse feed and send feed items to database
  22. */
  23. public function index()
  24. {
  25. // Max number of feeds to keep
  26. $max_feeds = 100;
  27. // Today's Date
  28. $today = strtotime('now');
  29. // Get All Feeds From DB
  30. $feeds = ORM::factory('feed')->find_all();
  31. foreach ($feeds as $feed)
  32. {
  33. $last_update = $feed->feed_update;
  34. // Parse Feed URL using Feed Helper
  35. $feed_data = feed::simplepie( $feed->feed_url );
  36. foreach($feed_data->get_items(0,50) as $feed_data_item)
  37. {
  38. $title = $feed_data_item->get_title();
  39. $link = $feed_data_item->get_link();
  40. $description = $feed_data_item->get_description();
  41. $date = $feed_data_item->get_date();
  42. $latitude = $feed_data_item->get_latitude();
  43. $longitude = $feed_data_item->get_longitude();
  44. $categories = $feed_data_item->get_categories(); // HT: new code
  45. $category_ids = new stdClass(); // HT: new code
  46. // Make Sure Title is Set (Atleast)
  47. if (isset($title) && !empty($title ))
  48. {
  49. // We need to check for duplicates!!!
  50. // Maybe combination of Title + Date? (Kinda Heavy on the Server :-( )
  51. $dupe_count = ORM::factory('feed_item')->where('item_title',$title)->where('item_date',date("Y-m-d H:i:s",strtotime($date)))->count_all();
  52. if ($dupe_count == 0)
  53. {
  54. // Does this feed have a location??
  55. $location_id = 0;
  56. // STEP 1: SAVE LOCATION
  57. if ($latitude AND $longitude)
  58. {
  59. $location = new Location_Model();
  60. $location->location_name = "Unknown";
  61. $location->latitude = $latitude;
  62. $location->longitude = $longitude;
  63. $location->location_date = date("Y-m-d H:i:s",time());
  64. $location->save();
  65. $location_id = $location->id;
  66. }
  67. $newitem = new Feed_Item_Model();
  68. $newitem->feed_id = $feed->id;
  69. $newitem->location_id = $location_id;
  70. $newitem->item_title = $title;
  71. if (isset($description) AND !empty($description))
  72. {
  73. $newitem->item_description = $description;
  74. }
  75. if (isset($link) AND !empty($link))
  76. {
  77. $newitem->item_link = $link;
  78. }
  79. if (isset($date) AND !empty($date))
  80. {
  81. $newitem->item_date = date("Y-m-d H:i:s",strtotime($date));
  82. }
  83. // Set todays date
  84. else
  85. {
  86. $newitem->item_date = date("Y-m-d H:i:s",time());
  87. }
  88. // HT: new code
  89. if(!empty($categories)) {
  90. foreach($categories as $category) {
  91. $categoryData = ORM::factory('category')->where('category_title', $category->term)->find();
  92. if($categoryData->loaded == TRUE) {
  93. $category_ids->feed_item_category[$categoryData->id] = $categoryData->id;
  94. }
  95. elseif (Kohana::config('settings.allow_feed_category'))
  96. {
  97. $newcategory = new Category_Model();
  98. $newcategory->category_title = $category->term;
  99. $newcategory->parent_id = 0;
  100. $newcategory->category_description = $category->term;
  101. $newcategory->category_color = '000000';
  102. $newcategory->category_visible = 0;
  103. $newcategory->save();
  104. $category_ids->feed_item_category[$newcategory->id] = $newcategory->id;
  105. }
  106. }
  107. }
  108. // HT: End of new code
  109. $newitem->save();
  110. // HT: New code
  111. if(!empty($category_ids->feed_item_category)) {
  112. feed::save_category($category_ids, $newitem);
  113. }
  114. // HT: End of New code
  115. // Action::feed_item_add - Feed Item Received!
  116. Event::run('ushahidi_action.feed_item_add', $newitem);
  117. }
  118. }
  119. }
  120. // Get Feed Item Count
  121. $feed_count = ORM::factory('feed_item')->where('feed_id', $feed->id)->count_all();
  122. if ($feed_count > $max_feeds)
  123. {
  124. // Excess Feeds
  125. $feed_excess = $feed_count - $max_feeds;
  126. }
  127. // Set feed update date
  128. $feed->feed_update = strtotime('now');
  129. $feed->save();
  130. }
  131. }
  132. }