context = gpsa(array('page', 'sort', 'dir', 'crit', 'search_method'));
if(empty($this->context['sort'])) $this->context['sort'] = 'id';
if($this->context['dir'] != 'asc') $this->context['dir'] = 'desc';
}
// -------------------------------------------------------------
function list_view()
{
global $prefs;
extract($prefs);
$out = array();
if (!is_dir(IMPATH) or !is_writeable(IMPATH)) {
$this->_error(gTxt('img_dir_not_writeable', array('{imgdir}' => IMPATH)));
} else {
$out[] = upload_form(gTxt('upload_image'), 'upload', 'insert', $this->event, '', $file_max_upload_size);
}
extract($this->context);
switch ($sort) {
case 'name':
$sort_sql = 'name '.$dir;
break;
case 'thumbnail':
$sort_sql = 'thumbnail '.$dir.', id asc';
break;
case 'category':
$sort_sql = 'category '.$dir.', id asc';
break;
case 'date':
$sort_sql = 'date '.$dir.', id asc';
break;
case 'author':
$sort_sql = 'author '.$dir.', id asc';
break;
default:
$sort = 'id';
$sort_sql = 'id '.$dir;
break;
}
$switch_dir = ($dir == 'desc') ? 'asc' : 'desc';
$criteria = 1;
if ($search_method and $crit) {
$crit_escaped = doSlash($crit);
$critsql = array(
'id' => "id = '$crit_escaped'",
'name' => "name like '%$crit_escaped%'",
'category' => "category like '%$crit_escaped%'",
'author' => "author like '%$crit_escaped%'"
);
if (array_key_exists($search_method, $critsql)) {
$criteria = $critsql[$search_method];
$limit = 500;
} else {
$search_method = '';
$crit = '';
}
} else {
$search_method = '';
$crit = '';
}
$total = safe_count('txp_image', "$criteria");
if ($total < 1) {
if ($criteria != 1) {
$out[] = n.$this->search_form($crit, $search_method);
$this->_message(gTxt('no_results_found'));
} else {
$this->_message(gTxt('no_images_recorded'));
}
} else {
$limit = max(@$image_list_pageby, 15);
list($page, $offset, $numPages) = pager($total, $limit, $page);
$out[] = $this->search_form($crit, $search_method);
$rs = safe_rows('*, unix_timestamp(date) as uDate', 'txp_image',
"$criteria order by $sort_sql limit $offset, $limit");
if ($rs) {
$v = new ImageListView($rs, $this);
$out[] = $v->render();
$out[] = nav_form($this->event, $page, $numPages, $sort, $dir, $crit, $search_method);
$out[] = $this->pageby_form($this->event, $image_list_pageby);
}
}
return join('', $out);
}
// -------------------------------------------------------------
function edit_view($id='')
{
global $txpcfg,$img_dir,$file_max_upload_size;
$out = array();
if (!$id) $id = assert_int(gps('id'));
extract($this->context);
$categories = tree_get('txp_category', NULL, "type='image'");
$rs = safe_row("*", "txp_image", "id = $id");
if ($rs) {
extract($rs);
if ($ext != '.swf')
{
$img = '';
}
else
{
$img = '';
}
if ($thumbnail and ($ext != '.swf'))
{
$thumb = '
';
}
else
{
$thumb = '';
}
$out[] = startTable('edit').
tr(
td(
$img.br.
upload_form(gTxt('replace_image'), 'replace_image_form', 'replace', $this->event, $id, $file_max_upload_size, 'image-replace', '')
)
).
tr(
td(
join('',
array(
($thumbnail)
? $thumb.br
: '',
upload_form(gTxt('upload_thumbnail'),'upload_thumbnail',
'thumbnail_insert',$this->event,$id,$file_max_upload_size, 'upload-thumbnail', '')
)
)
)
);
$out[] = (check_gd($ext))
? $this->thumb_ui($id, $thumbnail)
: '';
$out[] = tr(
td(
form(
graf(''.br.
fInput('text', 'name', $name, 'edit', '', '', '', '', 'image-name')).
graf(''.br.
categorySelectInput('image', 'category', $category, 'image-category')).
graf(''.br.
fInput('text', 'alt', $alt, 'edit', '', '', 50, '', 'alt-text')).
graf(''.br.
text_area('caption', '100', '400', $caption, 'caption')).
n.graf(fInput('submit', '', gTxt('save'), 'publish')).
// web two.oh ;-) href(gTxt('or_cancel'), "?event=$this->event".a.'step=list').
n.hInput('id', $id).
n.eInput($this->event).
n.sInput('save').
n.hInput('sort', $sort).
n.hInput('dir', $dir).
n.hInput('page', $page).
n.hInput('search_method', $search_method).
n.hInput('crit', $crit)
)
)
).
endTable();
}
return join('', $out);
}
// -------------------------------------------------------------
function save_post()
{
extract(doSlash(gpsa(array('name','category','caption','alt'))));
$id = $this->psi('id');
safe_update(
"txp_image",
"name = '$name',
category = '$category',
alt = '$alt',
caption = '$caption'",
"id = $id"
);
update_lastmod();
$this->_message(gTxt('image_updated', array('{name}' => $name)));
$this->_set_view('list');
}
// -------------------------------------------------------------
function insert_post()
{
$meta = gpsa(array('caption', 'alt', 'category'));
$img_result = image_data($_FILES['thefile'], '');
if (is_array($img_result)) {
list($message, $id) = $img_result;
$this->_message($message);
$this->_set_view('edit', $id);
} else {
$this->_error(gTxt($img_result));
$this->_set_view('list');
}
}
// -------------------------------------------------------------
function delete_post()
{
$id = assert_int(ps('id'));
$rs = safe_row('*', 'txp_image', "id = $id");
if ($rs) {
extract($rs);
$rsd = safe_delete('txp_image', "id = $id");
$ul = false;
if (is_file(IMPATH.$id.$ext)) {
$ul = unlink(IMPATH.$id.$ext);
}
if (is_file(IMPATH.$id.'t'.$ext)) {
$ult = unlink(IMPATH.$id.'t'.$ext);
}
if ($rsd && $ul) {
update_lastmod();
$this->_message(gTxt('image_deleted', array('{name}' => $name)));
}
else {
$this->_message(gTxt('image_deleted_failed', array('{name}' => $name)));
}
}
$this->_set_view('list');
}
// -------------------------------------------------------------
function replace_post()
{
$id = assert_int(gps('id'));
$rs = safe_row("*", "txp_image", "id = $id");
if ($rs) {
$meta = array('category' => $rs['category'], 'caption' => $rs['caption'], 'alt' => $rs['alt']);
} else {
$meta = '';
}
$img_result = image_data($_FILES['thefile'], $meta, $id);
if(is_array($img_result)) {
list($message, $id) = $img_result;
$this->_message($message);
} else {
$this->_error($img_result);
}
$this->_set_view('edit', $id);
}
// -------------------------------------------------------------
function thumbnail_insert_post()
{
global $img_dir;
$id = $this->psi('id');
$file = $_FILES['thefile']['tmp_name'];
$name = $_FILES['thefile']['name'];
$file = get_uploaded_file($file);
list(,,$extension) = @getimagesize($file);
if (($file !== false) && $this->extensions[$extension]) {
$ext = $this->extensions[$extension];
$newpath = IMPATH.$id.'t'.$ext;
if (shift_uploaded_file($file, $newpath) == false) {
image_list($newpath.sp.gTxt('upload_dir_perms'));
} else {
chmod($newpath,0644);
safe_update("txp_image", "thumbnail = 1", "id = $id");
update_lastmod();
$this->_message(gTxt('image_uploaded', array('{name}' => $name)));
$this->_set_view('edit', $id);
}
} else {
if ($file === false) {
$this->_error(upload_get_errormsg($_FILES['thefile']['error']));
$this->_set_view('edit', $id);
} else {
$this->_error(gTxt('only_graphic_files_allowed'));
$this->_set_view('edit', $id);
}
}
}
// -------------------------------------------------------------
function thumbnail_create_post()
{
$id = $this->psi('id');
extract(doSlash(gpsa(array('thumbnail_clear_settings', 'thumbnail_delete', 'width', 'height', 'crop'))));
if($thumbnail_clear_settings) {
$message = $this->thumbnail_clear_settings($id);
} elseif($thumbnail_delete) {
$message = $this->thumbnail_delete($id);
} else {
$width = (int) $width;
$height = (int) $height;
if ($width != 0 || $height != 0) {
if (img_makethumb($id, $width, $height, $crop)) {
global $prefs;
if ($width == 0) $width = '';
if ($height == 0) $height = '';
$prefs['thumb_w'] = $width;
$prefs['thumb_h'] = $height;
$prefs['thumb_crop'] = $crop;
// hidden prefs
set_pref('thumb_w', $width, 'image', 2);
set_pref('thumb_h', $height, 'image', 2);
set_pref('thumb_crop', $crop, 'image', 2);
update_lastmod();
$message = gTxt('thumbnail_saved', array('{id}' => $id));
} else {
$message = gTxt('thumbnail_not_saved', array('{id}' => $id));
}
} else {
$message = messenger('invalid_width_or_height', "($width)/($height)", '');
}
}
$this->_message($message);
$this->_set_view('edit', $id);
}
// -------------------------------------------------------------
function thumbnail_clear_settings($id)
{
set_pref('thumb_w', '', 'image', 2);
set_pref('thumb_h', '', 'image', 2);
set_pref('thumb_crop', 0, 'image', 2);
$GLOBALS['prefs'] = get_prefs();
return '';
}
// -------------------------------------------------------------
function thumbnail_delete($id)
{
$ext = safe_field('ext', 'txp_image', "id = $id");
$file = IMPATH.DS.$id.'t'.$ext;
if (unlink($file)) {
safe_update('txp_image', 'thumbnail = 0', "id = $id");
$message = gTxt('thumbnail_removed');
} else {
$message = gTxt('thumbnail_not_removed');
}
return $message;
}
// -------------------------------------------------------------
function thumb_ui($id,$thumbnail)
{
global $prefs, $sort, $dir, $page, $search_method, $crit;
extract($prefs);
return
tr(
td(
form(
graf(gTxt('manage_thumbnail')).
startTable('', 'left', '', 1).
tr(
tda(
''.sp.
fInput('text', 'width', @$thumb_w, 'edit', '', '', 4, '', 'width').sp.
''.sp.
fInput('text', 'height', @$thumb_h, 'edit', '', '', 4, '', 'height').sp.
''.sp.
checkbox('crop', 1, @$thumb_crop, '', 'crop')
, ' class="noline" style="vertical-align: top;"').
tda(
graf(fInput('submit', 'create', gTxt('create'), 'smallerbox').
($thumbnail ? sp.fInput('submit', 'thumbnail_delete', gTxt('remove'), 'smallerbox') : '')).
graf(fInput('submit', 'thumbnail_clear_settings', gTxt('clear_settings'), 'smallerbox'))
, ' colspan="6" class="noline"')
).
endTable().
n.hInput('id', $id).
n.eInput($this->event).
n.sInput('thumbnail_create').
n.hInput('sort', $sort).
n.hInput('dir', $dir).
n.hInput('page', $page).
n.hInput('search_method', $search_method).
n.hInput('crit', $crit)
)
)
);
}
// -------------------------------------------------------------
function search_form($crit, $method)
{
$methods = array(
'id' => gTxt('ID'),
'name' => gTxt('name'),
'category' => gTxt('image_category'),
'author' => gTxt('author')
);
return search_form($this->event, 'list', $crit, $methods, $method, 'name');
}
// -------------------------------------------------------------
function change_pageby()
{
event_change_pageby($this->event);
$this->_set_view('list');
}
}
// -------------------------------------------------------------
class ImageListView extends TxpTableView
{
var $controller = NULL;
function ImageListView(&$rows, $controller, $caption='', $edit_actions=array())
{
parent::TxpTableView($rows, $caption, $edit_actions);
$this->controller = $controller;
}
function head($cols)
{
if (!$this->controller) return;
extract($this->controller->context);
$switch_dir = ($dir == 'asc') ? 'desc' : 'asc';
$e = $this->controller->event;
return
'