Exemplo n.º 1
0
Arquivo: user.php Projeto: 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));
 }
Exemplo n.º 2
0
 /**
  * Action: image
  */
 public function action_image()
 {
     $this->history = false;
     $user = $this->_get_user();
     Permission::required($user, Model_User::PERMISSION_UPDATE, self::$user);
     if (!$this->ajax) {
         $this->_set_page($user);
     }
     // Change existing
     if (isset($_REQUEST['default'])) {
         /** @var  Model_Image  $image */
         $image = Jelly::select('image')->load((int) $_REQUEST['default']);
         if (Security::csrf_valid() && $image->loaded() && $user->has('images', $image)) {
             $user->default_image = $image;
             $user->picture = $image->get_url();
             $user->save();
         }
         $cancel = true;
     }
     // Delete existing
     if (isset($_REQUEST['delete'])) {
         /** @var  Model_Image  $image */
         $image = Jelly::select('image')->load((int) $_REQUEST['delete']);
         if (Security::csrf_valid() && $image->loaded() && $image->id != $user->default_image->id && $user->has('images', $image)) {
             $user->remove('images', $image);
             $user->picture = null;
             $user->save();
             $image->delete();
         }
         $cancel = true;
     }
     // Cancel change
     if (isset($cancel) || isset($_REQUEST['cancel'])) {
         if ($this->ajax) {
             echo $this->_get_mod_image($user);
             return;
         }
         $this->request->redirect(URL::user($user));
     }
     $image = Jelly::factory('image')->set(array('author' => $user));
     // Handle post
     $errors = array();
     if ($_POST && $_FILES && Security::csrf_valid()) {
         $image->file = Arr::get($_FILES, 'file');
         try {
             $image->save();
             // Add exif, silently continue if failed - not critical
             try {
                 Jelly::factory('image_exif')->set(array('image' => $image))->save();
             } catch (Kohana_Exception $e) {
             }
             // Set the image as user image
             $user->add('images', $image);
             $user->default_image = $image;
             $user->picture = $image->get_url();
             // @TODO: Legacy, will be removed after migration
             $user->save();
             // Newsfeed
             NewsfeedItem_User::default_image($user, $image);
             if ($this->ajax) {
                 echo $this->_get_mod_image($user);
                 return;
             }
             $this->request->redirect(URL::user($user));
         } catch (Validate_Exception $e) {
             $errors = $e->array->errors('validation');
         } catch (Kohana_Exception $e) {
             $errors = array('file' => __('Failed with image'));
         }
     }
     // Build form
     $form = array('ajaxify' => $this->ajax, 'values' => $image, 'errors' => $errors, 'attributes' => array('enctype' => 'multipart/form-data'), 'cancel' => $this->ajax ? URL::user($user, 'image') . '?cancel' : URL::user($user), 'groups' => array(array('fields' => array('file' => array()))));
     $view = View_Module::factory('form/anqh', array('mod_title' => __('Add image'), 'form' => $form));
     if ($this->ajax) {
         echo $view;
         return;
     }
     Widget::add('main', $view);
 }