More work on show image uploads

This commit is contained in:
Duncan Sommerville 2014-09-16 18:03:14 -04:00
parent cb80423fdd
commit 5434311f7d
8 changed files with 80 additions and 79 deletions

View File

@ -84,7 +84,7 @@ class PreferenceController extends Zend_Controller_Action
$form = new Application_Form_SupportSettings();
if ($request->isPost()) {
$values = $request->getPost();
if ($form->isValid($values)) {
if ($form->isValid($values)) {
Application_Model_Preference::SetHeadTitle($values["stationName"], $this->view);
Application_Model_Preference::SetPhone($values["Phone"]);
Application_Model_Preference::SetEmail($values["Email"]);

View File

@ -1,5 +1,8 @@
<?php
$filepath = realpath (dirname(__FILE__));
require_once($filepath."/../modules/rest/controllers/MediaController.php");
class ScheduleController extends Zend_Controller_Action
{
@ -553,7 +556,6 @@ class ScheduleController extends Zend_Controller_Action
public function addShowAction()
{
$service_showForm = new Application_Service_ShowFormService(null);
//$service_show = new Application_Service_ShowService();
$js = $this->_getParam('data');
$data = array();
@ -566,9 +568,9 @@ class ScheduleController extends Zend_Controller_Action
$service_show = new Application_Service_ShowService(null, $data);
// TODO: move this to js
$data['add_show_hosts'] = $this->_getParam('hosts');
$data['add_show_day_check'] = $this->_getParam('days');
$data['add_show_hosts'] = $this->_getParam('hosts');
$data['add_show_day_check'] = $this->_getParam('days');
if ($data['add_show_day_check'] == "") {
$data['add_show_day_check'] = null;
}
@ -580,13 +582,17 @@ class ScheduleController extends Zend_Controller_Action
$log_vars["params"]["form_data"] = $data;
Logging::info($log_vars);
$request = $this->getRequest();
Logging::info($request);
$forms = $this->createShowFormAction();
$this->view->addNewShow = true;
/*
* hack to prevent validating the file upload field since it
* isn't passed into $data
*/
$upload = $forms["style"]->getElement("upload");
$forms["style"]->removeElement("upload");
if ($service_showForm->validateShowForms($forms, $data)) {
$service_show->addUpdateShow($data);
@ -596,11 +602,25 @@ class ScheduleController extends Zend_Controller_Action
Logging::debug("Show creation succeeded");
} else {
// re-add the element
$forms["style"]->addElement($upload);
$this->view->form = $this->view->render('schedule/add-show-form.phtml');
Logging::debug("Show creation failed");
}
}
/**
* Since the form is being submitted via jQuery, this function accepts
* a second AJAX request and writes the file (sent as a FormData object)
*/
public function uploadImageAction()
{
Rest_MediaController::processUploadedImage(
$_FILES["show-image"]["tmp-name"],
$_FILES["show-image"]["name"]);
}
public function createShowFormAction($populateDefaults=false)
{
$service_showForm = new Application_Service_ShowFormService();
@ -682,7 +702,7 @@ class ScheduleController extends Zend_Controller_Action
$paramsPop = str_replace('#id#', $id, $params);
// added for downlaod
// added for download
$id = $this->_getParam('id');
$file_id = $this->_getParam('id', null);
@ -707,7 +727,7 @@ class ScheduleController extends Zend_Controller_Action
Application_Model_Preference::SetCalendarTimeScale($this->_getParam('timeScale'));
}
/**
/**
* Sets the user specific preference for which time interval to use in Calendar.
* This is only being used by schedule.js at the moment.
*/
@ -753,4 +773,5 @@ class ScheduleController extends Zend_Controller_Action
$this->_helper->json->sendJson($localTime);
}
}

View File

@ -46,7 +46,6 @@ class Application_Form_AddShowStyle extends Zend_Form_SubForm
// Add show image input
$fileCountValidator = Application_Form_Helper_ValidationTypes::overrideFileCountValidator(1);
$fileSizeValidator = Application_Form_Helper_ValidationTypes::overrideFileSizeValidator(array('max' => '5120000'));
$fileExtensionValidator = Application_Form_Helper_ValidationTypes::overrideFileExtensionValidator('jpg,png,gif');
$upload = new Zend_Form_Element_File('upload');
@ -58,31 +57,13 @@ class Application_Form_AddShowStyle extends Zend_Form_SubForm
'class' => 'big',
'placement' => false
))))
->addValidator('Count', false, 1)
->addValidator('Extension', false, 'jpg,jpeg,png,gif')
->addValidators(array(
$fileCountValidator,
$fileExtensionValidator
))
->addFilter('ImageSize');
$this->addElement($upload);
// $this->addElement('file', 'add_show_image', array(
// 'disableLoadDefaultDecorators' => true,
// 'decorators' => array('File', array('ViewScript', array(
// 'viewScript' => 'form/add-show-style.phtml',
// 'class' => 'big',
// 'placement' => false
// ))),
// 'label' => _('Show Image:'),
// 'class' => 'input_file',
// 'required' => false,
// 'validators' => array(
// $fileCountValidator,
// $fileSizeValidator,
// $fileExtensionValidator),
// 'destination' => '../public/images/upload',
// 'method' => 'post'
// ));
// Change form enctype to accommodate file upload
$this->setEnctype(Zend_Form::ENCTYPE_MULTIPART);
}
public function disable()

View File

@ -105,18 +105,6 @@ Class Application_Form_Helper_ValidationTypes {
return $validator;
}
public static function overrideFileSizeValidator($p_fileSize)
{
$validator = new Zend_Validate_File_Size($p_fileSize);
$validator->setMessage(
_("The uploaded file is too large. Please limit your upload to '%max%'"),
Zend_Validate_File_Size::TOO_BIG
);
return $validator;
}
public static function overrideFileExtensionValidator($p_fileExtensions)
{
$validator = new Zend_Validate_File_Extension($p_fileExtensions);

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 939 B

View File

@ -8,7 +8,7 @@ function openAddShowForm() {
if($("#add-show-form").length == 1) {
if( ($("#add-show-form").css('display')=='none')) {
$("#add-show-form").show();
$("#add-show-form").prop("enctype", "multipart/form-data");
$("#upload").prop("accept", "image/*");
/*
var windowWidth = $(window).width();
@ -581,7 +581,7 @@ function setAddShowEvents(form) {
}
})
form.find("#schedule-show-style input .input_text").ColorPicker({
form.find("#schedule-show-style .input_text").ColorPicker({
onChange: function (hsb, hex, rgb, el) {
$(el).val(hex);
},
@ -630,37 +630,48 @@ function setAddShowEvents(form) {
var start_date = $("#add_show_start_date").val();
var end_date = $("#add_show_end_date").val();
var action = baseUrl+"Schedule/"+String(addShowButton.attr("data-action"));
var image = new FormData();
image.append('show-image', $('#upload')[0].files[0]);
$.post(action, {format: "json", data: data, hosts: hosts, days: days}, function(json){
$('#schedule-add-show').unblock();
var $addShowForm = $("#add-show-form");
if (json.form) {
redrawAddShowForm($addShowForm, json.form);
$.ajax({
url: action,
data: {format: "json", data: data, hosts: hosts, days: days},
success: function(json) {
$.ajax({
url: '/Schedule/upload-image',
data: image,
cache: false,
contentType: false,
processData: false,
type: 'POST'
});
$("#add_show_end_date").val(end_date);
$("#add_show_start_date").val(start_date);
showErrorSections();
}
else if (json.edit) {
$("#schedule_calendar").removeAttr("style")
.fullCalendar('render');
$addShowForm.hide();
$.get(baseUrl+"Schedule/get-form", {format:"json"}, function(json){
redrawAddShowForm($addShowForm, json.form);
});
makeAddShowButton();
}
else {
redrawAddShowForm($addShowForm, json.newForm);
scheduleRefetchEvents(json);
}
$('#schedule-add-show').unblock();
var $addShowForm = $("#add-show-form");
if (json.form) {
redrawAddShowForm($addShowForm, json.form);
$("#add_show_end_date").val(end_date);
$("#add_show_start_date").val(start_date);
showErrorSections();
} else if (json.edit) {
$("#schedule_calendar").removeAttr("style")
.fullCalendar('render');
$addShowForm.hide();
$.get(baseUrl+"Schedule/get-form", {format:"json"}, function(json){
redrawAddShowForm($addShowForm, json.form);
});
makeAddShowButton();
} else {
redrawAddShowForm($addShowForm, json.newForm);
scheduleRefetchEvents(json);
}
}
});
});