Skip to content

Commit

Permalink
Dev : use flash message rather than controller error
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisGac committed Nov 27, 2015
1 parent bee9b84 commit c0da7df
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 35 deletions.
72 changes: 56 additions & 16 deletions application/controllers/admin/templates.php
Expand Up @@ -99,14 +99,17 @@ public function upload()
{
die('No permission');
}
//$aViewUrls = $this->_initialise('default', 'welcome', 'startpage.pstpl', FALSE);

$lid = returnGlobal('lid');
$action = returnGlobal('action');

if ($action == 'templateupload')
{
if (Yii::app()->getConfig('demoMode'))
$this->getController()->error(gT("Demo mode: Uploading templates is disabled."));
{
Yii::app()->session['flashmessage'] = gT("Demo mode: Uploading templates is disabled.");
$this->getController()->redirect(array("admin/templates/sa/upload"));
}

Yii::app()->loadLibrary('admin.pclzip');

Expand All @@ -117,12 +120,18 @@ public function upload()
$destdir = Yii::app()->getConfig('usertemplaterootdir').DIRECTORY_SEPARATOR.$sNewDirectoryName;

if (!is_writeable(dirname($destdir)))
$this->getController()->error(sprintf(gT("Incorrect permissions in your %s folder."), dirname($destdir)));
{
Yii::app()->session['flashmessage'] = sprintf(gT("Incorrect permissions in your %s folder."), dirname($destdir));
$this->getController()->redirect(array("admin/templates/sa/upload"));
}

if (!is_dir($destdir))
mkdir($destdir);
else
$this->getController()->error(sprintf(gT("Template '%s' does already exist."), $sNewDirectoryName));
{
Yii::app()->session['flashmessage'] = sprintf(gT("Template '%s' does already exist."), $sNewDirectoryName);
$this->getController()->redirect(array("admin/templates/sa/upload"));
}

$aImportedFilesInfo = array();
$aErrorFilesInfo = array();
Expand All @@ -134,8 +143,9 @@ public function upload()
$aExtractResult=$zip->extract(PCLZIP_OPT_PATH, $destdir, PCLZIP_CB_PRE_EXTRACT, 'templateExtractFilter');
if ($aExtractResult==0)
{
$this->getController()->error(gT("This file is not a valid ZIP file archive. Import failed."));

//$this->getController()->error(gT("This file is not a valid ZIP file archive. Import failed."));
Yii::app()->session['flashmessage'] = gT("This file is not a valid ZIP file archive. Import failed.");
$this->getController()->redirect(array("admin/templates/sa/upload"));
}
else
{
Expand All @@ -151,10 +161,15 @@ public function upload()
}

if (count($aErrorFilesInfo) == 0 && count($aImportedFilesInfo) == 0)
$this->getController()->error(gT("This ZIP archive contains no valid template files. Import failed."));
Yii::app()->session['flashmessage'] = gT("This ZIP archive contains no valid template files. Import failed.");
$this->getController()->redirect(array("admin/templates/sa/upload"));
}
else
$this->getController()->error(sprintf(gT("An error occurred uploading your file. This may be caused by incorrect permissions in your %s folder."), $basedestdir));
{
Yii::app()->session['flashmessage'] = sprintf(gT("An error occurred uploading your file. This may be caused by incorrect permissions in your %s folder."), $basedestdir);
$this->getController()->redirect(array("admin/templates/sa/upload"));
}



if (count($aImportedFilesInfo) > 0)
Expand Down Expand Up @@ -440,11 +455,20 @@ public function templaterename()
$sNewDirectoryPath = Yii::app()->getConfig('usertemplaterootdir') . "/" . $sNewName;
$sOldDirectoryPath = Yii::app()->getConfig('usertemplaterootdir') . "/" . returnGlobal('copydir');
if (isStandardTemplate(returnGlobal('newname')))
$this->getController()->error(sprintf(gT("Template could not be renamed to `%s`.", "js"), $sNewName) . " " . gT("This name is reserved for standard template.", "js"));
{
Yii::app()->session['flashmessage'] = sprintf(gT("Template could not be renamed to `%s`.", "js"), $sNewName) . " " . gT("This name is reserved for standard template.", "js");
$this->getController()->redirect(array("admin/templates/sa/upload"));
}
elseif (file_exists($sNewDirectoryPath))
$this->getController()->error(sprintf(gT("Template could not be renamed to `%s`.", "js"), $sNewName) . " " . gT("A template with that name already exists.", "js"));
{
Yii::app()->session['flashmessage'] = sprintf(gT("Template could not be renamed to `%s`.", "js"), $sNewName) . " " . gT("A template with that name already exists.", "js");
$this->getController()->redirect(array("admin/templates/sa/upload"));
}
elseif (rename($sOldDirectoryPath, $sNewDirectoryPath) == false)
$this->getController()->error(sprintf(gT("Template could not be renamed to `%s`.", "js"), $sNewName) . " " . gT("Maybe you don't have permission.", "js"));
{
Yii::app()->session['flashmessage'] = sprintf(gT("Template could not be renamed to `%s`.", "js"), $sNewName) . " " . gT("Maybe you don't have permission.", "js");
$this->getController()->redirect(array("admin/templates/sa/upload"));
}
else
{
Survey::model()->updateAll(array( 'template' => $sNewName ), "template = :oldname", array(':oldname'=>$sOldName));
Expand Down Expand Up @@ -580,20 +604,33 @@ public function templatesavechanges()
if (multiarray_search($files, 'name', $editfile) === false &&
multiarray_search($cssfiles, 'name', $editfile) === false
)
$this->getController()->error('Invalid template name');
{
Yii::app()->session['flashmessage'] = gT('Invalid template name');
$this->getController()->redirect(array("admin/templates/sa/upload"));
}

$savefilename = Yii::app()->getConfig('usertemplaterootdir') . "/" . $templatename . "/" . $editfile;
if (is_writable($savefilename)) {
if (!$handle = fopen($savefilename, 'w'))
$this->getController()->error('Could not open file ' . $savefilename);
{
Yii::app()->session['flashmessage'] = gT('Could not open file '). $savefilename;
$this->getController()->redirect(array("admin/templates/sa/upload"));
}

if (!fwrite($handle, $changedtext))
$this->getController()->error('Could not write file ' . $savefilename);
{
Yii::app()->session['flashmessage'] = gT('Could not write file '). $savefilename;
$this->getController()->redirect(array("admin/templates/sa/upload"));
}

fclose($handle);
}
else
$this->getController()->error("The file $savefilename is not writable");
{
Yii::app()->session['flashmessage'] = "The file $savefilename is not writable";
$this->getController()->redirect(array("admin/templates/sa/upload"));
}

}
}

Expand Down Expand Up @@ -867,7 +904,10 @@ protected function _initialise($templatename, $screenname, $editfile, $showsumma

// Checks if screen name is in the list of allowed screen names
if (multiarray_search($screens, 'id', $screenname) === false)
$this->getController()->error('Invalid screen name');
{
Yii::app()->session['flashmessage'] = gT('Invalid screen name');
$this->getController()->redirect(array("admin/templates/sa/upload"));
}

if (!isset($action))
$action = sanitize_paranoid_string(returnGlobal('action'));
Expand Down
37 changes: 18 additions & 19 deletions application/views/admin/templates/importform_view.php
@@ -1,20 +1,19 @@
<div class='header ui-widget-header'><?php eT("Uploaded template file") ?></div>
<?php echo CHtml::form(array('admin/templates/sa/upload'), 'post', array('id'=>'importtemplate', 'name'=>'importtemplate', 'enctype'=>'multipart/form-data', 'onsubmit'=>'return validatefilename(this,"'.gT('Please select a file to import!', 'js').'");')); ?>

<input type='hidden' name='lid' value='$lid' />
<input type='hidden' name='action' value='templateupload' />
<ul>
<li>
<label for='the_file'><?php eT("Select template ZIP file:") ?></label>
<input id='the_file' name='the_file' type="file" />
</li>
<li>
<label>&nbsp;</label>
<?php if (!function_exists("zip_open")) {?>
<?php eT("zip library not supported by PHP, Import ZIP Disabled", "js") ?>
<?php } else {?>
<input type='button' value='<?php eT("Import template ZIP archive") ?>' onclick='if (validatefilename(this.form,"<?php eT('Please select a file to import!', 'js') ?>")) { this.form.submit();}' />
<?php }?>
</li>
</ul>
</form>
<h3 class="pagetitle"><?php eT("Uploaded template file") ?></h3>
<?php echo CHtml::form(array('admin/templates/sa/upload'), 'post', array('id'=>'importtemplate', 'name'=>'importtemplate', 'enctype'=>'multipart/form-data', 'onsubmit'=>'return validatefilename(this,"'.gT('Please select a file to import!', 'js').'");')); ?>
<input type='hidden' name='lid' value='$lid' />
<input type='hidden' name='action' value='templateupload' />
<div class="form-group">
<label for='the_file'><?php eT("Select template ZIP file:") ?></label>
<input id='the_file' name='the_file' type="file" />
</div>
<div class="form-group">

<?php if (!function_exists("zip_open")) {?>
<?php eT("zip library not supported by PHP, Import ZIP Disabled", "js") ?>
<?php } else {?>
<input class="btn btn-default" type='button' value='<?php eT("Import template ZIP archive") ?>' onclick='if (validatefilename(this.form,"<?php eT('Please select a file to import!', 'js') ?>")) { this.form.submit();}' />
<?php }?>
</div>

</form>

0 comments on commit c0da7df

Please sign in to comment.