Skip to content

Instantly share code, notes, and snippets.

@kamaulynder
Created January 13, 2011 18:11
Show Gist options
  • Save kamaulynder/778321 to your computer and use it in GitHub Desktop.
Save kamaulynder/778321 to your computer and use it in GitHub Desktop.
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Election Tool Hook - Load All Events
*
* 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 <[email protected]>
* @package Ushahidi - http://source.ushahididev.com
* @copyright Ushahidi - http://www.ushahidi.com
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
*/
class election {
/**
* Registers the main event add method
*/
public function __construct()
{
$this->db = Database::instance();
// Hook into routing
Event::add('system.pre_controller', array($this, 'add'));
}
/**
* Adds all the events to the main Ushahidi application
*/
public function add()
{
// Add a Sub-Nav Link
Event::add('ushahidi_action.nav_main_right_tabs', array($this,
'_election_link'));
// Only add the events if we are on that controller
if (Router::$controller == 'admin')
{
plugin::add_stylesheet('election/views/css/main');
}
elseif(strripos(Router::$current_uri, "admin/users") !== false)
{
// Hook into users report form
Event::add('ushahidi_action.users_form_admin',array($this,
'_users_form'));
//Hook into users submit form
Event::add('ushahidi_action.users_add_admin', array($this,
'_users_form_submit_admin'));
//Hook into users edit action
Event::add('ushahidi_action.users_edit_admin',array($this,
'_update_users_admin_section'));
Event::add('ushahidi_action.users_listing_header',array($this,'_show_adminsection_header_title'));
Event::add('ushahidi_action.users_listing_item',array($this,'_show_adminsection_item'));
//Hook into users delete
Event::add('ushahidi_action.users_delete_admin',array($this,
'_users_delete_admin'));
}
elseif(Router::$helper == 'sms' AND Router::$method ==
'add')
{
Event::add('ushahidi_action.message_sender',array($this,'_message_from'));
//Modify the message description from sms
Event::add('ushahidi_filter.message_description',array($this,'_modify_message_description'));
}
}
public function _election_link()
{
$main_right_tabs = Event::$data;
Event::$data = array_merge($main_right_tabs,
array('election'=>'Election Tool'));
}
/**
* hook into users report form.
*/
public function _users_form() {
// Load the View
$form = View::factory('admin/adminsection_users_form');
$id = Event::$data;
// incase we are editing a user
if ($id)
{
// Do We have an Existing Actionable Item for this Report?
$adminsections = ORM::factory('adminsection')
->where('user_id', $id)
->select_list('id','adminsection_title');
} else {
$adminsections = ORM::factory('adminsection')
->where('adminsection_active', '1')
->select_list('id','adminsection_title');
}
$form->adminsections = $adminsections;
$form->render(TRUE);
}
/**
* Add users to an admin section
*/
public function _users_form_submit_admin() {
$post = Event::$data;
$sql = "SELECT id FROM
".Kohana::config('database.default.table_prefix')."users WHERE username
='".$post->username."'";
$user_id = $this->db->query($sql);
if($post) {
// pull user id from users table for insertion in adminsection.
//Prevent duplicate entries.
if($this->_get_user_id('adminsection_users',$user_id[0]->id) !=
$user_id[0]->id) {
$adminsection = ORM::factory('adminsection_user');
$adminsection->user_id = $user_id[0]->id;
$adminsection->adminsection_id = $post->adminsection_title;
$adminsection->save();
}
}
}
/**
* Update a users admin section
*/
public function _update_users_admin_section()
{
$user_data = Event::$data;
//echo kohana::debug($user_data);
$user_id = $user_data[0];
$post = $user_data[1];
if ($post)
{
//add new
$userid = $this->_get_user_id('adminsection_users',$user_id);
if (empty($userid))
{
$this->db->insert('adminsection_users',array('user_id'=>$user_id,'adminsection_id'=>$post->adminsection_title));
}
else
{
//update
$this->db->update('adminsection_users',
array("adminsection_id" => $post->adminsection_title),
array('user_id' => $user_id));
}
}
}
/**
* Delete from adminsection table
*/
public function _users_delete_admin()
{
$users = Event::$data;
$this->db->delete('adminsection_users',
array('user_id' => $users->id));
}
/**
* Show the header title for the admisection at the users list page.
*/
public function _show_adminsection_header_title()
{
$template = View::factory('admin/adminsection_header_title');
$template->header_title = "Adminsection";
$template->render(TRUE);
}
/**
* Show the adminsection the user is in at the users
* listing page.
*/
public function _show_adminsection_item()
{
$user_id = Event::$data;
if ($user_id)
{
$adminsection_title = $this->_get_admin_section($user_id);
}
//Load the view
$template = View::factory('admin/adminsection_item');
if ( ! empty($adminsection_title))
{
$template->adminsection_title = $adminsection_title;
}
else
{
$template->adminsection_title = "None";
}
$template->render(TRUE);
}
/**
* Get admin section title.
*/
public function _get_admin_section($user_id)
{
$admin_section = ORM::factory('adminsection_user')
->where('user_id',$user_id)->orderby('adminsection_id')
->join('adminsection','adminsection_users.adminsection_id','adminsection.id','INNER')
->find();
return $admin_section->adminsection->adminsection_title;
}
/**
* Get user id
*/
public function _get_user_id($table_name, $user_id )
{
$user =
$this->db->from($table_name)->select('user_id')->where('user_id',$user_id)->get();
if ($user[0])
{
return $user[0]->user_id;
}
else
{
return "";
}
}
}
new election;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment