Skip to content

Commit

Permalink
Fixed issue #14180: Multiple logo filename with same url in dropdown
Browse files Browse the repository at this point in the history
Fixed issue #14171: Survey owner without template edit right can not upload logo
New feature : list image in Survey for Brand logo file in Theme option
Dev: use survey folder for Template uploaded image
Dev: separate image in template and image in survey
Dev: separate each image, add otpgroup label
Dev: Put optgroup in image selector (core template)
  • Loading branch information
Shnoulle committed Nov 8, 2018
1 parent 1c7dc0a commit a2bbf64
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 32 deletions.
85 changes: 85 additions & 0 deletions application/controllers/admin/surveyadmin.php
Expand Up @@ -2275,4 +2275,89 @@ public function applythemeoptions($iSurveyID = 0)
}
$this->getController()->redirect(array('admin/survey/sa/view/surveyid/'.$iSurveyID));
}

/**
* Upload an image in directory
* @return json
*/
public function uploadimagefile()
{
$iSurveyID = Yii::app()->request->getPost('surveyid');
$success = false;
$debug = [];
if(!Permission::model()->hasSurveyPermission($iSurveyID, 'surveycontent', 'update')) {
return Yii::app()->getController()->renderPartial(
'/admin/super/_renderJson',
array('data' => ['success' => $success, 'message' => gT("You don't have suffisient right to upload image in this survey"), 'debug' => $debug]),
false,
false
);
}
$debug[] = $_FILES;
if(empty($_FILES)) {
$uploadresult = gT("No file was uploaded.");
return Yii::app()->getController()->renderPartial(
'/admin/super/_renderJson',
array('data' => ['success' => $success, 'message' => $uploadresult, 'debug' => $debug]),
false,
false
);
}
if ($_FILES['file']['error'] == 1 || $_FILES['file']['error'] == 2) {
$uploadresult = sprintf(gT("Sorry, this file is too large. Only files up to %01.2f MB are allowed."), getMaximumFileUploadSize() / 1024 / 1024);
return Yii::app()->getController()->renderPartial(
'/admin/super/_renderJson',
array('data' => ['success' => $success, 'message' => $uploadresult, 'debug' => $debug]),
false,
false
);
}
$checkImage = LSYii_ImageValidator::validateImage($_FILES["file"]["tmp_name"]);
if ($checkImage['check'] === false) {
return Yii::app()->getController()->renderPartial(
'/admin/super/_renderJson',
array('data' => ['success' => $success, 'message' => $checkImage['uploadresult'], 'debug' => $checkImage['debug']]),
false,
false
);
}
$surveyDir = Yii::app()->getConfig('uploaddir')."/surveys/".$iSurveyID;
if (!is_dir($surveyDir)) {
@mkdir($surveyDir);
}
if (!is_dir($surveyDir."/images")) {
@mkdir($surveyDir."/images");
}
$destdir = $surveyDir."/images/";
if (!is_writeable($destdir)) {
$uploadresult = sprintf(gT("Incorrect permissions in your %s folder."), $destdir);
return Yii::app()->getController()->renderPartial(
'/admin/super/_renderJson',
array('data' => ['success' => $success, 'message' => $uploadresult, 'debug' => $debug]),
false,
false
);
}

$filename = sanitize_filename($_FILES['file']['name'], false, false, false); // Don't force lowercase or alphanumeric
$fullfilepath = $destdir.$filename;
$debug[] = $destdir;
$debug[] = $filename;
$debug[] = $fullfilepath;
if (!@move_uploaded_file($_FILES['file']['tmp_name'], $fullfilepath)) {
$uploadresult = gT("An error occurred uploading your file. This may be caused by incorrect permissions for the application /tmp folder.");
} else {
$uploadresult = sprintf(gT("File %s uploaded"), $filename);
$success = true;
};
return Yii::app()->getController()->renderPartial(
'/admin/super/_renderJson',
array('data' => ['success' => $success, 'message' => $uploadresult, 'debug' => $debug]),
false,
false
);



}
}
6 changes: 3 additions & 3 deletions application/controllers/admin/themeoptions.php
Expand Up @@ -112,7 +112,6 @@ public function updatesurvey($sid)
$this->getController()->redirect(Yii::app()->getController()->createUrl("/admin/themeoptions/sa/updatesurvey", ['surveyid'=>$sid, 'sid'=>$sid]));
}
}

$this->_updateCommon($model, $sid);
} else {
Yii::app()->setFlashMessage(gT("We are sorry but you don't have permissions to do this."), 'error');
Expand Down Expand Up @@ -343,8 +342,9 @@ private function _updateCommon(TemplateConfiguration $model, $sid = null)

Yii::app()->clientScript->registerPackage('bootstrap-switch', LSYii_ClientScript::POS_BEGIN);
$aData = array(
'model'=>$model,
'templateOptionPage' => $templateOptionPage
'model' => $model,
'templateOptionPage' => $templateOptionPage,
'sid' => $sid
);

if ($sid !== null) {
Expand Down
7 changes: 5 additions & 2 deletions application/controllers/admin/themes.php
Expand Up @@ -172,14 +172,17 @@ public function tmp($id)
*/
public function upload()
{
$action = returnGlobal('action');
if ($action == 'templateuploadimagefile' && Yii::app()->request->getPost('surveyid') ) {
Yii::app()->getController()->forward("/admin/survey/sa/uploadimagefile/");
Yii::app()->end();
}
if (Permission::model()->hasGlobalPermission('templates', 'import')) {
Yii::app()->loadHelper('admin/template');
$lid = returnGlobal('lid');
$action = returnGlobal('action');
$uploadresult = "";
$success = false;
$debug = [];

if ($action == 'templateuploadimagefile') {
// $iTemplateConfigurationId = Yii::app()->request->getPost('templateconfig');
// $oTemplateConfiguration = TemplateConfiguration::getInstanceFromConfigurationId($iTemplateConfigurationId);
Expand Down
50 changes: 31 additions & 19 deletions application/models/TemplateConfiguration.php
Expand Up @@ -785,20 +785,24 @@ private function _getRelativePath($from, $to) {
return str_repeat('..'.DIRECTORY_SEPARATOR, count($dir)) . implode(DIRECTORY_SEPARATOR, $file);
}

private function _filterImages($file)
/**
* Return image information
* @param string $file with Path
* @return array|null
*/
private function _getImageInfo($file)
{
$imagePath = (file_exists($this->filesPath.$file['name']))
? $this->filesPath.$file['name']
: $this->generalFilesPath.$file['name'] ;

$filePath = $this->_getRelativePath(Yii::app()->getConfig('rootdir'), $imagePath);

$previewFilePath = App()->getAssetManager()->publish($imagePath);

$checkImage = LSYii_ImageValidator::validateImage($imagePath);
if (!$checkImage['check'] === false) {
return ['preview' => $previewFilePath, 'filepath' => $filePath, 'filepathOptions' => $filePath ,'filename'=>$file['name']];
if(!file_exists($file)) {
return;
}
// Currently it's private and only used one time, before put this function in twig : must validate directory is inside rootdir
$checkImage = LSYii_ImageValidator::validateImage($file);
if (!$checkImage['check']) {
return;
}
$filePath = $this->_getRelativePath(Yii::app()->getConfig('rootdir'), $file);
$previewFilePath = App()->getAssetManager()->publish($file);
return ['preview' => $previewFilePath, 'filepath' => $filePath, 'filepathOptions' => $filePath ,'filename'=>basename($file)];
}

protected function getOptionPageAttributes()
Expand All @@ -807,13 +811,21 @@ protected function getOptionPageAttributes()
$fileList = array_merge(Template::getOtherFiles($this->filesPath), Template::getOtherFiles($this->generalFilesPath));
$aData['maxFileSize'] = getMaximumFileUploadSize();
$aData['imageFileList'] = [];
foreach ($fileList as $file) {
$isImage = $this->_filterImages($file);

if ($isImage) {
$aData['imageFileList'][] = $isImage;
}
};
$categoryList = []; // Array with optgroup label and path
$categoryList[] = ['group' => gT("Global"),'path' => $this->generalFilesPath];
$categoryList[] = ['group' => gT("Theme"),'path' => $this->filesPath];
if($this->sid) {
$categoryList[] = ['group' => gT("Survey"),'path' => Yii::app()->getConfig('uploaddir').'/surveys/'.$this->sid.'/images/'];
}
foreach($categoryList as $category) {
$fileList = Template::getOtherFiles($category['path']);
foreach ($fileList as $file) {
$imageInfo = $this->_getImageInfo($category['path'].$file['name']);
if ($imageInfo) {
$aData['imageFileList'][] = array_merge($category,$imageInfo);
}
};
}

return $aData;
}
Expand Down
12 changes: 11 additions & 1 deletion themes/survey/bootswatch/options/options.twig
Expand Up @@ -205,9 +205,19 @@
<option value="inherit">Inherit</option>
{% endif %}

{% set optgroup = '' %}
{% for image in templateConfiguration.imageFileList %}
<option data-lightbox-src="{{image.preview}}" value="{{image.filepath}}">{{image.filename}}</option>
{% if image.group != optgroup %}
{% if optgroup != '' %}
</optgroup>
{% endif %}
<optgroup label="{{image.group}}">
{% set optgroup = image.group %}
{% endif %}
<option data-lightbox-src="{{image.preview}}" value="{{image.filepath}}">{{image.filename}}</option>
{% endfor %}
</optgroup>

</select>
</div>
</div>
Expand Down
26 changes: 22 additions & 4 deletions themes/survey/fruity/options/options.twig
Expand Up @@ -706,9 +706,19 @@
<option value="inherit">{{ gT("Inherit") }}</option>
{% endif %}

{% set optgroup = '' %}
{% for image in templateConfiguration.imageFileList %}
<option data-lightbox-src="{{image.preview}}" value="{{image.filepath}}">{{image.filename}}</option>
{% if image.group != optgroup %}
{% if optgroup != '' %}
</optgroup>
{% endif %}
<optgroup label="{{image.group}}">
{% set optgroup = image.group %}
{% endif %}
<option data-lightbox-src="{{image.preview}}" value="{{image.filepath}}">{{image.filename}}</option>
{% endfor %}
</optgroup>

</select>
</div>
</div>
Expand Down Expand Up @@ -751,12 +761,20 @@
<div class='col-sm-12'>
<select class='form-control selector_option_value_field selector_radio_childfield selector_image_selector' data-parent="brandlogo" id='simple_edit_options_brandlogofile' name='brandlogofile'>
{% if templateConfiguration.sid is not empty or templateConfiguration.gsid is not empty %}
<option value="inherit">{{ gT("Inherit") }}</option>
<option value="inherit">Inherit</option>
{% endif %}

{% set optgroup = '' %}
{% for image in templateConfiguration.imageFileList %}
<option data-lightbox-src="{{image.preview}}" value="{{image.filepath}}">{{image.filename}}</option>
{% if image.group != optgroup %}
{% if optgroup != '' %}
</optgroup>
{% endif %}
<optgroup label="{{image.group}}">
{% set optgroup = image.group %}
{% endif %}
<option data-lightbox-src="{{image.preview}}" value="{{image.filepath}}">{{image.filename}}</option>
{% endfor %}
</optgroup>
</select>
</div>
</div>
Expand Down
14 changes: 11 additions & 3 deletions themes/survey/vanilla/options/options.twig
Expand Up @@ -244,10 +244,18 @@
{% if templateConfiguration.sid is not empty or templateConfiguration.gsid is not empty %}
<option value="inherit">Inherit</option>
{% endif %}

{% set optgroup = '' %}
{% for image in templateConfiguration.imageFileList %}
<option data-lightbox-src="{{image.preview}}" value="{{image.filepath}}">{{image.filename}}</option>
{% if image.group != optgroup %}
{% if optgroup != '' %}
</optgroup>
{% endif %}
<optgroup label="{{image.group}}">
{% set optgroup = image.group %}
{% endif %}
<option data-lightbox-src="{{image.preview}}" value="{{image.filepath}}">{{image.filename}}</option>
{% endfor %}
</optgroup>
</select>
</div>
</div>
Expand Down Expand Up @@ -303,7 +311,7 @@
{{ gT("Upload") }}
</label>
</span>

<input type='hidden' name='surveyid' value='{{ templateConfiguration.sid }}' />
<input type='hidden' name='templatename' value='{{ templateConfiguration.template_name }}' />
<input type='hidden' name='templateconfig' value='{{ templateConfiguration.id }}' />
<input type='hidden' name='action' value='templateuploadimagefile' />
Expand Down

0 comments on commit a2bbf64

Please sign in to comment.