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(); $form = new Application_Form_SupportSettings();
if ($request->isPost()) { if ($request->isPost()) {
$values = $request->getPost(); $values = $request->getPost();
if ($form->isValid($values)) { if ($form->isValid($values)) {
Application_Model_Preference::SetHeadTitle($values["stationName"], $this->view); Application_Model_Preference::SetHeadTitle($values["stationName"], $this->view);
Application_Model_Preference::SetPhone($values["Phone"]); Application_Model_Preference::SetPhone($values["Phone"]);
Application_Model_Preference::SetEmail($values["Email"]); Application_Model_Preference::SetEmail($values["Email"]);

View File

@ -1,5 +1,8 @@
<?php <?php
$filepath = realpath (dirname(__FILE__));
require_once($filepath."/../modules/rest/controllers/MediaController.php");
class ScheduleController extends Zend_Controller_Action class ScheduleController extends Zend_Controller_Action
{ {
@ -553,7 +556,6 @@ class ScheduleController extends Zend_Controller_Action
public function addShowAction() public function addShowAction()
{ {
$service_showForm = new Application_Service_ShowFormService(null); $service_showForm = new Application_Service_ShowFormService(null);
//$service_show = new Application_Service_ShowService();
$js = $this->_getParam('data'); $js = $this->_getParam('data');
$data = array(); $data = array();
@ -566,8 +568,8 @@ class ScheduleController extends Zend_Controller_Action
$service_show = new Application_Service_ShowService(null, $data); $service_show = new Application_Service_ShowService(null, $data);
// TODO: move this to js // TODO: move this to js
$data['add_show_hosts'] = $this->_getParam('hosts'); $data['add_show_hosts'] = $this->_getParam('hosts');
$data['add_show_day_check'] = $this->_getParam('days'); $data['add_show_day_check'] = $this->_getParam('days');
if ($data['add_show_day_check'] == "") { if ($data['add_show_day_check'] == "") {
$data['add_show_day_check'] = null; $data['add_show_day_check'] = null;
@ -580,13 +582,17 @@ class ScheduleController extends Zend_Controller_Action
$log_vars["params"]["form_data"] = $data; $log_vars["params"]["form_data"] = $data;
Logging::info($log_vars); Logging::info($log_vars);
$request = $this->getRequest();
Logging::info($request);
$forms = $this->createShowFormAction(); $forms = $this->createShowFormAction();
$this->view->addNewShow = true; $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)) { if ($service_showForm->validateShowForms($forms, $data)) {
$service_show->addUpdateShow($data); $service_show->addUpdateShow($data);
@ -596,11 +602,25 @@ class ScheduleController extends Zend_Controller_Action
Logging::debug("Show creation succeeded"); Logging::debug("Show creation succeeded");
} else { } else {
// re-add the element
$forms["style"]->addElement($upload);
$this->view->form = $this->view->render('schedule/add-show-form.phtml'); $this->view->form = $this->view->render('schedule/add-show-form.phtml');
Logging::debug("Show creation failed"); 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) public function createShowFormAction($populateDefaults=false)
{ {
$service_showForm = new Application_Service_ShowFormService(); $service_showForm = new Application_Service_ShowFormService();
@ -682,7 +702,7 @@ class ScheduleController extends Zend_Controller_Action
$paramsPop = str_replace('#id#', $id, $params); $paramsPop = str_replace('#id#', $id, $params);
// added for downlaod // added for download
$id = $this->_getParam('id'); $id = $this->_getParam('id');
$file_id = $this->_getParam('id', null); $file_id = $this->_getParam('id', null);
@ -707,7 +727,7 @@ class ScheduleController extends Zend_Controller_Action
Application_Model_Preference::SetCalendarTimeScale($this->_getParam('timeScale')); Application_Model_Preference::SetCalendarTimeScale($this->_getParam('timeScale'));
} }
/** /**
* Sets the user specific preference for which time interval to use in Calendar. * Sets the user specific preference for which time interval to use in Calendar.
* This is only being used by schedule.js at the moment. * 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); $this->_helper->json->sendJson($localTime);
} }
} }

View File

@ -46,7 +46,6 @@ class Application_Form_AddShowStyle extends Zend_Form_SubForm
// Add show image input // Add show image input
$fileCountValidator = Application_Form_Helper_ValidationTypes::overrideFileCountValidator(1); $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'); $fileExtensionValidator = Application_Form_Helper_ValidationTypes::overrideFileExtensionValidator('jpg,png,gif');
$upload = new Zend_Form_Element_File('upload'); $upload = new Zend_Form_Element_File('upload');
@ -58,31 +57,13 @@ class Application_Form_AddShowStyle extends Zend_Form_SubForm
'class' => 'big', 'class' => 'big',
'placement' => false 'placement' => false
)))) ))))
->addValidator('Count', false, 1) ->addValidators(array(
->addValidator('Extension', false, 'jpg,jpeg,png,gif') $fileCountValidator,
$fileExtensionValidator
))
->addFilter('ImageSize'); ->addFilter('ImageSize');
$this->addElement($upload); $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() public function disable()

View File

@ -105,18 +105,6 @@ Class Application_Form_Helper_ValidationTypes {
return $validator; 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) public static function overrideFileExtensionValidator($p_fileExtensions)
{ {
$validator = new Zend_Validate_File_Extension($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").length == 1) {
if( ($("#add-show-form").css('display')=='none')) { if( ($("#add-show-form").css('display')=='none')) {
$("#add-show-form").show(); $("#add-show-form").show();
$("#add-show-form").prop("enctype", "multipart/form-data"); $("#upload").prop("accept", "image/*");
/* /*
var windowWidth = $(window).width(); 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) { onChange: function (hsb, hex, rgb, el) {
$(el).val(hex); $(el).val(hex);
}, },
@ -631,36 +631,47 @@ function setAddShowEvents(form) {
var end_date = $("#add_show_end_date").val(); var end_date = $("#add_show_end_date").val();
var action = baseUrl+"Schedule/"+String(addShowButton.attr("data-action")); var action = baseUrl+"Schedule/"+String(addShowButton.attr("data-action"));
$.post(action, {format: "json", data: data, hosts: hosts, days: days}, function(json){ var image = new FormData();
image.append('show-image', $('#upload')[0].files[0]);
$('#schedule-add-show').unblock(); $.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'
});
var $addShowForm = $("#add-show-form"); $('#schedule-add-show').unblock();
if (json.form) { var $addShowForm = $("#add-show-form");
redrawAddShowForm($addShowForm, json.form); if (json.form) {
$("#add_show_end_date").val(end_date); redrawAddShowForm($addShowForm, json.form);
$("#add_show_start_date").val(start_date);
showErrorSections();
}
else if (json.edit) {
$("#schedule_calendar").removeAttr("style") $("#add_show_end_date").val(end_date);
.fullCalendar('render'); $("#add_show_start_date").val(start_date);
showErrorSections();
} else if (json.edit) {
$("#schedule_calendar").removeAttr("style")
.fullCalendar('render');
$addShowForm.hide(); $addShowForm.hide();
$.get(baseUrl+"Schedule/get-form", {format:"json"}, function(json){ $.get(baseUrl+"Schedule/get-form", {format:"json"}, function(json){
redrawAddShowForm($addShowForm, json.form); redrawAddShowForm($addShowForm, json.form);
}); });
makeAddShowButton(); makeAddShowButton();
} } else {
else { redrawAddShowForm($addShowForm, json.newForm);
scheduleRefetchEvents(json);
redrawAddShowForm($addShowForm, json.newForm); }
scheduleRefetchEvents(json); }
}
}); });
}); });