Ejemplo n.º 1
0
 public function updateAction()
 {
     // Get info from url
     $module = $this->params('module');
     $id = $this->params('id');
     $item = $this->params('item');
     // Get config
     $config = Pi::service('registry')->config->read($module);
     // Set option
     $option = array('side' => 'front', 'use_news_topic' => $config['use_news_topic'], 'use_guide_category' => $config['use_guide_category'], 'use_guide_location' => $config['use_guide_location'], 'order_active' => $config['order_active']);
     // check
     if (!$config['manage_active']) {
         $this->getResponse()->setStatusCode(401);
         $this->terminate(__('Owner dashboard is inactive'), '', 'error-denied');
         $this->view()->setLayout('layout-simple');
         return;
     } else {
         Pi::service('authentication')->requireLogin();
     }
     // Get user
     $uid = Pi::user()->getId();
     // Find event
     if ($id) {
         $event = Pi::api('event', 'event')->getEventSingle($id, 'id', 'full');
         if ($event['image']) {
             $option['thumbUrl'] = $event['thumbUrl'];
             $option['removeUrl'] = $this->url('', array('action' => 'remove', 'id' => $event['id']));
         }
     }
     // Check event uid
     if (isset($event['uid']) && $event['uid'] != $uid) {
         $this->getResponse()->setStatusCode(401);
         $this->terminate(__('Its not your event'), '', 'error-denied');
         $this->view()->setLayout('layout-simple');
         return;
     }
     // Set title
     $title = __('Add event');
     // Check event guide owner
     if (Pi::service('module')->isActive('guide')) {
         $owner = $this->canonizeGuideOwner();
         $option['owner'] = $owner['id'];
         if (isset($event['guide_owner']) && $event['guide_owner'] != $owner['id']) {
             $this->getResponse()->setStatusCode(401);
             $this->terminate(__('Its not your event'), '', 'error-denied');
             $this->view()->setLayout('layout-simple');
             return;
         }
         // Check item
         $item = Pi::api('item', 'guide')->getItemLight($item);
         $option['item'] = $item['id'];
         $title = sprintf(__('Add event to %s'), $item['title']);
     }
     // Set form
     $form = new EventForm('event', $option);
     $form->setAttribute('enctype', 'multipart/form-data');
     if ($this->request->isPost()) {
         $data = $this->request->getPost();
         $file = $this->request->getFiles();
         // Set slug
         $slug = $data['slug'] ? $data['slug'] : $data['title'];
         $filter = new Filter\Slug();
         $data['slug'] = $filter($slug);
         // Form filter
         $form->setInputFilter(new EventFilter($option));
         $form->setData($data);
         if ($form->isValid()) {
             $values = $form->getData();
             // upload image
             $image = Pi::api('api', 'news')->uploadImage($file, 'event-', 'event/image');
             $values = array_merge($values, $image);
             if (!isset($values['image'])) {
                 $values['image'] = '';
             }
             // Set time
             $values['time_publish'] = $values['time_end'] ? strtotime($values['time_end']) : strtotime($values['time_start']);
             $values['time_start'] = strtotime($values['time_start']);
             $values['time_end'] = $values['time_end'] ? strtotime($values['time_end']) : '';
             // Set type
             $values['type'] = 'event';
             // Set status
             $values['status'] = $config['manage_approval'] ? 1 : 2;
             // Set guide module info
             if (isset($owner) && isset($owner['id'])) {
                 $values['guide_owner'] = $owner['id'];
             }
             $values['guide_category'] = Json::encode($values['guide_category']);
             $values['guide_location'] = Json::encode($values['guide_location']);
             $values['guide_item'] = !empty($item) ? Json::encode(array($item['id'])) : Json::encode($values['guide_item']);
             // Save values on news story table and event extra table
             if (!empty($values['id'])) {
                 $story = Pi::api('api', 'news')->editStory($values);
                 if (isset($story) && !empty($story)) {
                     $row = $this->getModel('extra')->find($story['id']);
                 } else {
                     $message = __('Error on save story data on news module.');
                     $this->jump(array('action' => 'index'), $message, 'error');
                 }
             } else {
                 $values['uid'] = Pi::user()->getId();
                 $story = Pi::api('api', 'news')->addStory($values);
                 if (isset($story) && !empty($story)) {
                     $row = $this->getModel('extra')->createRow();
                     $values['id'] = $story['id'];
                 } else {
                     $message = __('Error on save story data on news module.');
                     $this->jump(array('action' => 'index'), $message, 'error');
                 }
             }
             $row->assign($values);
             $row->save();
             // Check topic
             if (!$config['use_news_topic']) {
                 $values['topic'] = array();
             }
             // Set link array
             $link = array('story' => $story['id'], 'time_publish' => $story['time_publish'], 'time_update' => $story['time_update'], 'status' => $story['status'], 'uid' => $story['uid'], 'type' => $story['type'], 'module' => array('event' => array('name' => 'event', 'controller' => array('topic' => array('name' => 'topic', 'topic' => $values['topic'])))));
             // Add guide module info on link
             if (Pi::service('module')->isActive('guide')) {
                 $link['module']['guide'] = array('name' => 'guide', 'controller' => array());
                 if ($config['use_guide_category'] && isset($values['guide_category']) && !empty($values['guide_category'])) {
                     $link['module']['guide']['controller']['category'] = array('name' => 'category', 'topic' => Json::decode($values['guide_category'], true));
                 }
                 if ($config['use_guide_location'] && isset($values['guide_location']) && !empty($values['guide_location'])) {
                     $link['module']['guide']['controller']['location'] = array('name' => 'location', 'topic' => Json::decode($values['guide_location'], true));
                 }
                 if (isset($values['guide_item']) && !empty($values['guide_item'])) {
                     $link['module']['guide']['controller']['item'] = array('name' => 'item', 'topic' => Json::decode($values['guide_item'], true));
                 }
                 if (isset($values['guide_owner']) && !empty($values['guide_owner'])) {
                     $link['module']['guide']['controller']['owner'] = array('name' => 'owner', 'topic' => array($values['guide_owner']));
                 }
             }
             // Setup link
             Pi::api('api', 'news')->setupLink($link);
             // Add / Edit sitemap
             if (Pi::service('module')->isActive('sitemap')) {
                 // Set loc
                 $loc = Pi::url($this->url('event', array('module' => $module, 'controller' => 'index', 'slug' => $values['slug'])));
                 // Update sitemap
                 Pi::api('sitemap', 'sitemap')->singleLink($loc, $story['status'], $module, 'event', $story['id']);
             }
             // Add log
             if ($row->status == 1) {
                 $message = __('Thanks for contributing ! Event data saved successfully and was published on public side');
             } else {
                 $message = __('Thanks for contributing ! Event data saved successfully and we will be validate it soon');
             }
             $this->jump(array('action' => 'index'), $message);
         }
     } else {
         if ($id) {
             // Set time
             $event['time_start'] = $event['time_start'] ? date('Y-m-d', $event['time_start']) : date('Y-m-d');
             $event['time_end'] = $event['time_end'] ? date('Y-m-d', $event['time_end']) : '';
             $form->setData($event);
         }
     }
     // Set view
     $this->view()->setTemplate('manage-update');
     $this->view()->assign('form', $form);
     $this->view()->assign('title', $title);
     $this->view()->assign('config', $config);
 }
Ejemplo n.º 2
0
 public function updateAction()
 {
     // Get id
     $id = $this->params('id');
     $module = $this->params('module');
     // Get config
     $config = Pi::service('registry')->config->read($module);
     // Set option
     $option = array('side' => 'admin', 'use_news_topic' => $config['use_news_topic'], 'use_guide_category' => $config['use_guide_category'], 'use_guide_location' => $config['use_guide_location'], 'order_active' => $config['order_active'], 'order_discount' => $config['order_discount']);
     // Find event
     if ($id) {
         $event = Pi::api('event', 'event')->getEventSingle($id, 'id', 'full');
         if ($event['image']) {
             $option['thumbUrl'] = $event['thumbUrl'];
             $option['removeUrl'] = $this->url('', array('action' => 'remove', 'id' => $event['id']));
         }
     }
     // Set form
     $form = new EventForm('event', $option);
     $form->setAttribute('enctype', 'multipart/form-data');
     if ($this->request->isPost()) {
         $data = $this->request->getPost();
         $file = $this->request->getFiles();
         // Set slug
         $slug = $data['slug'] ? $data['slug'] : $data['title'];
         $filter = new Filter\Slug();
         $data['slug'] = $filter($slug);
         // Form filter
         $form->setInputFilter(new EventFilter($option));
         $form->setData($data);
         if ($form->isValid()) {
             $values = $form->getData();
             // upload image
             $image = Pi::api('api', 'news')->uploadImage($file, 'event-', 'event/image');
             $values = array_merge($values, $image);
             if (!isset($values['image'])) {
                 $values['image'] = '';
             }
             // Set time
             $values['time_publish'] = $values['time_end'] ? strtotime($values['time_end']) : strtotime($values['time_start']);
             $values['time_start'] = strtotime($values['time_start']);
             $values['time_end'] = $values['time_end'] ? strtotime($values['time_end']) : '';
             // Set type
             $values['type'] = 'event';
             // Set guide module info
             $values['guide_category'] = Json::encode($values['guide_category']);
             $values['guide_location'] = Json::encode($values['guide_location']);
             $values['guide_item'] = Json::encode($values['guide_item']);
             // Set register_discount
             $discount = array();
             if ($config['order_discount']) {
                 // Get role list
                 $roles = Pi::service('registry')->Role->read('front');
                 unset($roles['webmaster']);
                 unset($roles['guest']);
                 foreach ($roles as $name => $role) {
                     $discount[$name] = $values[$name];
                 }
             }
             $values['register_discount'] = Json::encode($discount);
             // Save values on news story table and event extra table
             if (!empty($values['id'])) {
                 $story = Pi::api('api', 'news')->editStory($values);
                 if (isset($story) && !empty($story)) {
                     $row = $this->getModel('extra')->find($story['id']);
                 } else {
                     $message = __('Error on save story data on event module.');
                     $this->jump(array('action' => 'index'), $message, 'error');
                 }
             } else {
                 $values['uid'] = Pi::user()->getId();
                 $story = Pi::api('api', 'news')->addStory($values);
                 if (isset($story) && !empty($story)) {
                     $row = $this->getModel('extra')->createRow();
                     $values['id'] = $story['id'];
                 } else {
                     $message = __('Error on save story data on event module.');
                     $this->jump(array('action' => 'index'), $message, 'error');
                 }
             }
             $row->assign($values);
             $row->save();
             // Check topic
             if (!$config['use_news_topic']) {
                 $values['topic'] = array();
             }
             // Set link array
             $link = array('story' => $story['id'], 'time_publish' => $story['time_publish'], 'time_update' => $story['time_update'], 'status' => $story['status'], 'uid' => $story['uid'], 'type' => $story['type'], 'module' => array('event' => array('name' => 'event', 'controller' => array('topic' => array('name' => 'topic', 'topic' => $values['topic'])))));
             // Add guide module info on link
             if (Pi::service('module')->isActive('guide')) {
                 $link['module']['guide'] = array('name' => 'guide', 'controller' => array());
                 if ($config['use_guide_category'] && isset($values['guide_category']) && !empty($values['guide_category'])) {
                     $link['module']['guide']['controller']['category'] = array('name' => 'category', 'topic' => Json::decode($values['guide_category'], true));
                 }
                 if ($config['use_guide_location'] && isset($values['guide_location']) && !empty($values['guide_location'])) {
                     $link['module']['guide']['controller']['location'] = array('name' => 'location', 'topic' => Json::decode($values['guide_location'], true));
                 }
                 if (isset($values['guide_item']) && !empty($values['guide_item'])) {
                     $link['module']['guide']['controller']['item'] = array('name' => 'item', 'topic' => Json::decode($values['guide_item'], true));
                 }
                 if (isset($values['guide_owner']) && !empty($values['guide_owner'])) {
                     $link['module']['guide']['controller']['owner'] = array('name' => 'owner', 'topic' => array($values['guide_owner']));
                 }
             }
             // Setup link
             Pi::api('api', 'news')->setupLink($link);
             // Add / Edit sitemap
             if (Pi::service('module')->isActive('sitemap')) {
                 // Set loc
                 $loc = Pi::url($this->url('event', array('module' => $module, 'controller' => 'index', 'slug' => $values['slug'])));
                 // Update sitemap
                 Pi::api('sitemap', 'sitemap')->singleLink($loc, $story['status'], $module, 'event', $story['id']);
             }
             // Add log
             $message = __('Event data saved successfully.');
             $this->jump(array('action' => 'index'), $message);
         }
     } else {
         if ($id) {
             // Make discount
             if ($config['order_discount']) {
                 foreach ($event['register_discount'] as $name => $value) {
                     $event[$name] = $value;
                 }
             }
             // Set time
             $event['time_start'] = $event['time_start'] ? date('Y-m-d', $event['time_start']) : date('Y-m-d');
             $event['time_end'] = $event['time_end'] ? date('Y-m-d', $event['time_end']) : '';
             $form->setData($event);
         }
     }
     // Set view
     $this->view()->setTemplate('event-update');
     $this->view()->assign('form', $form);
     $this->view()->assign('title', __('Add event'));
 }