Skip to content

Commit

Permalink
Merge pull request #1291 from HTSolution/develop
Browse files Browse the repository at this point in the history
#623 Feed Category
  • Loading branch information
kamaulynder committed Jan 16, 2014
2 parents 12f6936 + 9ebbeb8 commit 83aaa4f
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 7 deletions.
3 changes: 2 additions & 1 deletion application/config/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
'api_url' => '',
'api_url_all' => '',
'subdomain' => $subdomain,
'title_delimiter' => ' | '
'title_delimiter' => ' | ',
'alert_days' => 0 // HT: No of days of alert to be sent
);
9 changes: 9 additions & 0 deletions application/controllers/admin/reports.php
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,15 @@ public function edit($id = FALSE, $saved = FALSE)
$form['longitude'] = $feed_item->location->longitude;
$form['location_name'] = $feed_item->location->location_name;
}
// HT: new code
$feed_categories = ORM::factory('feed_category')->where('feed_item_id', $feed_item->id)->select_list('id', 'category_id');
if ($feed_categories)
{
foreach($feed_categories as $feed_category) {
$form['incident_category'][] = $feed_category;
}
}
// HT: end of new code
}
else
{
Expand Down
8 changes: 6 additions & 2 deletions application/controllers/admin/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public function site()
'manually_approve_users' => '',
'require_email_confirmation' => '',
'google_analytics' => '',
'api_akismet' => ''
'api_akismet' => '',
'alert_days' => 0, // HT: No of days of alert to be sent
);
// Copy the form as errors, so the errors will be stored with keys
// corresponding to the form field names
Expand Down Expand Up @@ -115,6 +116,7 @@ public function site()
$post->add_rules('require_email_confirmation','required','between[0,1]');
$post->add_rules('google_analytics','length[0,20]');
$post->add_rules('api_akismet','length[0,100]', 'alpha_numeric');
$post->add_rules('alert_days', 'numeric'); // HT: No of days of alert to be sent

// Add rules for file upload
$files = Validation::factory($_FILES);
Expand Down Expand Up @@ -238,6 +240,7 @@ public function site()
else
{
$settings = Settings_Model::get_array();
$settings['alert_days'] = (isset($settings['alert_days'])) ? $settings['alert_days'] : 0; // HT: might not be in database so calling manually retrun NULL if not exist

$form = array(
'site_name' => $settings['site_name'],
Expand Down Expand Up @@ -265,7 +268,8 @@ public function site()
'manually_approve_users' => $settings['manually_approve_users'],
'require_email_confirmation' => $settings['require_email_confirmation'],
'google_analytics' => $settings['google_analytics'],
'api_akismet' => $settings['api_akismet']
'api_akismet' => $settings['api_akismet'],
'alert_days' => $settings['alert_days'] // HT: No of days of alert to be sent
);
}

Expand Down
34 changes: 30 additions & 4 deletions application/controllers/scheduler/s_alerts.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,19 @@ public function index()
// HT: New Code
// Fixes an issue with one report being sent out as an alert more than ones
// becoming spam to users
$incidents = $db->query("SELECT i.id, incident_title,
incident_description, incident_verified,
l.latitude, l.longitude FROM ".$this->table_prefix."incident AS i INNER JOIN ".$this->table_prefix."location AS l ON i.location_id = l.id
WHERE i.incident_active=1 AND i.incident_alert_status = 1 ");
$incident_query = "SELECT i.id, incident_title,
incident_description, incident_verified,
l.latitude, l.longitude FROM ".$this->table_prefix."incident AS i INNER JOIN ".$this->table_prefix."location AS l ON i.location_id = l.id
WHERE i.incident_active=1 AND i.incident_alert_status = 1 ";
/** HT: Code for alert days limitation
* @int alert_days = 0 : All alerts
* @int alert_days = 1 : TODAY
* @int alert_days > 1 : alert_days - 1 days before
*/
if($alert_days = $settings['alert_days'])
{
$incident_query .= "AND DATE(i.incident_date) >= DATE_SUB( CURDATE(), INTERVAL ".($alert_days-1)." DAY )";
}
// End of New Code

foreach ($incidents as $incident)
Expand Down Expand Up @@ -128,6 +137,10 @@ public function index()

foreach ($alertees as $alertee)
{
// HT: check same alert_receipent multi subscription does not get multiple alert
if($this->_multi_subscribe($alertee, $incident->id)) {
continue;
}
// Check the categories
if (!$this->_check_categories($alertee, $category_ids)) {
continue;
Expand Down Expand Up @@ -263,4 +276,17 @@ private function _check_categories(Alert_Model $alertee, array $category_ids) {

return $ret;
}

/**
* HT: Function to verify that alert is not sent to same alert_receipent being subscribed multiple time
* @param Alert_Model $alertee
* @param integer $incident_id
* @return boolean
*/
private function _multi_subscribe(Alert_Model $alertee, $incident_id) {
$multi_subscribe_ids = ORM::factory('alert')->where('alert_confirmed','1')->where('alert_recipient', $alertee->recipient)->select_list('id', 'id');
$subscription_alert = ORM::factory('alert_sent')->where('incident_id', $incident_id)->in('alert_id', $multi_subscribe_ids)->find();
return ((boolean) $subscription_alert->id);
}

}
27 changes: 27 additions & 0 deletions application/controllers/scheduler/s_feeds.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public function index()
$date = $feed_data_item->get_date();
$latitude = $feed_data_item->get_latitude();
$longitude = $feed_data_item->get_longitude();
$categories = $feed_data_item->get_categories(); // HT: new code
$category_ids = new stdClass(); // HT: new code

// Make Sure Title is Set (Atleast)
if (isset($title) && !empty($title ))
Expand Down Expand Up @@ -93,8 +95,33 @@ public function index()
{
$newitem->item_date = date("Y-m-d H:i:s",time());
}
// HT: new code
if(!empty($categories)) {
foreach($categories as $category) {
$categoryData = ORM::factory('category')->where('category_title', $category->term)->find();
if($categoryData) {
$category_ids->feed_category[$categoryData->id] = $categoryData->id;
} else {
$newcategory = new Category_Model();
$newcategory->category_title = $category->term;
$newcategory->parent_id = 0;
$newcategory->category_description = $category->term;
$newcategory->category_color = '000000';
$newcategory->save();
$category_ids->feed_category[$newcategory->id] = $newcategory->id;
}
}
}
// HT: End of new code

$newitem->save();

// HT: New code
if(!empty($category_ids->feed_category)) {
feed::save_category($category_ids, $newitem);
}
// HT: End of New code

// Action::feed_item_add - Feed Item Received!
Event::run('ushahidi_action.feed_item_add', $newitem);
}
Expand Down
16 changes: 16 additions & 0 deletions application/helpers/feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,20 @@ public static function simplepie( $feed_url = NULL )

return $data;
}

// HT: New function to save category of feed
public static function save_category($post, $feed_item)
{
// Delete Previous Entries
ORM::factory('feed_category')->where('feed_item_id', $feed_item->id)->delete_all();

foreach ($post->feed_category as $item)
{
$feed_category = new Feed_Category_Model();
$feed_category->feed_item_id = $feed_item->id;
$feed_category->category_id = $item;
$feed_category->save();
}
}

}
48 changes: 48 additions & 0 deletions application/models/feed_category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php defined('SYSPATH') or die('No direct script access.');
// HT: New model
/**
* Model for Categories for each Feed
*
* PHP version 5
* LICENSE: This source file is subject to LGPL license
* that is available through the world-wide-web at the following URI:
* http://www.gnu.org/copyleft/lesser.html
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi - http://source.ushahididev.com
* @subpackage Models
* @copyright Ushahidi - http://www.ushahidi.com
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
*/

class Feed_Category_Model extends ORM
{
protected $belongs_to = array('feed_item', 'category');

// Database table name
protected $table_name = 'feed_category';

/**
* Assigns a category id to an feed if it hasn't already been assigned
* @param int $feed_id feed to assign the category to
* @param int $category_id category id of the category you want to assign to the feed
* @return array
*/
public static function assign_category_to_feed($feed_id,$category_id)
{

// Check to see if it is already added to that category
// If it's not, add it.

$feed_category = ORM::factory('feed_category')->where(array('feed_item_id'=>$feed_id,'category_id'=>$category_id))->find_all();

if( ! $feed_category->count() )
{
$new_feed_category = ORM::factory('feed_category');
$new_feed_category->category_id = $category_id;
$new_feed_category->feed_item_id = $feed_id;
$new_feed_category->save();
}

return true;
}
}
7 changes: 7 additions & 0 deletions application/models/feed_item.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@ class Feed_Item_Model extends ORM

// Database table name
protected $table_name = 'feed_item';

// HT: New code
protected $has_many = array(
'category' => 'feed_category',
);
// HT: End of new code

}
7 changes: 7 additions & 0 deletions application/views/admin/settings/site.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@
<?php print form::dropdown('allow_comments', $comments_array, $form['allow_comments']); ?>
</span>
</div>
<!-- HT: Number of alert days setting -->
<div class="row">
<h4><a href="#" class="tooltip" title="<?php echo Kohana::lang("tooltips.settings_alert_days"); ?>"><?php echo Kohana::lang('settings.site.alert_days');?></a>
<br /><?php echo Kohana::lang('settings.site.alert_days_notice');?></h4>
<?php print form::input('alert_days', $form['alert_days'], ' class="text long2"'); ?>
</div>
<!-- HT: End of Number of alert days setting -->
<div class="row">
<h4><a href="#" class="tooltip" title="<?php echo Kohana::lang("tooltips.settings_allow_feed"); ?>"><?php echo Kohana::lang('settings.site.allow_feed');?></a></h4>
<span class="sel-holder">
Expand Down
14 changes: 14 additions & 0 deletions sql/upgrade111-112.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Table structure for table `feed_item_category`
*
*/

CREATE TABLE IF NOT EXISTS `feed_item_category` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`feed_item_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`category_id` int(11) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `feed_item_category_ids` (`feed_item_id`,`category_id`),
KEY `feed_item_id` (`feed_item_id`),
KEY `category_id` (`category_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Stores fetched feed items categories' AUTO_INCREMENT=1 ;

0 comments on commit 83aaa4f

Please sign in to comment.