diff --git a/application/classes/Controller/Client.php b/application/classes/Controller/Client.php index 5666d4c..d124316 100644 --- a/application/classes/Controller/Client.php +++ b/application/classes/Controller/Client.php @@ -6,7 +6,6 @@ class Controller_Client extends Controller_Layout { protected $secure_actions = array( 'index', - 'group', 'create', 'edit', 'delete', @@ -21,18 +20,22 @@ class Controller_Client extends Controller_Layout { { $this->template = new View_Client_Index; $this->template->title = __('Clients'); - $this->template->items = ORM::factory('Client') - ->filter_by_page($this->request->param('page')) - ->find_all(); - } - - public function action_group() - { - $this->template = new View_Client_Index; - $this->template->title = __('Clients'); - $this->template->items = ORM::factory('Client') - ->filter_by_page($this->request->param('page')) - ->find_by_group($this->request->param('group_id')); + $group_id = $this->request->post('group_id'); + if ($group_id) { + $group = ORM::factory('Group', $group_id); + if (!$group->loaded()) { + $this->redirect('error/404'); + } + $this->template->title .= ' - ' . $group->name; + $this->template->items = $group + ->clients + ->filter_by_page($this->request->param('page')) + ->find_all(); + } else { + $this->template->items = ORM::factory('Client') + ->filter_by_page($this->request->param('page')) + ->find_all(); + } } /** @@ -74,7 +77,7 @@ class Controller_Client extends Controller_Layout { $confirmation = $this->request->post('confirmation'); if ($confirmation === 'yes') { - $post->delete(); + $model->delete(); $this->redirect('/'); } } @@ -93,11 +96,4 @@ class Controller_Client extends Controller_Layout { ->filter_by_page($this->request->param('page')) ->find_all(); } - - /** - * Unsubscription link action. - **/ - public function action_unsubscribe() - { - } } diff --git a/application/classes/Controller/Group.php b/application/classes/Controller/Group.php new file mode 100644 index 0000000..a21bd7c --- /dev/null +++ b/application/classes/Controller/Group.php @@ -0,0 +1,99 @@ + 'input', + ); + public function action_index() + { + $this->template = new View_Index; + $this->template->title = __('Groups'); + $this->template->header = [ + __('Group name'), + __('Edit'), + __('Delete'), + ]; + $this->template->items = ORM::factory('Group') + ->filter_by_page($this->request->param('page')) + ->find_all(); + } + + /** + * Manually add a group. + **/ + public function action_create() + { + $this->template = new View_Edit; + $this->template->model = ORM::factory('Group'); + $this->template->title = __('New group'); + $this->_edit($this->template->model); + } + + public function action_edit() + { + $this->template = new View_Edit; + $this->template->title = __('Edit group info'); + $id = $this->request->param('id'); + $model = ORM::factory('Group', $id); + if (!$model->loaded()) + { + $this->redirect('error/404'); + } + $this->_edit($model); + } + + public function action_delete() + { + $this->template = new View_Delete; + $id = $this->request->param('id'); + $model = ORM::factory('Group', $id); + if (!$model->loaded()) + { + $this->redirect('error/404'); + } + $this->template->title = __('Delete group'); + $this->template->content_title = $model->name; + // $this->template->content = $model->email; + // TODO - display a list of subscribers in group + + $confirmation = $this->request->post('confirmation'); + if ($confirmation === 'yes') { + $model->delete(); + $this->redirect('/'); + } + } + + /** + * Group view. + **/ + public function action_view() + { + $this->redirect('client/index?group_id='.$this->request->param('id')); + } + + /** + * Search groups. + **/ + public function action_search() + { + $this->template = new View_Client_Index; + $this->template->show_create = FALSE; + $this->template->title = __('Groups'); + $query = $this->request->post('query'); + $this->template->items = ORM::factory('Group') + ->search($query) + ->filter_by_page($this->request->param('page')) + ->find_all(); + } +} diff --git a/application/classes/Form.php b/application/classes/Form.php index 544bca2..333e4ac 100644 --- a/application/classes/Form.php +++ b/application/classes/Form.php @@ -1,10 +1,10 @@ -name = $name; $template->label = __(Arr::get($attributes, 'label')); $template->options = array(); - foreach ($options as $name => $value) + foreach ($options as $value => $text) { $template->options[] = array( - 'name' => $name, 'value' => $value, - 'is_selected' => ($name == $selected) + 'text' => $text, + 'is_selected' => ($value == $selected) ); } return self::render_control($template); @@ -214,7 +214,7 @@ class Form extends Kohana_Form { { return self::btn('submit', __($text), 'primary', 'submit'); } - + protected static function render_control($template) { $renderer = Kostache_Layout::factory('form/control'); diff --git a/application/classes/Model/Client.php b/application/classes/Model/Client.php index fe38916..536f65d 100644 --- a/application/classes/Model/Client.php +++ b/application/classes/Model/Client.php @@ -65,4 +65,15 @@ class Model_Client extends ORM { ->or_where(DB::expr('LOWER(email)'), 'LIKE', strtolower($query)); } + /** + * A function to search by group ID + * @param int $group_id + */ + public function find_by_group($group_id) { + return $this + ->with('groups') + ->find() + ->where('groups.id', '=', (int) $group_id); + } + } diff --git a/application/classes/Model/Group.php b/application/classes/Model/Group.php index f639990..88c9027 100644 --- a/application/classes/Model/Group.php +++ b/application/classes/Model/Group.php @@ -19,10 +19,10 @@ class Model_Group extends ORM { public function rules() { return array( - 'title' => array( - array('not_empty'), - array('min_length', array(':value', 4)), - array('max_length', array(':value', 255)), + 'name' => array( + array('not_empty'), + array('min_length', array(':value', 4)), + array('max_length', array(':value', 255)), ), ); } @@ -32,7 +32,7 @@ class Model_Group extends ORM { * Used in forms. **/ protected $_labels = array( - 'title' => 'Title', + 'name' => 'Name', ); public static function count($id) diff --git a/application/classes/View/Client/Index.php b/application/classes/View/Client/Index.php index 3ddbdfe..3a6fcdb 100644 --- a/application/classes/View/Client/Index.php +++ b/application/classes/View/Client/Index.php @@ -9,6 +9,27 @@ class View_Client_Index extends View_Index { protected $is_admin = TRUE; // admin only view public $show_date = FALSE; public $subscription_id; + public $group; + public function get_header() + { + return array( + __('Name'), + __('Email'), + __('Edit'), + __('Delete') + ); + } + + /** + * Group search field + */ + public function groups() { + return Form::open() + . Form::select('group_id', ORM::factory('Group')->find_all()->as_array('id', 'name')) + . Form::submit('s', __('Submit')) + . Form::close(); + } + /** * An internal function to prepare item data. **/ diff --git a/application/classes/View/Index.php b/application/classes/View/Index.php index bfa56d3..646bc21 100644 --- a/application/classes/View/Index.php +++ b/application/classes/View/Index.php @@ -22,6 +22,10 @@ class View_Index extends View_Layout { * Index description **/ public $content = ''; + /** + * Table header + */ + public $header = NULL; protected $is_admin; @@ -51,6 +55,14 @@ class View_Index extends View_Layout { return $output; } + /** + * Table header + */ + public function get_header() + { + return $this->header; + } + public function get_items() { $result = array(); @@ -87,10 +99,6 @@ class View_Index extends View_Layout { $output = array( 'view_link' => HTML::anchor(Route::url('default', array('controller' => Request::current()->controller(), 'action' => 'view','id' => $item->id)), $item->name, array('class' => 'link_view')), ); - if ($this->show_date) - { - $output['date'] = $item->posted_at; - } if ($this->is_admin and $this->show_edit) { $output['edit_link'] = HTML::anchor(Route::url('default', array('controller' => Request::current()->controller(), 'action' => 'edit','id' => $item->id)), __('Edit'), array('class' => 'link_edit')); diff --git a/application/classes/View/Layout.php b/application/classes/View/Layout.php index a4da51d..ea53fea 100644 --- a/application/classes/View/Layout.php +++ b/application/classes/View/Layout.php @@ -11,12 +11,12 @@ class View_Layout { public $base_scripts = array( ); public $errors; - + /** * Inherited paging function **/ public function get_paging() {} - + public function has_errors() { return !empty($this->errors); @@ -80,6 +80,7 @@ class View_Layout { __('Courses') => 'course/index', __('Subscriptions') => 'subscription/index', __('Clients') => 'client/index', + __('Groups') => 'group/index', )); } @@ -121,7 +122,7 @@ class View_Layout { return array( 'button_text' => __('Submit'), 'input_text' => __('Search'), - 'action' => Route::url('default', array('controller' => 'Client', 'action' => 'search')) + 'action' => Route::url('default', array('controller' => 'Client', 'action' => 'search')) ); } } diff --git a/application/i18n/ru.php b/application/i18n/ru.php index 88f0c9b..6c9fa5c 100644 --- a/application/i18n/ru.php +++ b/application/i18n/ru.php @@ -73,5 +73,8 @@ return array( 'Your e-mail' => 'Ваш адрес электронной почты', 'Tired of receiving these emails? Click this link to unsubscribe.' => 'Устали получать эти письма? Нажмите на эту ссылку для отписки.', 'Client not found. Possible subscription token problem.' => 'Клиент не найден. Возможна проблема с токеном подписки.', - 'You have been successfully unsubscribed from course %s' => 'Вы были успешно отписаны от курса %s' + 'You have been successfully unsubscribed from course %s' => 'Вы были успешно отписаны от курса %s', + 'Groups' => 'Группы', + 'Group name' => 'Название группы', + 'Delete group' => 'Удалить группу', ); diff --git a/application/scss/layout.scss b/application/scss/layout.scss index 35a6cb0..ac0bd6c 100644 --- a/application/scss/layout.scss +++ b/application/scss/layout.scss @@ -56,3 +56,15 @@ footer { } } } +.groupform { + margin-top: -3em; + float: right; + width: 50%; + .form-group { + width: 50%; + display: inline-block; + } + input { + margin-right: 1em; + } +} diff --git a/application/templates/client/index.mustache b/application/templates/client/index.mustache index c0e63f9..4df5834 100644 --- a/application/templates/client/index.mustache +++ b/application/templates/client/index.mustache @@ -1,10 +1,21 @@ -{{#show_create}} - {{{link_new}}} -{{/show_create}} -
{{{link_new}}}
+{{/show_create}} +{{#content}} +{{.}} | +{{/get_header}} + + {{#get_items}}
---|
{{{view_link}}} | @@ -15,4 +26,5 @@ {{/edit_link}}
+ {{.}} + | +{{/get_header}} + + {{#get_items}} -||
---|---|---|
{{{view_link}}} | {{#edit_link}} -{{{edit_link}}} | +{{{delete_link}}} | {{/edit_link}} - +