Skip to content

Commit

Permalink
Merge pull request #56 from ushahidi/groups-dev
Browse files Browse the repository at this point in the history
Groups Setup
  • Loading branch information
David Kobia committed Oct 2, 2013
2 parents 4aa75a7 + 05c671a commit 2b580ff
Show file tree
Hide file tree
Showing 15 changed files with 459 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ php:
- 5.3
services:
- mysql
env:
- KOHANA_ENV=testing
before_script:
- composer install --no-interaction --prefer-source # Have to prefer source or hit github rate limit
- git submodule update --init --recursive
Expand Down
17 changes: 13 additions & 4 deletions application/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,29 @@
/**
* Set Kohana::$environment if a 'KOHANA_ENV' environment variable has been supplied.
*
* Note: If you supply an invalid environment name, a PHP warning will be thrown
* saying "Couldn't find constant Kohana::<INVALID_ENV_NAME>"
* Note: If you supply an invalid environment name 'development' will be used instead
*/
if (isset($_SERVER['KOHANA_ENV']))
if (($env = getenv('KOHANA_ENV')) === FALSE OR defined('Kohana::'.strtoupper($env)) === FALSE)
{
Kohana::$environment = constant('Kohana::'.strtoupper($_SERVER['KOHANA_ENV']));
$env = 'development';
}

// Ignoring code standards error about constant case
// @codingStandardsIgnoreStart
Kohana::$environment = constant('Kohana::'.strtoupper($env));
// @codingStandardsIgnoreEnd

/**
* Attach a file reader to config. Multiple readers are supported.
*/
Kohana::$config = new Config;
Kohana::$config->attach(new Config_File);

/**
* Attach the environment specific configuration file reader to config
*/
Kohana::$config->attach(new Config_File('config/environments/'.$env));

/**
* Initialize Kohana, setting the default options.
*/
Expand Down
110 changes: 110 additions & 0 deletions application/classes/Controller/Groups.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');

/**
* Groups Controller
*
* @author Ushahidi Team <[email protected]>
* @package Ushahidi\Application\Controllers
* @copyright Ushahidi - http://www.ushahidi.com
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License Version 3 (GPLv3)
*/
class Controller_Groups extends Controller_PingApp {

/**
* List all Groups
*
* @return void
*/
public function action_index()
{
$this->template->content = View::factory('pages/groups/list')
->bind('groups', $groups);

$groups = $this->user->groups
->select(array(DB::expr('COUNT(gp.person_id)'), 'people'))
->join(array('groups_people', 'gp'), 'LEFT')
->on('gp.group_id', '=', 'group.id')
->join(array('people', 'p'), 'LEFT')
->on('p.id', '=', 'gp.person_id')
->group_by('group.id')
->order_by('name', 'ASC')
->find_all();
}

/**
* Add/Edit Group
*
* @return void
*/
public function action_edit()
{
$this->template->content = View::factory('pages/groups/edit')
->bind('group', $group)
->bind('post', $post)
->bind('errors', $errors)
->bind('done', $done);

$group_id = $this->request->param('id', 0);
$group = ORM::factory('Group')
->where('id', '=', $group_id)
->where('user_id', '=', $this->user->id)
->find();

if ( ! empty($_POST) )
{
$post = $_POST;

try
{
// Save Group
$group->values($post, array(
'name',
));
$group->check();
$group->user_id = $this->user->id;
$group->save();

// Redirect to prevent repost
HTTP::redirect('groups/edit/'.$group->id.'?done');
}
catch (ORM_Validation_Exception $e)
{
$errors = Arr::flatten($e->errors('models'));
}
}
else
{
if ( $group->loaded() )
{
$post = $group->as_array();
}

$done = (isset($_GET['done'])) ? TRUE : FALSE;
}
}

/**
* Delete A Group
*
* @return void
*/
public function action_delete()
{
$group_id = $this->request->param('id', 0);

$group = ORM::factory('Group')
->where('id', '=', $group_id)
->where('user_id', '=', $this->user->id)
->find();

if ( $group->loaded() )
{
$group->delete();
HTTP::redirect('groups');
}
else
{
HTTP::redirect('groups');
}
}
}
40 changes: 37 additions & 3 deletions application/classes/Controller/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ class Controller_Person extends Controller_PingApp {
public function action_edit()
{
$this->template->content = View::factory('pages/person/edit')
->bind('user', $this->user)
->bind('groups', $groups)
->bind('person', $person)
->bind('parent', $parent)
->bind('post', $post)
->bind('errors', $errors)
->bind('done', $done);

$groups = $this->user->groups
->order_by('name', 'ASC')
->find_all();

$this->template->footer->js = View::factory('pages/person/js/edit');

$person_id = $this->request->param('id', 0);
Expand Down Expand Up @@ -54,6 +60,7 @@ public function action_edit()
if ( ! empty($_POST) )
{
$post = $_POST;
print_r($post);
$extra_validation = Validation::factory($post)
->rule('contact', 'not_empty')
->rule('contact', 'is_array')
Expand All @@ -75,7 +82,28 @@ public function action_edit()
$person->user_id = $this->user->id;
$person->save();

// 2. Delete A Contact
// 2. Save Group Info
// Drop relationships first
foreach ($person->groups->find_all() as $group)
{
$person->remove('groups', $group);
}
// Re-Attach Groups
foreach ($post['group'] as $key => $group_id)
{
$group = ORM::factory('Group')
->where('id', '=', (int) $group_id)
->where('user_id', '=', $this->user->id)
->find();

if( $group->loaded() AND ! $person->has('groups', $group))
{
// Add Relationship
$person->add('groups', $group);
}
}

// 3. Delete A Contact
foreach ($post['delete'] as $delete_id)
{

Expand All @@ -87,9 +115,9 @@ public function action_edit()
// Remove Relationship
$person->remove('contacts', $contact);
}
}
}

// 3. Save Contact Info
// 4. Save Contact Info
foreach ($post['contact'] as $key => $_contact)
{
// Clean Contact Before Comparing
Expand Down Expand Up @@ -134,6 +162,12 @@ public function action_edit()
'name' => $person->name,
);

// Get Person Groups
foreach ($person->groups->find_all() as $group)
{
$post['group'][] = $group->id;
}

// Get Person Contacts
foreach ($person->contacts->find_all() as $contact)
{
Expand Down
13 changes: 12 additions & 1 deletion application/classes/Model/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Model_Group extends ORM {
* A group has many people
*/
protected $_has_many = array(
'people' => array(),
'people' => array('through' => 'groups_people'),
);

/**
Expand All @@ -27,4 +27,15 @@ class Model_Group extends ORM {
// Insert/Update Timestamps
protected $_created_column = array('column' => 'created', 'format' => 'Y-m-d H:i:s');
protected $_updated_column = array('column' => 'updated', 'format' => 'Y-m-d H:i:s');

public function rules()
{
return array(
'name' => array(
array('not_empty'),
array('min_length', array(':value', 3)),
array('max_length', array(':value', 150)),
)
);
}
}
3 changes: 2 additions & 1 deletion application/classes/Model/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ class Model_Person extends ORM {
'pings' => array(),
'pongs' => array(),
'contacts' => array('through' => 'contacts_people'),
'groups' => array('through' => 'groups_people'),
'children' => array(
'model' => 'Person',
'foreign_key' => 'parent_id',
),
);

/**
* A person belongs to a parent, user
* A person belongs to a parent, user and a group
*/
protected $_belongs_to = array(
'user' => array(),
Expand Down
2 changes: 1 addition & 1 deletion application/classes/PingApp/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static function contact_types($select = TRUE)
* Get People for Send Form
*
* @param int $user_id
* @returnarray
* @return array
*/
public static function people($user)
{
Expand Down
18 changes: 18 additions & 0 deletions application/config/environments/testing/auth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php defined('SYSPATH') or die('No direct access allowed.');

return array(

'driver' => 'ORM',
'hash_method' => 'sha256',
'hash_key' => 'somereallylongkey',
'lifetime' => 1209600,
'session_type' => Session::$default,
'session_key' => 'auth_user',

// Username/password combinations for the Auth File driver
'users' => array(
// 'admin' => 'b3154acf3a344170077d11bdb5fff31532f679a1919e716a02',
'admin' => ''
),

);
29 changes: 29 additions & 0 deletions application/config/environments/testing/database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php defined('SYSPATH') or die('No direct access allowed.');

/**
* Database Config
*
* @author Ushahidi Team <[email protected]>
* @package Ushahidi\Application
* @copyright Ushahidi - http://www.ushahidi.com
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License Version 3 (GPLv3)
*/

return array
(
'default' => array
(
'type' => 'MySQL',
'connection' => array(
'hostname' => 'localhost',
'database' => 'pingapp_test',
'username' => 'root',
'password' => '',
'persistent' => FALSE,
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => TRUE,
'profiling' => TRUE,
)
);
42 changes: 42 additions & 0 deletions application/migrations/1/20131001183143.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php defined('SYSPATH') OR die('No direct script access.');

class Migration_1_20131001183143 extends Minion_Migration_Base {

/**
* Run queries needed to apply this migration
*
* @param Kohana_Database $db Database connection
*/
public function up(Kohana_Database $db)
{
/**
* Groups_People Table
*/
$db->query(NULL, "DROP TABLE IF EXISTS `groups_people`;");
$db->query(NULL, "CREATE TABLE `groups_people` (
`group_id` int(11) unsigned NOT NULL DEFAULT '0',
`person_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`group_id`,`person_id`),
CONSTRAINT `fk_groups_people_group_id` FOREIGN KEY (`group_id`) REFERENCES `groups` (`id`) ON DELETE CASCADE,
CONSTRAINT `fk_groups_people_person_id` FOREIGN KEY (`person_id`) REFERENCES `people` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;");

// Drop group_id column from people
$db->query(NULL, "ALTER TABLE `people` DROP `group_id`;");

}

/**
* Run queries needed to remove this migration
*
* @param Kohana_Database $db Database connection
*/
public function down(Kohana_Database $db)
{
$db->query(NULL, "ALTER TABLE `people` ADD `group_id` INT(11) UNSIGNED NOT NULL DEFAULT '0' AFTER `user_id`;");
$db->query(NULL, "ALTER TABLE `people` ADD INDEX `idx_group_id` (`group_id`);");

$db->query(NULL, "DROP TABLE IF EXISTS `groups_people`;");
}

}
Loading

0 comments on commit 2b580ff

Please sign in to comment.