/** * Edit entry * * @param integer|string $entry_id */ public function _entry_edit($entry_id = false) { $this->history = false; $entry = new Blog_Entry_Model((int) $entry_id); // For authenticated users only if (!$this->user || !$entry->is_author() && !$this->visitor->logged_in('admin')) { url::redirect(empty($_SESSION['history']) ? '/blogs' : $_SESSION['history']); } $errors = $form_errors = array(); $form_messages = ''; $form_values = $entry->as_array(); /***** CHECK POST *****/ if (request::method() == 'post') { $post = $this->input->post(); // update $editing = (bool) $entry->id; if ($editing) { $extra['modified'] = date::unix2sql(time()); $extra['modifies'] = (int) $entry->modifies + 1; } else { $extra['author_id'] = $this->user->id; } if ($entry->validate($post, true, $extra)) { // News feed event if (!$editing) { newsfeeditem_blog::entry($this->user, $entry); } url::redirect(url::model($entry)); } else { $form_errors = $post->errors(); $form_messages = $post->message(); } $form_values = arr::overwrite($form_values, $post->as_array()); } /***** /CHECK POST *****/ /***** SHOW FORM *****/ if ($entry->id) { $this->page_actions[] = array('link' => url::model($entry) . '/delete?token=' . csrf::token($this->user->id), 'text' => __('Delete entry'), 'class' => 'entry-delete'); $this->page_title = text::title($entry->name); $this->page_subtitle = __('Edit entry'); } else { $this->page_title = __('New entry'); } $form = $entry->get_form(); if (empty($errors)) { widget::add('head', html::script(array('js/jquery.markitup.pack.js', 'js/markitup.bbcode.js'))); widget::add('main', View::factory('blog/entry_edit', array('form' => $form, 'values' => $form_values, 'errors' => $form_errors, 'messages' => $form_messages))); } else { $this->_error(Kohana::lang('generic.error'), $errors); } /***** /SHOW FORM *****/ }
/** * Edit topic * * @param mixed $topic_id * @param mixed $area_id */ public function _topic_edit($topic_id, $area_id = false) { $this->history = false; $errors = array(); $forum_topic = new Forum_Topic_Model((int) $topic_id); $forum_area = $forum_topic->loaded() ? $forum_topic->forum_area : new Forum_Area_Model((int) $area_id); if ($forum_topic->loaded()) { // Editing topic $editing = true; if (!$forum_topic->has_access(Forum_Topic_Model::ACCESS_EDIT)) { url::back('forum'); } } else { if ($forum_area->loaded()) { // New topic $editing = false; if (!$forum_area->has_access(Forum_Area_Model::ACCESS_WRITE)) { url::back('forum'); } } else { // New topic in unknown area $errors[] = __('Area :area or topic :topic not found', array(':area' => (int) $area_id, ':topic' => (int) $topic_id)); } } if (empty($errors)) { $forum_post = new Forum_Post_Model((int) $forum_topic->first_post_id); $form_errors = array(); $form_values_topic = $forum_topic->as_array(); $form_values_post = $forum_post->as_array(); $form_topics = false; // Bound area? if ($forum_area->is_type(Forum_Area_Model::TYPE_BIND)) { // Get bind config and load topics $bind = Forum_Area_Model::binds($forum_area->bind); if ($editing) { // Can't edit bound topic $form_topics = array($forum_topic->bind_id => $forum_topic->name); } else { // Try to load options from configured model try { $bind_topics = ORM::factory($bind['model'])->find_bind_topics($forum_area->bind); $form_topics = array(0 => __('Choose..')) + $bind_topics; } catch (Kohana_Exception $e) { $form_topics = array(); } } } // Admin actions if ($editing && $forum_topic->has_access(Forum_Topic_Model::ACCESS_DELETE)) { $this->page_actions[] = array('link' => url::model($forum_topic) . '/delete/?token=' . csrf::token(), 'text' => __('Delete topic'), 'class' => 'topic-delete'); } // Check post if ($post = $this->input->post()) { $post['forum_area_id'] = $forum_area->id; $topic = $post; if (isset($bind_topics)) { $topic['name'] = arr::get($bind_topics, (int) $topic['bind_id'], ''); } $post_extra = $topic_extra = array('author_id' => $this->user->id, 'author_name' => $this->user->username); if ($editing) { $post_extra['modifies'] = (int) $forum_post->modifies + 1; $post_extra['modified'] = date::unix2sql(time()); } $post_extra['author_ip'] = $this->input->ip_address(); $post_extra['author_host'] = $this->input->host_name(); // validate post first and save topic if ok if (csrf::valid() && $forum_post->validate($post, false, $post_extra) && $forum_topic->validate($topic, true, $topic_extra)) { // post $forum_post->forum_topic_id = $forum_topic->id; $forum_post->save(); if (!$editing) { // topic $forum_topic->first_post_id = $forum_post->id; $forum_topic->last_post_id = $forum_post->id; $forum_topic->last_poster = $this->user->username; $forum_topic->last_posted = date::unix2sql(time()); $forum_topic->posts = 1; $forum_topic->save(); // area $forum_area->last_topic_id = $forum_topic->id; $forum_area->posts += 1; $forum_area->topics += 1; $forum_area->save(); // user $this->user->posts += 1; $this->user->save(); // News feed newsfeeditem_forum::topic($this->user, $forum_topic); } // redirect back to topic URL::redirect(url::model($forum_topic)); } else { $form_errors = array_merge($post->errors(), is_object($topic) ? $topic->errors() : array()); } $form_values_topic = arr::overwrite($form_values_topic, is_object($topic) ? $topic->as_array() : $topic); $form_values_post = arr::overwrite($form_values_post, $post->as_array()); } } // Show form if (empty($errors)) { $this->breadcrumb[] = html::anchor(url::model($forum_area), text::title($forum_area->name)); $this->page_title = $editing ? text::title($forum_topic->name) : __('New topic'); $this->page_subtitle = __('Area :area', array(':area' => html::anchor(url::model($forum_area), text::title($forum_area->name), array('title' => strip_tags($forum_area->description))))); widget::add('head', html::script(array('js/jquery.markitup.pack', 'js/markitup.bbcode'))); widget::add('main', View_Mod::factory('forum/topic_edit', array('topic' => $form_values_topic, 'topics' => $form_topics, 'post' => $form_values_post, 'errors' => $form_errors))); } else { $this->_error(__('Error'), $errors); } $this->_side_views(); }
/** * Edit event * * @param int|string $event_id */ public function _event_edit($event_id = false) { $this->history = false; $event = new Event_Model((int) $event_id); // For authenticated users only if (!$this->user || !$event->is_author() && !$this->visitor->logged_in(array('admin', 'event moderator'))) { url::back('/events'); } $errors = $form_errors = array(); $form_values = $event->as_array(); $form_values['start_date'] = ''; $form_values['start_hour'] = ''; $form_values['end_hour'] = ''; /***** CHECK POST *****/ if (request::method() == 'post') { $post = array_merge($this->input->post(), $_FILES); $extra = array('start_time' => date::unix2sql(strtotime($post['start_date'] . ' ' . date::time_24h($post['start_hour'])))); if (!empty($post['end_hour'])) { $end_time = strtotime($post['start_date']); // end hour is earlier than start hour = event ends the next day if ($post['end_hour'] < $post['start_hour']) { $end_time = strtotime('+1 day', $end_time); } $extra['end_time'] = date('Y-m-d', $end_time) . ' ' . date::time_24h($post['end_hour']) . ':00'; } // update $editing = (bool) $event->id; if ($editing) { $extra['modified'] = date::unix2sql(time()); $extra['modifies'] = (int) $event->modifies + 1; } else { $extra['author_id'] = $this->user->id; } $city = ORM::factory('city', $post['city_id']); if ($city) { $extra['country_id'] = $city->country_id; } if (csrf::valid() && $event->validate($post, true, $extra)) { // Update tags $event->remove(ORM::factory('tag')); if (!empty($post['tags'])) { foreach ($post['tags'] as $tag_id => $tag) { $event->add(ORM::factory('tag', $tag_id)); } $event->save(); } // Handle flyer uploads foreach (array('flyer_front_image_id' => $post->flyer_front, 'flyer_back_image_id' => $post->flyer_back) as $image_id => $flyer) { if (isset($flyer) && empty($flyer['error'])) { $image = Image_Model::factory('events.flyer', $flyer, $this->user->id); if ($image->id) { $event->add($image); $event->{$image_id} = $image->id; $event->save(); } } } if (!$editing) { newsfeeditem_events::event($this->user, $event); } url::redirect(url::model($event)); } else { $form_errors = $post->errors(); } $form_values = arr::overwrite($form_values, $post->as_array()); } /***** /CHECK POST *****/ /***** SHOW FORM *****/ if ($event->id) { $this->page_actions[] = array('link' => url::model($event) . '/delete/?token=' . csrf::token(), 'text' => __('Delete event'), 'class' => 'event-delete'); $this->page_title = text::title($event->name); $this->page_subtitle = __('Edit event'); list($form_values['start_date'], $form_values['start_hour']) = explode(' ', date('Y-m-d H', strtotime($event->start_time))); if (!empty($event->end_time)) { list($temp, $form_values['end_hour']) = explode(' ', date('Y-m-d H', strtotime($event->end_time))); } } else { $this->page_title = __('New event'); } $form = $event->get_form(); // Tags if ($tag_group = Kohana::config('events.tag_group')) { $form['tags'] = $form_values['tags'] = array(); $tags = ORM::factory('tag_group', $tag_group); foreach ($tags->tags as $tag) { $form['tags'][$tag->id] = $tag->name; if ($event->has($tag)) { $form_values['tags'][$tag->id] = $tag->name; } } } // City autocomplete $this->_autocomplete_city('city_name'); // Venue autocomplete $venues = ORM::factory('venue')->where('event_host', '=', 1)->find_all(); $hosts = array(); foreach ($venues as $venue) { $hosts[] = "{ id: '" . $venue->id . "', text: '" . html::chars($venue->name) . "' }"; } widget::add('foot', html::script_source('var venues = [' . implode(', ', $hosts) . "];\n\$('input[name=venue_name]').autocomplete(venues, {\n\tformatItem: function(item) {\n\t\treturn item.text;\n\t}\n}).result(function(event, item) {\n\t\$('input[name=venue_id]').val(item.id);\n});")); // Date pickers widget::add('foot', html::script_source("\$('input#start_date').datepicker({ dateFormat: 'd.m.yy', firstDay: 1, changeFirstDay: false, showOtherMonths: true, showWeeks: true, showStatus: true, showOn: 'both' });")); if (empty($errors)) { widget::add('main', View::factory('events/event_edit', array('form' => $form, 'values' => $form_values, 'errors' => $form_errors))); } else { // $this->_error(Kohana::lang('generic.error'), $errors); } /***** /SHOW FORM *****/ $this->_side_views(); }