From f139f3569bf64dceb311f2b5eb1bdbf6c7bd79ee Mon Sep 17 00:00:00 2001 From: Krator Date: Tue, 17 May 2016 16:26:33 +0200 Subject: [PATCH 1/2] Feature: Screenshot for Download --- lib/Downloads/Controller/User.php | 3 ++ lib/Downloads/Entity/Download.php | 17 +++++++++++ lib/Downloads/Form/Handler/Admin/Edit.php | 35 ++++++++++++++++++++++- lib/Downloads/Installer.php | 7 +++++ lib/Downloads/Util.php | 2 +- templates/admin/edit.tpl | 5 ++++ templates/admin/modifyconfig.tpl | 2 +- templates/user/display.tpl | 4 +++ 8 files changed, 72 insertions(+), 3 deletions(-) diff --git a/lib/Downloads/Controller/User.php b/lib/Downloads/Controller/User.php index 501781e..22999ae 100644 --- a/lib/Downloads/Controller/User.php +++ b/lib/Downloads/Controller/User.php @@ -113,12 +113,15 @@ public function display($args) //$item['filetype'] = FileUtil::getExtension($item['filename']); $filename = $item->getFilename(); $filetype = (!empty($filename)) ? FileUtil::getExtension($filename) : $this->__('unknown'); + // screenshot path + $ssPath = $this->getVar('screenshot_folder'). "/"; $this->view->setCacheId('display|lid_' . $lid); return $this->view ->assign('item', $item) ->assign('filetype', $filetype) + ->assign('ssPath', $ssPath) ->fetch('user/display.tpl'); } diff --git a/lib/Downloads/Entity/Download.php b/lib/Downloads/Entity/Download.php index 630a8a1..282b4bc 100644 --- a/lib/Downloads/Entity/Download.php +++ b/lib/Downloads/Entity/Download.php @@ -152,6 +152,13 @@ class Downloads_Entity_Download extends Zikula_EntityAccess */ private $objectid = ''; + /** + * item screenshot + * + * @ORM\Column(nullable=true) + */ + private $screenshot; + /** * Constructor */ @@ -336,4 +343,14 @@ public function setObjectid($objectid) $this->objectid = $objectid; } + public function getScreenshot() + { + return $this->screenshot; + } + + public function setScreenshot($screenshot) + { + $this->screenshot = $screenshot; + } + } diff --git a/lib/Downloads/Form/Handler/Admin/Edit.php b/lib/Downloads/Form/Handler/Admin/Edit.php index 8aa243e..860760d 100644 --- a/lib/Downloads/Form/Handler/Admin/Edit.php +++ b/lib/Downloads/Form/Handler/Admin/Edit.php @@ -71,13 +71,19 @@ public function handleCommand(Zikula_Form_View $view, &$args) } $storage = $this->getVar('upload_folder'); + $screens_folder = $this->getVar('screenshot_folder'); if ($args['commandName'] == 'delete') { if(SecurityUtil::checkPermission('Downloads::', '::', ACCESS_DELETE)) { $file = $this->entityManager->getRepository('Downloads_Entity_Download')->find($this->id); - $oldname = $file->getFilename(); + // file + $oldname = $file->getFilename(); $fullpath = DataUtil::formatForOS("$storage/$oldname"); @unlink($fullpath); + // screenshot + $oldssname = $file->getScreenshot(); + $fullsspath = DataUtil::formatForOS("$screens_folder/$oldssname"); + @unlink($fullsspath); $this->entityManager->remove($file); $this->entityManager->flush(); ModUtil::apiFunc('Downloads', 'user', 'clearItemCache', $file); @@ -105,6 +111,11 @@ public function handleCommand(Zikula_Form_View $view, &$args) $plugin->setError($this->__('OR specify a download url.')); return false; } + // validate the max file size for screenshot + $screenshotmaxsize = $this->getVar('screenshotmaxsize'); + if ($data['screenshot']['size'] > $screenshotmaxsize){ + return LogUtil::registerError($this->__f('Screenshot file is bigger that the max. file size:', $screenshotmaxsize)); + } $newFileUploadedFlag = false; $data['update'] = new DateTime(); @@ -127,6 +138,20 @@ public function handleCommand(Zikula_Form_View $view, &$args) $data['filename'] = ''; } + // screenshot + if ((is_array($data['screenshot'])) && ($data['screenshot']['size'] > 0)) { + $result = Downloads_Util::uploadFile('screenshot', $screens_folder, $data['screenshot']['name']); + if (!$result) { + return LogUtil::registerError($result); + } + $newSSUploadedFlag = true; + $screenshot = $data['screenshot']['name']; + unset($data['screenshot']); + $data['screenshot'] = $screenshot; + } else if (((is_array($data['filename'])) && (!$data['filename']['size'] > 0)) || (!isset($data['filename']))) { + $data['screenshot'] = ''; + } + // switch between edit and create mode if ($this->id) { if(SecurityUtil::checkPermission('Downloads::', '::', ACCESS_EDIT)) { @@ -138,6 +163,14 @@ public function handleCommand(Zikula_Form_View $view, &$args) @unlink($fullpath); } else { $data['filename'] = $file->getFilename(); + } + // screenshot + $oldssname = $file->getScreenshot(); + if ($newSSUploadedFlag) { + $fullpath = "$screens_folder/$oldssname"; + @unlink($fullpath); + } else { + $data['screenshot'] = $file->getScreenshot(); } } else { $view->setPluginErrorMsg('title', $this->__('You are not authorized to edit this entry!')); diff --git a/lib/Downloads/Installer.php b/lib/Downloads/Installer.php index 353a952..ba499b3 100644 --- a/lib/Downloads/Installer.php +++ b/lib/Downloads/Installer.php @@ -194,6 +194,13 @@ private function createUploadDir() LogUtil::registerStatus($this->__f('Created the file storage directory successfully at [%s]. Be sure that this directory is accessible via web and writable by the webserver.', $uploaddir)); } + // upload dir creation + $screenshotdir = $this->getVar('screenshot_folder'); + + if (mkdir($screenshotdir, System::getVar('system.chmod_dir', 0777), true)) { + LogUtil::registerStatus($this->__f('Created the screenshots directory successfully at [%s]. Be sure that this directory is accessible via web and writable by the webserver.', $screenshotdir)); + } + return $uploaddir; } diff --git a/lib/Downloads/Util.php b/lib/Downloads/Util.php index f9f4537..f0646eb 100644 --- a/lib/Downloads/Util.php +++ b/lib/Downloads/Util.php @@ -27,7 +27,7 @@ public static function getModuleDefaults() 'showscreenshot' => true, 'thumbnailwidth' => 100, 'thumbnailheight' => 100, - 'screenshotmaxsize' => '2-Mb', + 'screenshotmaxsize' => '2097152', 'thumbnailmaxsize' => '1-Mb', 'limit_extension' => true, 'allowscreenshotupload' => true, diff --git a/templates/admin/edit.tpl b/templates/admin/edit.tpl index 5edb5c6..4149e17 100644 --- a/templates/admin/edit.tpl +++ b/templates/admin/edit.tpl @@ -41,6 +41,11 @@
+ {formlabel for="screenshot" __text="Choose file for screenshot"} + {formuploadinput id="screenshot"} +
+ +
{formlabel mandatorysym=true for="submitter" __text="Submitted by"} {formtextinput id="submitter" mandatory=true maxLength=60}
diff --git a/templates/admin/modifyconfig.tpl b/templates/admin/modifyconfig.tpl index 2eb2160..c77333c 100644 --- a/templates/admin/modifyconfig.tpl +++ b/templates/admin/modifyconfig.tpl @@ -57,7 +57,7 @@
- +
diff --git a/templates/user/display.tpl b/templates/user/display.tpl index 5266b41..340edb1 100644 --- a/templates/user/display.tpl +++ b/templates/user/display.tpl @@ -28,6 +28,10 @@ {/if} + {if $item->getScreenshot()} +
  • {gt text='Screenshot'}:
  • +
    + {/if}
    {checkpermissionblock component='Downloads::' instance='::' level=ACCESS_EDIT} From 9cfbacbee4a894d2b06f268e3bada2d7bd07a2e7 Mon Sep 17 00:00:00 2001 From: Krator Date: Tue, 17 May 2016 16:48:15 +0200 Subject: [PATCH 2/2] Feature: Screenshot for Download --- lib/Downloads/Controller/User.php | 6 +++--- lib/Downloads/Entity/Download.php | 2 +- lib/Downloads/Form/Handler/Admin/Edit.php | 26 +++++++++++------------ lib/Downloads/Installer.php | 2 +- templates/admin/edit.tpl | 2 +- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/Downloads/Controller/User.php b/lib/Downloads/Controller/User.php index 22999ae..c55fd70 100644 --- a/lib/Downloads/Controller/User.php +++ b/lib/Downloads/Controller/User.php @@ -113,15 +113,15 @@ public function display($args) //$item['filetype'] = FileUtil::getExtension($item['filename']); $filename = $item->getFilename(); $filetype = (!empty($filename)) ? FileUtil::getExtension($filename) : $this->__('unknown'); - // screenshot path - $ssPath = $this->getVar('screenshot_folder'). "/"; + // screenshot path + $ssPath = $this->getVar('screenshot_folder'). "/"; $this->view->setCacheId('display|lid_' . $lid); return $this->view ->assign('item', $item) ->assign('filetype', $filetype) - ->assign('ssPath', $ssPath) + ->assign('ssPath', $ssPath) ->fetch('user/display.tpl'); } diff --git a/lib/Downloads/Entity/Download.php b/lib/Downloads/Entity/Download.php index 282b4bc..7778f8d 100644 --- a/lib/Downloads/Entity/Download.php +++ b/lib/Downloads/Entity/Download.php @@ -343,7 +343,7 @@ public function setObjectid($objectid) $this->objectid = $objectid; } - public function getScreenshot() + public function getScreenshot() { return $this->screenshot; } diff --git a/lib/Downloads/Form/Handler/Admin/Edit.php b/lib/Downloads/Form/Handler/Admin/Edit.php index 860760d..4896cde 100644 --- a/lib/Downloads/Form/Handler/Admin/Edit.php +++ b/lib/Downloads/Form/Handler/Admin/Edit.php @@ -77,11 +77,11 @@ public function handleCommand(Zikula_Form_View $view, &$args) if(SecurityUtil::checkPermission('Downloads::', '::', ACCESS_DELETE)) { $file = $this->entityManager->getRepository('Downloads_Entity_Download')->find($this->id); // file - $oldname = $file->getFilename(); + $oldname = $file->getFilename(); $fullpath = DataUtil::formatForOS("$storage/$oldname"); @unlink($fullpath); - // screenshot - $oldssname = $file->getScreenshot(); + // screenshot + $oldssname = $file->getScreenshot(); $fullsspath = DataUtil::formatForOS("$screens_folder/$oldssname"); @unlink($fullsspath); $this->entityManager->remove($file); @@ -111,11 +111,11 @@ public function handleCommand(Zikula_Form_View $view, &$args) $plugin->setError($this->__('OR specify a download url.')); return false; } - // validate the max file size for screenshot - $screenshotmaxsize = $this->getVar('screenshotmaxsize'); - if ($data['screenshot']['size'] > $screenshotmaxsize){ - return LogUtil::registerError($this->__f('Screenshot file is bigger that the max. file size:', $screenshotmaxsize)); - } + // validate the max file size for screenshot + $screenshotmaxsize = $this->getVar('screenshotmaxsize'); + if ($data['screenshot']['size'] > $screenshotmaxsize){ + return LogUtil::registerError($this->__f('Screenshot file is bigger that the max. file size:', $screenshotmaxsize)); + } $newFileUploadedFlag = false; $data['update'] = new DateTime(); @@ -138,9 +138,9 @@ public function handleCommand(Zikula_Form_View $view, &$args) $data['filename'] = ''; } - // screenshot - if ((is_array($data['screenshot'])) && ($data['screenshot']['size'] > 0)) { - $result = Downloads_Util::uploadFile('screenshot', $screens_folder, $data['screenshot']['name']); + // screenshot + if ((is_array($data['screenshot'])) && ($data['screenshot']['size'] > 0)) { + $result = Downloads_Util::uploadFile('screenshot', $screens_folder, $data['screenshot']['name']); if (!$result) { return LogUtil::registerError($result); } @@ -164,8 +164,8 @@ public function handleCommand(Zikula_Form_View $view, &$args) } else { $data['filename'] = $file->getFilename(); } - // screenshot - $oldssname = $file->getScreenshot(); + // screenshot + $oldssname = $file->getScreenshot(); if ($newSSUploadedFlag) { $fullpath = "$screens_folder/$oldssname"; @unlink($fullpath); diff --git a/lib/Downloads/Installer.php b/lib/Downloads/Installer.php index ba499b3..eed2bdf 100644 --- a/lib/Downloads/Installer.php +++ b/lib/Downloads/Installer.php @@ -194,7 +194,7 @@ private function createUploadDir() LogUtil::registerStatus($this->__f('Created the file storage directory successfully at [%s]. Be sure that this directory is accessible via web and writable by the webserver.', $uploaddir)); } - // upload dir creation + // screenshot dir creation $screenshotdir = $this->getVar('screenshot_folder'); if (mkdir($screenshotdir, System::getVar('system.chmod_dir', 0777), true)) { diff --git a/templates/admin/edit.tpl b/templates/admin/edit.tpl index 4149e17..84190c3 100644 --- a/templates/admin/edit.tpl +++ b/templates/admin/edit.tpl @@ -45,7 +45,7 @@ {formuploadinput id="screenshot"} -
    +
    {formlabel mandatorysym=true for="submitter" __text="Submitted by"} {formtextinput id="submitter" mandatory=true maxLength=60}