XOOPS  2.6.0
images.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
13 
23 include __DIR__ . '/header.php';
24 
25 // Call Header
26 $xoops->header('admin:images/images_admin_images.tpl');
27 
28 $admin_page = new \Xoops\Module\Admin();
29 $admin_page->renderNavigation('images.php');
30 
31 $mimetypes = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/x-png', 'image/png');
32 
33 switch ($op) {
34  case 'save':
35  if (!$xoops->security()->check()) {
36  $xoops->redirect('images.php?imgcat_id=' . $imgcat_id, 3, implode('<br />', $xoops->security()->getErrors()));
37  }
38 
40 
41  $category = $helper->getHandlerCategories()->get($imgcat_id);
42  $image_id = Request::getInt('image_id', 0);
43  if (isset($image_id) && $image_id != 0) {
44  $obj = $helper->getHandlerImages()->get($image_id);
45  $isnew = false;
46  } else {
47  $obj = $helper->getHandlerImages()->create();
48  $obj->setVar('image_name', 'blank.gif');
49  $obj->setVar('image_mimetype', 'image/gif');
50  $isnew = true;
51  }
52 
53  $obj->setVar('image_nicename', Request::getString('image_nicename', ''));
54  $obj->setVar('image_created', time());
55  $obj->setVar('image_display', Request::getInt('image_display', 1));
56  $obj->setVar('image_weight', Request::getInt('image_weight', 0));
57  $obj->setVar('imgcat_id', $imgcat_id);
58 
59  // Default value
60  $image_body = '';
61  $error = true;
62  $error_message = '';
63  $xoops_upload_file = Request::getArray('xoops_upload_file', array());
64  if ($_FILES[$xoops_upload_file[0]]['error'] === 0) {
65  $uploader = new XoopsMediaUploader(XOOPS_UPLOAD_PATH . '/images', $mimetypes, $category->getVar('imgcat_maxsize'), $category->getVar('imgcat_maxwidth'), $category->getVar('imgcat_maxheight'));
66  if ($uploader->fetchMedia($xoops_upload_file[0])) {
67  $uploader->setPrefix('img');
68  if (!$uploader->upload()) {
69  $error_message .= $uploader->getErrors();
70  $obj->setVar('image_name', 'blank.gif');
71  $obj->setVar('image_mimetype', 'image/gif');
72  } else {
73  $error = false;
74  $obj->setVar('image_mimetype', $uploader->getMediaType());
75  if ($category->getVar('imgcat_storetype') == 'db' && $isnew) {
76  $fp = @fopen($uploader->getSavedDestination(), 'rb');
77  $fbinary = @fread($fp, filesize($uploader->getSavedDestination()));
78  @fclose($fp);
79  $image_body = $fbinary;
80  } else {
81  $obj->setVar('image_name', 'images/' . $uploader->getSavedFileName());
82  }
83  }
84  }
85  }
86  if ($error == true) {
87  $xoops->tpl()->assign('error_message', $error_message);
88  } else {
89  if ($image_id = $helper->getHandlerImages()->insert($obj)) {
90  if ($category->getVar('imgcat_storetype') == 'db' && $isnew) {
91  $imagebody = $helper->getHandlerImagesBody()->get($image_id);
92  if (!is_object($imagebody)) {
93  $imagebody = $helper->getHandlerImagesBody()->create();
94  $imagebody->setVar('image_id', $image_id);
95  }
96  $imagebody->setVar('image_body', $image_body);
97  if ($helper->getHandlerImagesBody()->insert($imagebody)) {
98  @unlink($uploader->getSavedDestination());
99  }
100  }
101  $xoops->redirect('images.php?imgcat_id=' . $imgcat_id, 2, implode('<br />', $msg));
102  }
103  echo $xoops->alert('error', $obj->getHtmlErrors());
104  }
105  $form = $helper->getForm($obj, 'image');
106  $xoops->tpl()->assign('form', $form->render());
107  break;
108 
109  case 'add':
110  $obj = $helper->getHandlerImages()->create();
111  $obj->setVar('imgcat_id', $imgcat_id);
112  $form = $helper->getForm($obj, 'image');
113  $xoops->tpl()->assign('form', $form->render());
114  break;
115 
116  case 'edit':
117  $image_id = Request::getInt('image_id', 0);
118  if ($image_id > 0) {
119  $obj = $helper->getHandlerImages()->get($image_id);
120  $form = $helper->getForm($obj, 'image');
121  $xoops->tpl()->assign('form', $form->render());
122  }
123  break;
124 
125  case 'del':
126  $image_id = Request::getInt('image_id', 0);
127  if ($image_id > 0) {
128  $ok = Request::getInt('ok', 0);
129  $obj = $helper->getHandlerImages()->get($image_id);
130 
131  if ($ok == 1) {
132  if (!$xoops->security()->check()) {
133  $xoops->redirect('images.php?imgcat_id=' . $imgcat_id, 3, implode('<br />', $xoops->security()->getErrors()));
134  }
135  $category = $helper->getHandlerCategories()->get($obj->getvar('imgcat_id'));
136 
137  if ($helper->getHandlerImages()->delete($obj)) {
138  if ($category->getVar('imgcat_storetype') == 'db') {
139  $helper->getHandlerImagesBody()->delete($helper->getHandlerImagesBody()->get($image_id));
140  } else {
141  unlink(\XoopsBaseConfig::get('uploads-path') . '/' . $obj->getVar('image_name'));
142  }
143  $xoops->redirect('images.php?imgcat_id=' . $imgcat_id, 2, XoopsLocale::S_DATABASE_UPDATED);
144  }
145  } else {
146  $category = $helper->getHandlerCategories()->get($obj->getvar('imgcat_id'));
147  if ($category->getVar('imgcat_storetype') == 'db') {
148  $img = \XoopsBaseConfig::get('url') . '/image.php?id=' . $image_id;
149  } else {
150  $img = \XoopsBaseConfig::get('uploads-url') . '/' . $obj->getVar('image_name');
151  }
152  echo $xoops->confirm(
153  array('op' => 'del', 'ok' => 1, 'image_id' => $image_id, 'imgcat_id' => $obj->getVar('imgcat_id')),
154  \XoopsBaseConfig::get('url') . '/modules/images/admin/images.php',
155  sprintf(_AM_IMAGES_IMG_DELETE, $obj->getVar('image_nicename'))
156  . '<br /><br /><img src="' . $img . '" /><br />'
157  );
158  }
159  }
160  break;
161 
162  case 'display':
163  $image_id = Request::getInt('image_id', 0);
164  if ($image_id > 0) {
165  $obj = $helper->getHandlerImages()->get($image_id);
166  $old = $obj->getVar('image_display');
167  $obj->setVar('image_display', !$old);
168  if (!$helper->getHandlerImages()->insert($obj)) {
169  $error = true;
170  }
171  }
172  break;
173 
174  case 'list':
175  default:
176  // Get rights
177  $imgcat_write = $gperm_handler->checkRight('imgcat_write', $imgcat_id, $groups, $xoops->module->mid());
178  if ($imgcat_write) {
179  $admin_page->addItemButton(_AM_IMAGES_IMG_ADD, 'images.php?op=add&imgcat_id=' . $imgcat_id, 'add');
180  $admin_page->renderButton();
181  }
182 
183  // Get category store type
184  $category = $helper->getHandlerCategories()->get($imgcat_id);
185  if (is_object($category)) {
186  if ($category->getVar('imgcat_storetype') == 'db') {
187  $xoops->tpl()->assign('db_store', 1);
188  }
189  }
190 
191  // Category Select form
192  $form_category = $helper->getForm($imgcat_id, 'categoryselect');
193  $xoops->tpl()->assign('form_category', $form_category->render());
194 
195  $imgcount = $helper->getHandlerImages()->countByCategory($imgcat_id);
196  $images = $helper->getHandlerImages()->getByCategory($imgcat_id, $start, $helper->getConfig('images_pager'), false);
197  $xoops->tpl()->assign('images', $images);
198 
199  if ($imgcount > 0 && $imgcount > $helper->getConfig('images_pager')) {
200  $nav = new XoopsPageNav($imgcount, $helper->getConfig('images_pager'), $start, 'start', 'imgcat_id=' . $imgcat_id);
201  $xoops->tpl()->assign('nav_menu', $nav->renderNav());
202  }
203 
204  $xoops->tpl()->assign('listimg', true);
205  $xoops->tpl()->assign('imgcat_id', $imgcat_id);
206  break;
207 }
208 $xoops->footer();
if(empty($settings['ROOT_PATH'])) elseif(empty($settings['DB_PARAMETERS'])) $error
$form
Definition: xoops_code.php:21
$gperm_handler
$xoops
Definition: admin.php:25
const _AM_IMAGES_IMG_ADD
Definition: admin.php:59
$admin_page
Definition: images.php:28
static get($name)
$groups
$imgcat_id
$helper
if(!is_object($module)||!$module->getVar('isactive')) $msg
Definition: groupperm.php:38
$image_id
Definition: image.php:30
$start
$op
$mimetypes
Definition: images.php:31
const _AM_IMAGES_IMG_DELETE
Definition: admin.php:62
const S_DATABASE_UPDATED
Definition: en_US.php:1024
const _AM_IMAGES_IMG_SAVE
Definition: admin.php:61