diff --git a/application/classes/Controller/Course.php b/application/classes/Controller/Course.php index 0778936..0f8f029 100644 --- a/application/classes/Controller/Course.php +++ b/application/classes/Controller/Course.php @@ -31,6 +31,55 @@ class Controller_Course extends Controller_Layout { $this->_edit($this->template->model); } + /** + * One-page course creation + **/ + public function action_simple() + { + $this->template = new View_Course_Simple; + $this->template->controls = array(); + $this->template->title = __('New course'); + if ($this->request->method() === HTTP_Request::POST) { + $course = ORM::factory('Course'); + $letter = ORM::factory('Letter'); + $course->values($this->request->post(), array('title', 'description')); + $letter->subject = $this->request->post('letter_subject'); + $letter->text = $this->request->post('letter_body'); + $course->price = 0; + $course->period = 1; + $letter->order = 1; + $validation_course = $course->validate_create($this->request->post()); + $validation_letter = $letter->validate_create(array( + 'subject' => $this->request->post('letter_subject'), + 'text' => $this->request->post('letter_body') + )); + try + { + if ($validation_course->check() AND $validation_letter->check()) + { + $course->create(); + $letter->course_id = $course->id; + $letter->create(); + } + else + { + $this->template->errors = array_merge( + $validation_course->errors('default'), + $validation_letter->errors('default') + ); + } + } + catch (ORM_Validation_Exception $e) + { + $this->template->errors = $e->errors('default'); + } + if (empty($this->template->errors)) + { + $this->redirect($this->_edit_redirect($course)); + } + } + } + public function action_edit() { $this->template = new View_Edit; diff --git a/application/classes/Form.php b/application/classes/Form.php index 1281798..544bca2 100644 --- a/application/classes/Form.php +++ b/application/classes/Form.php @@ -116,6 +116,28 @@ class Form extends Kohana_Form { $template->value = $model->$name; return self::render_inline_control($template); } + public static function textarea_inline($model, $name) + { + $template = new View_Form_Textarea; + $template->_view = 'form/inline/textarea'; + $template->name = $name; + $template->label = __($model->get_label($name)); + $template->value = $model->$name; + return self::render_inline_control($template); + } + + /** + * New textarea. + * Does not support $double_encode = FALSE. + **/ + public static function textarea($name, $value = NULL, array $attributes = NULL, $double_encode = TRUE) + { + $template = new View_Form_Textarea; + $template->name = $name; + $template->label = __(Arr::get($attributes, 'label')); + $template->value = $value; + return self::render_control($template); + } /** * A textarea with a HTML visual editor diff --git a/application/classes/View/Course/Simple.php b/application/classes/View/Course/Simple.php new file mode 100644 index 0000000..6b420cd --- /dev/null +++ b/application/classes/View/Course/Simple.php @@ -0,0 +1,28 @@ + __('New course'), + 'controls' => array( + Form::textinput('title', '', array('label' => 'Title')), + Form::textarea('description', '', array('label' => 'Description')) + ) + ); + } + + public function controls_letter() + { + return array( + 'heading' => __('First letter'), + 'controls' => array( + Form::textinput('letter_subject', '', array('label' => 'Subject')), + Form::textarea('letter_body', '', array('label' => 'Message body')) + ) + ); + } +} diff --git a/application/classes/View/Edit.php b/application/classes/View/Edit.php index 3785ccb..36f1f8f 100644 --- a/application/classes/View/Edit.php +++ b/application/classes/View/Edit.php @@ -1,4 +1,4 @@ -controls as $key => $value) + if ($this->model instanceof ORM) { - $output .= Form::orm_input($this->model, $key, $value); + foreach ($this->controls as $key => $value) + { + $output .= Form::orm_input($this->model, $key, $value); + } } foreach ($this->custom_controls as $key => $value) { diff --git a/application/classes/View/Layout.php b/application/classes/View/Layout.php index f4c88f7..709be54 100644 --- a/application/classes/View/Layout.php +++ b/application/classes/View/Layout.php @@ -77,6 +77,7 @@ class View_Layout { else { $navigation = array_merge($navigation, array( + __('New course') => 'course/simple', __('Courses') => 'course/index', __('Subscriptions') => 'subscription/index', __('Clients') => 'client/index', diff --git a/application/i18n/ru.php b/application/i18n/ru.php index a6a840e..82a3313 100644 --- a/application/i18n/ru.php +++ b/application/i18n/ru.php @@ -61,5 +61,8 @@ return array( 'Welcome message' => 'Приветственное сообщение', 'Sent' => 'Отправлено', 'Send instant to all subscribers' => 'Отослать мгновенное всем подписчикам', - 'The instant has been sent.' => 'Мгновенное было выслано.' + 'The instant has been sent.' => 'Мгновенное было выслано.', + 'First letter' => 'Первое письмо', + 'Subject' => 'Тема', + 'Message body' => 'Тело письма' ); diff --git a/application/templates/course/simple.mustache b/application/templates/course/simple.mustache new file mode 100644 index 0000000..cfbb7e9 --- /dev/null +++ b/application/templates/course/simple.mustache @@ -0,0 +1,29 @@ +
+{{#has_errors}} +

При проверке формы были найдены ошибки:

+ +{{/has_errors}} +
+
+ {{#controls_course}} +

{{heading}}

+ {{#controls}} + {{{.}}} + {{/controls}} + {{/controls_course}} +
+
+ {{#controls_letter}} +

{{heading}}

+ {{#controls}} + {{{.}}} + {{/controls}} + {{/controls_letter}} +
+
+{{{get_controls}}} +