public function save($model, $value, $loaded) { $original = $model->get($this->name, FALSE); $config = Kohana::config('torn'); $force_delete = $model->get($this->name . $config->surfix->delete_old, FALSE); if ($force_delete or $this->delete_old_file and $original != $value) { if (file_exists($original)) { try { unlink($original); } catch (Exception $e) { } } if ($original == $value) { $value = NULL; } } if (is_string($value) and preg_match('/^[a-z0-9]{32}-[a-z0-9]{32}$/i', $value)) { if (file_exists(Kohana::$cache_dir . DIRECTORY_SEPARATOR . $value)) { $cache = Cache::instance(); $cached = $cache->get($value); $name = $cached['upload']['name']; if (is_callable($this->torn_filename_callback)) { $name = call_user_func($this->torn_filename_callback, $name); } $i = 1; $original = $name; while (file_exists($this->path . $name)) { $info = pathinfo($original); $name = $info['filename'] . '-' . $i . '.' . $info['extension']; $i++; } rename(Kohana::$cache_dir . DIRECTORY_SEPARATOR . $value, $this->path . $name); $cache->delete($value); return $name; } } $tmp_field = $this->name . $config->surfix->temp; if (is_string($value) and array_key_exists($tmp_field, $_POST) and empty($value) and empty($_POST[$tmp_field])) { return $model->get($this->name, FALSE); // don't save } return parent::save($model, $value, $loaded); }
/** * Adds the CSS class I want all of my textfields to have. * * @param string $model * @param string $column * @return void **/ public function initialize($model, $column) { parent::initialize($model, $column); array_push($this->css_class, 'inp-text'); }
/** * Logic to deal with uploading the image file and generating thumbnails according to * what has been specified in the $thumbnails array. * * @param Validation $validation * @param Jelly_Model $model * @param string $field * @return bool * @uses Image::factory */ public function _upload(Validation $validation, $model, $field) { if (!parent::_upload($validation, $model, $field)) { // Couldn't save the original untouched return FALSE; } $file = $validation[$field]; //no need upload if post helper isset if (!empty($_POST[self::$post_file_helper_prefix . $this->name])) { $this->_filename = $_POST[self::$post_file_helper_prefix . $this->name]; return TRUE; } // Set the filename and the source $filename = $this->_filename; $source = $filename; if ($model->changed($field)) { // Process thumbnails foreach ($this->thumbnails as $thumbnail) { if (!isset($thumbnail['path'])) { // Set the thumbnail path to the original's path if not set $thumbnail['path'] = $this->path; } $filename = explode('/', $filename); $filename = end($filename); // Set the destination $destination = $thumbnail['path'] . $thumbnail['prefix'] . $filename; // Delete old file if necessary $this->_delete_old_file($thumbnail['prefix'] . $model->original($field), $thumbnail['path']); // Set thumb $source = realpath(ltrim($this->_filename, '/')); $thumb = Image::factory($source, $this->driver); // Process thumbnail transformations $thumb = $this->_transform($thumb, $thumbnail['transformations']); // Save the thumbnail $thumb->save($destination, $thumbnail['quality']); } } if (count($this->transformations) > 0 && !empty($source)) { $source = realpath(ltrim($this->_filename, '/')); // Set image $image = Image::factory($source, $this->driver); // Process image transformations $image = $this->_transform($image, $this->transformations); // Save the image $image->save($source, $this->quality); } return TRUE; }