Beispiel #1
0
 /**
  * Action: add note
  */
 public function action_note()
 {
     $this->history = false;
     /** @var  Model_Gallery  $gallery */
     $gallery_id = (int) $this->request->param('gallery_id');
     $gallery = new Model_Gallery($gallery_id);
     if (!$gallery->loaded()) {
         throw new Model_Exception($gallery, $gallery_id);
     }
     /** @var  Model_Image $image */
     $image_id = $this->request->param('id');
     $image = new Model_Image($image_id);
     if (!$image->loaded()) {
         throw new Model_Exception($image, $image_id);
     }
     // Permission check
     Permission::required($image, Model_Image::PERMISSION_NOTE, self::$user);
     // Create note
     if (isset($_POST['name']) && trim($_POST['name'] != '')) {
         // Get note user
         $username = trim($_POST['name']);
         $user = Model_User::find_user($username);
         if (!$user && ($user_id = Arr::get($_POST, 'user_id'))) {
             $user = Model_User::find_user($user_id);
         }
         try {
             $position = Arr::intersect($_POST, array('x', 'y', 'width', 'height'), true);
             $image->add_note(self::$user->id, count($position) == 4 ? $position : null, $user ? $user : $username);
             // Newsfeed
             if ($user) {
                 NewsfeedItem_Galleries::note(self::$user, $gallery, $image, $user);
             }
         } catch (Validation_Exception $e) {
         }
     }
     // Redirect back to image
     // @todo: ajaxify for more graceful approach
     $this->request->redirect(Route::get('gallery_image')->uri(array('gallery_id' => Route::model_id($gallery), 'id' => $image->id, 'action' => '')));
 }
Beispiel #2
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);
     // 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 = array();
             $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' => Visitor::$user->id, 'file' => $file, '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++;
                 if (!$gallery->default_image_id) {
                     $gallery->default_image_id = $image->id;
                 }
                 $gallery->updated = time();
                 $gallery->save();
                 // Newsfeed item
                 NewsfeedItem_Galleries::upload(Visitor::$user, $gallery);
                 // 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 (!Visitor::$user->has_role('photo')) {
                     Visitor::$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));
                     $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->headers('Content-Type', 'application/json');
                     $this->response->body(json_encode($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->status(400);
                 $this->response->headers('Content-Type', 'application/json');
                 $this->response->body(json_encode($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_CENTER, $this->section_upload());
     // Help
     //		$this->view->add(View_Page::COLUMN_RIGHT, $this->section_upload_help());
 }