imagecache.inc in Patterns 5
File
components/imagecache.inc
View source
<?php
function imagecache_patterns($op, $id = null, &$data = null) {
switch ($op) {
case 'tags':
return array(
'imagecache',
);
break;
case 'actions':
return array(
'imagecache_admin' => t('Setup Imagecache'),
);
break;
case 'prepare':
$presets = array();
if (!empty($data['preset'])) {
for ($i = 0; $item = $data['preset'][$i]; $i++) {
$data[$i] = $data['preset'][$i];
unset($data['preset'][$i]);
}
if (empty($data['preset'])) {
unset($data['preset']);
}
}
for ($index = 0; $preset = $data[$index]; $index++) {
if ($pid = _imagecache_preset_load_by_name($preset['name'])) {
$preset['presetid'] = $pid;
}
else {
$pid = 'new';
}
if (!empty($preset['action']) && !$preset['action'][0]) {
$temp = $preset['action'];
unset($preset['action']);
$preset['action'][0] = $temp;
}
foreach ((array) $preset['action'] as $i => $action) {
$function = $action['function'] = $action['type'];
unset($action['type']);
$skip = array(
'data',
'actionid',
'presetid',
'weight',
);
foreach ($action as $key => $value) {
if (in_array($key, $skip)) {
continue;
}
else {
$action['data'][$key] = $value;
unset($action[$key]);
}
}
if (!$action['weight']) {
$action['weight'] = 0;
}
$preset['handlers']['new'][$function] = $action;
unset($data[$index][$i], $preset[$i]);
}
if ($pid == 'new') {
$presets[$pid][] = $preset;
}
else {
$presets[$pid] = $preset;
}
unset($data[$index]);
}
unset($data['preset']);
$data['presets'] = $presets;
break;
case 'pre-validate':
break;
case 'form_id':
return 'imagecache_admin';
break;
case 'build':
foreach ((array) $data['presets']['new'] as $preset) {
_imagecache_preset_create($preset['name']);
$pid = db_result(db_query('SELECT id FROM {sequences} WHERE name = "{imagecache}_preset_presetid"'));
$data['presets'][$pid] = $preset;
}
unset($data['presets']['new']);
$presets = _imagecache_get_presets(true);
foreach ($data['presets'] as $pid => $preset) {
if (is_numeric($pid)) {
$actions = _imagecache_actions_get_by_presetid($pid);
foreach ($actions as $action) {
_imagecache_action_delete($action);
}
foreach ($preset['handlers']['new'] as $handler => $action) {
$new = array();
$new['data'] = array(
'function' => $handler,
);
$new['presetid'] = $pid;
$new['weight'] = 0;
_imagecache_action_create($new);
$actionid = db_result(db_query('SELECT id FROM {sequences} WHERE name = "{imagecache_action}_actionid"'));
$action['presetid'] = $pid;
$action['actionid'] = $actionid;
$data['presets'][$pid]['handlers'][$actionid] = $action;
}
unset($data['presets'][$pid]['handlers']['new']);
if ($preset['delete']) {
$_POST['preset-op'][$pid] = t('Delete preset');
}
else {
$_POST['preset-op'][$pid] = t('Update preset');
}
}
}
return $data;
break;
case 'validate':
break;
case 'params':
break;
case 'reverse':
foreach ($data as $pid => $preset) {
if ($preset[$pid]['delete']) {
unset($preset[$pid]['delete']);
}
else {
$preset[$pid]['delete'] = true;
}
}
return $data;
break;
case 'cleanup':
unset($_POST['preset-op'], $_REQUEST['destination']);
break;
}
}