1
0
Fork 0
mirror of https://github.com/Oreolek/kangana.git synced 2024-06-16 23:11:07 +03:00

Course creating/editing

This commit is contained in:
Alexander Yakovlev 2016-10-27 13:36:38 +07:00
parent 213536ce11
commit c5f5d0de3d
4 changed files with 19 additions and 43 deletions

View file

@ -61,6 +61,16 @@ class Controller_Course extends Controller_Layout {
];
$this->template->model = ORM::factory('Course');
$this->template->title = I18n::translate('New course');
if ($this->request->method() === Request::POST) {
$this->template->model->price = $this->request->param('price', 0);
$this->template->model->period = $this->request->param('period', 1);
$this->template->model->values($this->request->post(), ['type', 'group_id']);
}
if ((int) $this->template->model->type === Model_Course::TYPE_SCHEDULED)
{
$this->controls['period'] = 'input';
$this->controls['price'] = 'input';
}
$this->_edit($this->template->model);
}
@ -126,40 +136,18 @@ class Controller_Course extends Controller_Layout {
'title' => 'input',
'description' => 'textarea',
];
if ($model->type === Model_Course::TYPE_SCHEDULED)
if ($this->request->method() === Request::POST) {
$model->price = $this->request->param('price', 0);
$model->period = $this->request->param('period', 1);
$model->values($this->request->post(), ['type', 'group_id']);
}
if ((int) $model->type === Model_Course::TYPE_SCHEDULED)
{
$this->controls['period'] = 'input';
$this->controls['price'] = 'input';
};
$this->template->errors = array();
if ($this->request->method() === Request::POST) {
$model->values($this->request->post(), array_keys($this->controls));
$model->values($this->request->post(), ['type', 'group_id']);
$model->customize();
$validation = $model->validate_create($this->request->post());
try
{
if ($validation->check())
{
$model->save();
$this->after_save($model);
}
else
{
$this->template->errors = $validation->errors('default');
}
}
catch (ORM_Validation_Exception $e)
{
$this->template->errors = $e->errors('default');
}
if (empty($this->template->errors))
{
$this->redirect($this->_edit_redirect($model));
}
}
$this->template->model = $model;
$this->_edit($model);
}
public function action_delete()

View file

@ -82,18 +82,6 @@ class Model_Course extends ORM {
];
}
public function customize()
{
if ($this->period < 1)
{
$this->period = 1;
}
if (empty($this->price))
{
$this->price = 0;
}
}
public static function count_letters($id)
{
$query = DB::select(array(DB::expr('COUNT(*)'), 'cnt'))->from('letters')->where('course_id', '=', $id);

View file

@ -4,7 +4,7 @@
* Course details editing view controller
**/
class View_Course_Edit extends View_Edit {
public function group_select()
public function custom_controls()
{
return Form::select('group_id', ORM::factory('Group')->find_all()->as_array('id', 'name'), NULL, [
'label' => I18n::translate('Group')

View file

@ -7,7 +7,7 @@
{{/get_errors}}
</ul>
{{/has_errors}}
{{{group_select}}}
{{{custom_controls}}}
{{{get_controls}}}
</form>
{{#preview}}