Skip to content

Commit

Permalink
auto creating the virtual fields for all the resize options along wit…
Browse files Browse the repository at this point in the history
…h a default no-image for when the field is empty or null
  • Loading branch information
dogmatic69 committed Aug 22, 2012
1 parent 0464781 commit 7208c8d
Showing 1 changed file with 67 additions and 4 deletions.
71 changes: 67 additions & 4 deletions Core/Filemanager/Model/Behavior/UploadBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,76 @@ class UploadBehavior extends ModelBehavior {
* @return void
* @access public
*/
public function setup(&$model, $config = array()) {
if (isset($this->settings[$model->alias])) return;
$this->settings[$model->alias] = array();
public function setup(&$Model, $config = array()) {
if (isset($this->settings[$Model->alias])) return;
$this->settings[$Model->alias] = array();

foreach ($config as $field => $options) {
$this->_setupField($model, $field, $options);
if(!$Model->hasField($field)) {
throw new InvalidArgumentException(sprintf('Model "%s" does not contain the field "%s"', $Model->name, $field));
}

$this->_setupField($Model, $field, $options);
$this->_generateVirtualFields($Model, $field);
}
}

/**
* @brief set up virtual fields for the model object for easy access to the image files
*
* @param Model $Model
* @param string $field
*
* @return boolean
*/
protected function _generateVirtualFields($Model, $field) {
$virtualFieldTemplate = sprintf(
'CASE WHEN %%s IS NULL OR %%s = "" THEN "%%s" ELSE ' .
'CONCAT("%s", %%s, "/%%s", %%s) END',
$this->_getUrlPath($Model, $field)
);

$Model->virtualFields[$field . '_full'] = sprintf(
$virtualFieldTemplate,
$Model->alias . '.' . $field,
$Model->alias . '.' . $field,
$this->_emptyFilePath(),
$Model->alias . '.' . $Model->primaryKey,
'',
$Model->alias . '.' . $field
);

if(empty($this->settings[$Model->alias][$field]['thumbnailSizes'])) {
return true;
}

foreach($this->settings[$Model->alias][$field]['thumbnailSizes'] as $name => $size) {
$Model->virtualFields[$field . '_' . $name] = sprintf(
$virtualFieldTemplate,
$Model->alias . '.' . $field,
$Model->alias . '.' . $field,
$this->_emptyFilePath(),
$Model->alias . '.' . $Model->primaryKey,
$name . '_',
$Model->alias . '.' . $field
);
}
}

/**
* @brief get the url path to where images are stored.
*
* @param Model $Model
* @param string $field
*
* @return string
*/
protected function _getUrlPath($Model, $field) {
return str_replace(APP . 'webroot', '', $this->settings[$Model->alias][$field]['path']);
}

protected function _emptyFilePath() {
return '/filemanager/img/no-image.png';
}

/**
Expand Down

0 comments on commit 7208c8d

Please sign in to comment.