Skip to content

Commit

Permalink
Merge pull request #33 from KratorD/master
Browse files Browse the repository at this point in the history
New feature: Screenshot for a download
  • Loading branch information
craigh committed May 17, 2016
2 parents 020fb85 + 9cfbacb commit e16e288
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/Downloads/Controller/User.php
Expand Up @@ -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');
}

Expand Down
17 changes: 17 additions & 0 deletions lib/Downloads/Entity/Download.php
Expand Up @@ -152,6 +152,13 @@ class Downloads_Entity_Download extends Zikula_EntityAccess
*/
private $objectid = '';

/**
* item screenshot
*
* @ORM\Column(nullable=true)
*/
private $screenshot;

/**
* Constructor
*/
Expand Down Expand Up @@ -336,4 +343,14 @@ public function setObjectid($objectid)
$this->objectid = $objectid;
}

public function getScreenshot()
{
return $this->screenshot;
}

public function setScreenshot($screenshot)
{
$this->screenshot = $screenshot;
}

}
33 changes: 33 additions & 0 deletions lib/Downloads/Form/Handler/Admin/Edit.php
Expand Up @@ -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);
// 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);
Expand Down Expand Up @@ -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();
Expand All @@ -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)) {
Expand All @@ -139,6 +164,14 @@ public function handleCommand(Zikula_Form_View $view, &$args)
} 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!'));
return false;
Expand Down
7 changes: 7 additions & 0 deletions lib/Downloads/Installer.php
Expand Up @@ -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));
}

// screenshot 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;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Downloads/Util.php
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions templates/admin/edit.tpl
Expand Up @@ -40,6 +40,11 @@
{formtextinput textMode='multiline' id="description" rows='6' cols='50' mandatory=true}
</div>

<div class="z-formrow">
{formlabel for="screenshot" __text="Choose file for screenshot"}
{formuploadinput id="screenshot"}
</div>

<div class="z-formrow">
{formlabel mandatorysym=true for="submitter" __text="Submitted by"}
{formtextinput id="submitter" mandatory=true maxLength=60}
Expand Down
2 changes: 1 addition & 1 deletion templates/admin/modifyconfig.tpl
Expand Up @@ -57,7 +57,7 @@

<div class="z-formrow">
<label for="screenshotmaxsize">{gt text='Maximum filesize for screenshots'}</label>
<input disabled="disabled" id="screenshotmaxsize" type="text" name="screenshotmaxsize" value="{$modvars.Downloads.screenshotmaxsize|safetext}" size="40" maxlength="80" />
<input id="screenshotmaxsize" type="text" name="screenshotmaxsize" value="{$modvars.Downloads.screenshotmaxsize|safetext}" size="40" maxlength="80" />
</div>

<div class="z-formrow">
Expand Down
4 changes: 4 additions & 0 deletions templates/user/display.tpl
Expand Up @@ -28,6 +28,10 @@
</ul>
{/if}
</li>
{if $item->getScreenshot()}
<li><strong>{gt text='Screenshot'}:</strong></li>
<center><img src="{$baseurl}{$ssPath}{$item->getScreenshot()}"></center>
{/if}
</ul>
</div>
{checkpermissionblock component='Downloads::' instance='::' level=ACCESS_EDIT}
Expand Down

0 comments on commit e16e288

Please sign in to comment.