示例#1
0
 /**
  * Action: upload
  */
 public function action_upload()
 {
     // Load existing gallery if any
     $gallery_id = (int) $this->request->param('gallery_id');
     if (!$gallery_id) {
         $gallery_id = (int) $this->request->param('id');
     }
     if ($gallery_id) {
         // Existing gallery
         $gallery = Model_Gallery::factory($gallery_id);
         if (!$gallery->loaded()) {
             throw new Model_Exception($gallery, $gallery_id);
         }
     } else {
         // New gallery
         return $this->_edit_gallery(null, Arr::get($_REQUEST, 'event'));
     }
     Permission::required(new Model_Gallery(), Model_Gallery::PERMISSION_UPLOAD, self::$user);
     // Handle post
     $errors = array();
     if ($_FILES) {
         $file = Arr::get($_FILES, 'file');
         if ($file) {
             // We need to flatten our file one level as ajax uploaded files are set up funnily.
             // Support for ajax uploads one by one for now..
             foreach ($file as $key => $value) {
                 is_array($value) and $file[$key] = $value[0];
             }
             // Needed for IE response
             if ($multiple = Arr::get($_REQUEST, 'multiple', false)) {
                 $this->auto_render = false;
             }
             // Upload info for JSON
             $info = new stdClass();
             $info->name = HTML::chars($file['name']);
             $info->size = intval($file['size']);
             // Save image
             try {
                 // Make sure we don't timeout. An external queue would be better thuough.
                 set_time_limit(0);
                 ignore_user_abort(true);
                 // Duplicate filename check
                 $uploaded = Session::instance()->get('uploaded', array());
                 if (isset($uploaded[$gallery->id]) && in_array($file['name'], $uploaded[$gallery->id])) {
                     throw new Kohana_Exception(__('Already uploaded'));
                 }
                 $image = Model_Image::factory();
                 $image->normal = 'wide';
                 $image->set_fields(array('author_id' => self::$user->id, 'file' => $file, 'status' => Model_Image::NOT_ACCEPTED, 'created' => time()));
                 $image->save();
                 // Save exif
                 try {
                     $exif = Model_Image_Exif::factory();
                     $exif->image_id = $image->id;
                     $exif->save();
                 } catch (Kohana_Exception $e) {
                     throw $e;
                 }
                 // Set the image as gallery image
                 $gallery->relate('images', array($image->id));
                 $gallery->image_count++;
                 $gallery->save();
                 // Mark filename as uploaded for current gallery
                 $uploaded[$gallery->id][] = $file['name'];
                 Session::instance()->set('uploaded', $uploaded);
                 // Make sure the user has photo role to be able to see uploaded pictures
                 if (!self::$user->has_role('photo')) {
                     self::$user->add_role('photo');
                 }
                 // Show image if uploaded with ajax
                 if ($this->ajax || $multiple) {
                     $info->url = $image->get_url();
                     $info->thumbnail_url = $image->get_url(Model_Image::SIZE_THUMBNAIL);
                     $info->gallery_url = Route::url('gallery_image', array('gallery_id' => Route::model_id($gallery), 'id' => $image->id, 'action' => 'approve'));
                     $info->delete_url = Route::url('gallery_image', array('gallery_id' => Route::model_id($gallery), 'id' => $image->id, 'action' => 'delete')) . '?token=' . Security::csrf();
                     $info->delete_type = 'GET';
                     $this->response->body(json_encode(array($info)));
                     return;
                 }
                 $this->request->redirect(Route::model($gallery));
             } catch (Validation_Exception $e) {
                 $errors = $e->array->errors('validation');
             } catch (Kohana_Exception $e) {
                 $errors = array('file' => $e->getMessage());
             }
             // Show errors if uploading with ajax, skip form
             if (($this->ajax || $multiple) && !empty($errors)) {
                 $info->error = Arr::get($errors, 'file');
                 $this->response->body(json_encode(array($info)));
                 return;
             }
         }
     }
     // Build page
     $this->view = View_Page::factory($gallery->name);
     $images = count($gallery->images());
     $this->view->subtitle = __($images == 1 ? ':images image' : ':images images', array(':images' => $images)) . ' - ' . HTML::time(Date::format('DMYYYY', $gallery->date), $gallery->date, true);
     // Upload
     $this->view->add(View_Page::COLUMN_MAIN, $this->section_upload());
     // Help
     $this->view->add(View_Page::COLUMN_SIDE, $this->section_upload_help());
 }
示例#2
0
文件: events.php 项目: anqh/events
 /**
  * Action: image
  */
 public function action_image()
 {
     $this->history = false;
     // Load event
     /** @var  Model_Event  $event */
     $event_id = (int) $this->request->param('id');
     $event = Model_Event::factory($event_id);
     if (!$event->loaded()) {
         throw new Model_Exception($event, $event_id);
     }
     Permission::required($event, Model_Event::PERMISSION_UPDATE, self::$user);
     if ($this->_request_type !== Controller::REQUEST_AJAX) {
         $this->page_title = HTML::chars($event->name);
     }
     if (isset($_REQUEST['front'])) {
         // Change front flyer
         /** @var  Model_Flyer  $flyer */
         $flyer = Model_Flyer::factory()->find_by_image((int) $_REQUEST['front']);
         if (Security::csrf_valid() && $flyer->loaded() && $flyer->event_id == $event->id) {
             $event->flyer_front_image_id = $flyer->image_id;
             $event->flyer_front_url = $flyer->image()->get_url();
             $event->save();
         }
         $cancel = true;
     } else {
         if (isset($_REQUEST['back'])) {
             // Change back flyer
             /** @var  Model_Flyer  $flyer */
             $flyer = Model_Flyer::factory()->find_by_image((int) $_REQUEST['back']);
             if (Security::csrf_valid() && $flyer->loaded() && $flyer->event_id == $event->id) {
                 $event->flyer_back_image_id = $flyer->image_id;
                 $event->flyer_back_url = $flyer->image()->get_url();
                 $event->save();
             }
             $cancel = true;
         } else {
             if (isset($_REQUEST['delete'])) {
                 // Delete existing
                 /** @var  Model_Flyer  $flyer */
                 $flyer = Model_Flyer::factory()->find_by_image((int) $_REQUEST['delete']);
                 if (Security::csrf_valid() && $flyer->loaded() && $flyer->event_id == $event->id) {
                     if ($flyer->image_id == $event->flyer_front_image_id) {
                         $event->flyer_front_image_id = null;
                         $event->flyer_front_url = null;
                     } else {
                         if ($flyer->image_id == $event->flyer_back_image_id->id) {
                             $event->flyer_back_image_id = null;
                             $event->flyer_back_url = null;
                         }
                     }
                     $event->save();
                     $flyer->delete();
                 }
                 $cancel = true;
             }
         }
     }
     // Cancel change
     if (isset($cancel) || isset($_REQUEST['cancel'])) {
         if ($this->_request_type === Controller::REQUEST_AJAX) {
             $this->response->body($this->section_carousel($event));
             return;
         }
         $this->request->redirect(Route::model($event));
     }
     // Handle post
     $errors = array();
     if ($_POST && $_FILES) {
         $image = Model_Image::factory();
         $image->author_id = self::$user->id;
         $image->created = time();
         $image->file = Arr::get($_FILES, 'file');
         $image->description = $event->get_forum_topic();
         try {
             $image->save();
             // Add exif, silently continue if failed - not critical
             try {
                 $exif = Model_Image_Exif::factory();
                 $exif->image_id = $image->id;
                 $exif->save();
             } catch (Kohana_Exception $e) {
             }
             // Add flyer
             try {
                 $flyer = Model_Flyer::factory();
                 $flyer->set_fields(array('image_id' => $image->id, 'event_id' => $event->id, 'name' => $event->name, 'stamp_begin' => $event->stamp_begin));
                 $flyer->save();
             } catch (Kohana_Exception $e) {
                 //					$event->add('flyers', $image);
             }
             if ($event->flyer_front_image_id) {
                 if (!$event->flyer_back_image_id) {
                     // Back flyer not set, set it
                     $event->flyer_back_image_id = $image->id;
                     $event->flyer_back_url = $image->get_url();
                 }
             } else {
                 // Front flyer not set, set it
                 $event->flyer_front_image_id = $image->id;
                 $event->flyer_front_url = $image->get_url();
             }
             $event->save();
             //				NewsfeedItem_Events::event_edit(self::$user, $event);
             if ($this->_request_type === Controller::REQUEST_AJAX) {
                 $this->response->body($this->section_carousel($event));
                 return;
             }
             $this->request->redirect(Route::model($event));
         } catch (Validation_Exception $e) {
             $errors = $e->array->errors('validation');
         } catch (Kohana_Exception $e) {
             $errors = array('file' => __('Failed with image'));
         }
     }
     $view = $this->section_flyer_upload(Route::model($event, 'image'), $this->_request_type === Controller::REQUEST_AJAX ? Route::model($event, 'image') . '?cancel' : Route::model($event), $errors);
     if ($this->_request_type === Controller::REQUEST_AJAX) {
         $this->response->body($view);
         return;
     }
     // Build page
     $this->view = View_Page::factory($event->name);
     $this->view->add(View_Page::COLUMN_MAIN, $view);
 }
示例#3
0
文件: user.php 项目: anqh/core
 /**
  * Action: image
  */
 public function action_image()
 {
     $this->history = false;
     $user = $this->_get_user();
     Permission::required($user, Model_User::PERMISSION_UPDATE, self::$user);
     // Change default image
     if ($image_id = (int) Arr::get($_REQUEST, 'default')) {
         /** @var  Model_Image  $image */
         $image = Model_Image::factory($image_id);
         if (Security::csrf_valid() && $image->loaded() && $user->has('images', $image->id)) {
             $user->default_image_id = $image->id;
             $user->picture = $image->get_url();
             $user->save();
         }
         $cancel = true;
     }
     // Delete existing
     if ($image_id = (int) Arr::get($_REQUEST, 'delete')) {
         /** @var  Model_Image  $image */
         $image = Model_Image::factory($image_id);
         if (Security::csrf_valid() && $image->loaded() && $user->has('images', $image->id)) {
             $user->remove('image', $image->id);
             if ($image->id === $user->default_image_id) {
                 $user->default_image_id = null;
                 $user->picture = null;
             }
             $user->save();
             $image->delete();
         }
         $cancel = true;
     }
     // Cancel change
     if (isset($cancel) || isset($_REQUEST['cancel'])) {
         $this->request->redirect(URL::user($user));
     }
     $image = Model_Image::factory();
     $image->author_id = $user->id;
     $image->created = time();
     // Handle post
     $errors = array();
     if ($_POST && $_FILES) {
         $image->file = Arr::get($_FILES, 'file');
         try {
             $image->save();
             // Add exif, silently continue if failed - not critical
             try {
                 $exif = Model_Image_Exif::factory();
                 $exif->image_id = $image->id;
                 $exif->save();
             } catch (Kohana_Exception $e) {
             }
             // Set the image as user image
             $user->relate('images', array($image->id));
             $user->default_image_id = $image->id;
             $user->picture = $image->get_url();
             // @TODO: Legacy, will be removed after migration
             $user->save();
             // Newsfeed
             NewsfeedItem_User::default_image($user, $image);
             $this->request->redirect(URL::user($user));
         } catch (Validation_Exception $e) {
             $errors = $e->array->errors('validation');
         } catch (Kohana_Exception $e) {
             $errors = array('file' => __('Failed with image'));
         }
     }
     // Build page
     $this->_set_page($user);
     $this->view->tab = 'profile';
     $this->view->add(View_Page::COLUMN_MAIN, $this->section_upload(URL::user($user), $errors));
 }
示例#4
0
文件: venues.php 项目: anqh/anqh
 /**
  * Action: image
  */
 public function action_image()
 {
     $this->history = false;
     // Load venue
     $venue_id = (int) $this->request->param('id');
     $venue = Model_Venue::factory($venue_id);
     if (!$venue->loaded()) {
         throw new Model_Exception($venue, $venue_id);
     }
     Permission::required($venue, Model_Venue::PERMISSION_UPDATE);
     // Change existing
     if ($image_id = (int) Arr::get($_REQUEST, 'default')) {
         $image = Model_Image::factory($image_id);
         if (Security::csrf_valid() && $image->loaded() && $venue->has('images', $image->id)) {
             $venue->default_image_id = $image->id;
             $venue->save();
         }
         $cancel = true;
     }
     // Delete existing
     if ($image_id = (int) Arr::get($_REQUEST, 'delete')) {
         $image = Model_Image::factory($image_id);
         if (Security::csrf_valid() && $image->loaded() && $venue->has('images', $image->id)) {
             if ($venue->default_image_id == $image->id) {
                 $venue->default_image_id = null;
             }
             $venue->remove('image', $image->id);
             $venue->save();
             $image->delete();
         }
         $cancel = true;
     }
     // Cancel change
     if (isset($cancel) || isset($_REQUEST['cancel'])) {
         if ($this->_request_type === Controller::REQUEST_AJAX) {
             $this->response->body($this->section_venue_image($venue));
             return;
         }
         $this->request->redirect(Route::model($venue));
     }
     // Handle post
     $errors = array();
     if ($_POST && $_FILES && Security::csrf_valid()) {
         $image = new Model_Image();
         $image->author_id = Visitor::$user->id;
         $image->file = Arr::get($_FILES, 'file');
         try {
             $image->save();
             // Add exif, silently continue if failed - not critical
             try {
                 $exif = new Model_Image_Exif();
                 $exif->image_id = $image->id;
                 $exif->save();
             } catch (Kohana_Exception $e) {
             }
             // Set the image as venue image
             $venue->relate('images', array($image->id));
             $venue->default_image_id = $image->id;
             $venue->save();
             if ($this->_request_type === Controller::REQUEST_AJAX) {
                 $this->response->body($this->section_venue_image($venue));
                 return;
             }
             $this->request->redirect(Route::model($venue));
         } catch (Validation_Exception $e) {
             $errors = $e->array->errors('validation');
         } catch (Kohana_Exception $e) {
             $errors = array('file' => __('Failed with image'));
         }
     }
     $view = $this->section_image_upload($this->_request_type === Controller::REQUEST_AJAX ? Route::model($venue, 'image') . '?cancel' : Route::model($venue), $errors);
     if ($this->_request_type === Controller::REQUEST_AJAX) {
         $this->response->body($view);
         return;
     }
     $this->view = new View_Page($venue->name);
     $this->view->add(View_Page::COLUMN_CENTER, $view);
 }