/**
  * create files in db
  */
 protected function store($form, $hash)
 {
     // get files in the temp/hash dir
     $files = FS::files($this->_settings->get('path_temp') . $hash . DIRECTORY_SEPARATOR);
     // set the files in the form
     $form->files($files);
     Event::raise($this, Event::BEFORE_STORE_FORM_PARSE, array('form' => $form));
     if ($form->valid()) {
         // get the hash of the temp dir from qs
         $hash = $this->request->param('id');
         // get the form values
         $values = $form->values();
         // loop through temp files
         for ($i = 0; $i < count($files); $i++) {
             // createmodel
             $model = ORM::factory($this->_settings->get('model'));
             // set group if one is active
             if ($group = $this->_state->get('group', FALSE)) {
                 $model->group_id = $group;
             }
             // undouble filename
             $file = FS::name_unique($files[$i], $this->_settings->get('path_files'));
             // store it
             FS::move($this->_settings->get('path_temp') . $hash . DIRECTORY_SEPARATOR . $files[$i], $this->_settings->get('path_files') . $file);
             // permissions
             FS::permissions($this->_settings->get('path_files') . $file, 0744);
             // set item properties
             $model->created = date('y-m-d H:i:s', time());
             $model->owner_id = Identity::instance()->id;
             $model->editor_id = Identity::instance()->id;
             $model->website_id = $this->_website;
             // set file
             $model->file = $file;
             // set additional info (mostly title)
             foreach ($values as $key => $value) {
                 if (is_array($value) && isset($value[$i])) {
                     $model->{$key} = $value[$i];
                 }
             }
             // call hook
             Event::raise($this, Event::BEFORE_STORE, array('model' => $model, 'form' => $form));
             //save
             $model->save();
             // call hook
             Event::raise($this, Event::AFTER_STORE, array('model' => $model, 'form' => $form));
         }
         // return true
         return TRUE;
     } else {
         // create viewer
         $viewer = Viewer::factory('Form', $form)->text(Text::instance());
         // render form
         $view = View::factory($this->_settings->get('view.create'), array('viewer' => $viewer));
         // event
         Event::raise($this, Event::BEFORE_STORE_RENDER, array('model' => NULL, 'form' => $form, 'viewer' => $viewer, 'view' => $view));
         // render
         $this->response->body($view->render());
         // return false
         return FALSE;
     }
 }
 /**
  * create an image size
  *
  * @param string $file 
  * @param array $size size configpart
  * @param string $path destination
  */
 public function create_size($size, $destination)
 {
     // size data to return
     $sizeData = array();
     // aspect ratios ofsource and destination
     $arSource = $this->height / $this->width;
     $arDestination = $size['height'] / $size['width'];
     // 0. check if we're enlarging
     if ($size['enlarge'] == FALSE || isset($size['enlarge_max'])) {
         if ($size['enlarge'] == FALSE) {
             // no enlarge allowed: multiply original by 1
             $factor = 1;
         } else {
             // enlarge allowed, but to a max: multiply original by enlarge_max
             $factor = $size['enlarge_max'];
         }
         // check for image too small: stretch or crop
         if ($size['method'] == Image::METHOD_CROP || $size['method'] == Image::METHOD_STRETCH) {
             if ($size['width'] > $this->width * $factor || $size['height'] > $this->height * $factor) {
                 return false;
             }
         }
         // check for image too small: scale
         if ($size['method'] == Image::METHOD_SCALE) {
             if ($arSource > $arDestination) {
                 // source image is higher than dest -> height is the determining factor
                 if ($size['height'] > $this->height * $factor) {
                     return false;
                 }
             } else {
                 // source image is wider than dest -> width is the determining factor
                 if ($size['width'] > $this->width * $factor) {
                     return false;
                 }
             }
         }
     }
     // 1. resize the image
     switch ($size['method']) {
         case Image::METHOD_SCALE:
             $this->resize($size['width'], $size['height']);
             break;
         case Image::METHOD_FIT:
             $this->resize($size['width'], $size['height'], Image::INVERSE);
             break;
         case Image::METHOD_CROP:
             $this->resize($size['width'], $size['height'], Image::INVERSE);
             $this->crop($size['width'], $size['height']);
             break;
         case Image::METHOD_STRETCH:
             $this->resize($size['width'], $size['height'], Image::NONE);
             break;
         default:
             return false;
     }
     // 2. apply filter
     if (isset($size['filters']) && is_array($size['filters'])) {
         foreach ($size['filters'] as $name => $filter) {
             $method = 'filter_' . $name;
             if (method_exists($this, $method)) {
                 $this->{$method}($filter);
             }
         }
     }
     // 3. save the image
     $this->save($destination, isset($size['quality']) ? $size['quality'] : 100);
     FS::permissions($destination, 0755);
     // 4. return the data
     $sizeData['width'] = $this->width;
     $sizeData['height'] = $this->height;
     return $sizeData;
 }