Skip to content

Commit

Permalink
Converted to PSR-2
Browse files Browse the repository at this point in the history
  • Loading branch information
helpfulrobot committed Dec 17, 2015
1 parent 5e5f858 commit 0b52f95
Show file tree
Hide file tree
Showing 2 changed files with 233 additions and 221 deletions.
345 changes: 177 additions & 168 deletions code/AjaxFileField.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,67 @@
* Similar to FileIFrameField, except it allows XHR uploads via the file-uploader javascript.
*
*/
class AjaxFileField extends FileField{

private static $allowed_actions = array(
'save',
'saveXHR'
);
class AjaxFileField extends FileField
{

private static $allowed_actions = array(
'save',
'saveXHR'
);

protected $buttonClasses,$config = array();
protected $buttonClasses,$config = array();

protected $sessionstoreid;

public function addButtonClass($class){
$this->buttonClasses[] = $class;
}

/**
* Set or override specific configs
* see thirdparty/valums/reademe.md
*/
public function setConfig($config = array()){
$this->config = $config;
}
protected $sessionstoreid;

public function addButtonClass($class)
{
$this->buttonClasses[] = $class;
}

/**
* Set or override specific configs
* see thirdparty/valums/reademe.md
*/
public function setConfig($config = array())
{
$this->config = $config;
}

public function setSessionStoreId($id) {
$this->sessionstoreid = $id;
public function setSessionStoreId($id)
{
$this->sessionstoreid = $id;

return $this;
}

public function Field($properties = array()){
Requirements::javascript('ajaxuploadfield/thirdparty/valums/client/fileuploader.min.js','fileuploader');

//configure javascript
$htmlid = $this->XML_val('Name')."_uploader";
$thislink = $this->Link('save');
$options = array(
'action' => $thislink,
'multiple' => false, //prevent multiple file uploads
);
$allowedextensions = $this->getValidator()->getAllowedExtensions();
if(is_array($allowedextensions)){
$options['allowedExtensions'] = $allowedextensions;
}
if($maxfilesize = $this->getValidator()->getAllowedMaxFileSize()){
$options['sizeLimit'] = $maxfilesize;
}
if(Director::isDev()){
$options['debug'] = true;
}
$options = array_merge($options,$this->config);
$encodedoptions = json_encode($options);
$extraclasses = count($this->buttonClasses) ? 'class=\"'.implode(" ",$this->buttonClasses).'\"' : "";
$replacementhtml = '<span id=\"'.$htmlid.'\"><input type=\"submit\" '.$extraclasses.' value=\"'.$this->title.'\" /></span>';

//store globally reachable js reference, to allow later customisations
$script =<<<JS
return $this;
}

public function Field($properties = array())
{
Requirements::javascript('ajaxuploadfield/thirdparty/valums/client/fileuploader.min.js', 'fileuploader');

//configure javascript
$htmlid = $this->XML_val('Name')."_uploader";
$thislink = $this->Link('save');
$options = array(
'action' => $thislink,
'multiple' => false, //prevent multiple file uploads
);
$allowedextensions = $this->getValidator()->getAllowedExtensions();
if (is_array($allowedextensions)) {
$options['allowedExtensions'] = $allowedextensions;
}
if ($maxfilesize = $this->getValidator()->getAllowedMaxFileSize()) {
$options['sizeLimit'] = $maxfilesize;
}
if (Director::isDev()) {
$options['debug'] = true;
}
$options = array_merge($options, $this->config);
$encodedoptions = json_encode($options);
$extraclasses = count($this->buttonClasses) ? 'class=\"'.implode(" ", $this->buttonClasses).'\"' : "";
$replacementhtml = '<span id=\"'.$htmlid.'\"><input type=\"submit\" '.$extraclasses.' value=\"'.$this->title.'\" /></span>';

//store globally reachable js reference, to allow later customisations
$script =<<<JS
qq.instances = qq.instances ? qq.instances : {};
$("#$htmlid").html("$replacementhtml").each(function(){
var el = $(this);
Expand All @@ -71,126 +76,130 @@ public function Field($properties = array()){
qq.instances['$htmlid'] = uploader;
});
JS;

Requirements::customScript($script,'uploader'.$this->id());

if($this->form){
$record = $this->form->getRecord();
}
$fieldName = $this->name;
if(isset($record) && $record) {
$imageField = $record->$fieldName();
} else {
$imageField = "";
}

$html = "<div id=\"$htmlid\">";
if($imageField && $imageField->exists()) {
$html .= '<div class="thumbnail">';
if($imageField->hasMethod('Thumbnail') && $imageField->Thumbnail()) {
$html .= "<img src=\"".$imageField->Thumbnail()->getURL()."\" />";
} else if($imageField->CMSThumbnail()) {
$html .= "<img src=\"".$imageField->CMSThumbnail()->getURL()."\" />";
}
$html .= '</div>';
}
$html .= $this->createTag("input",
array(
"type" => "file",
"name" => $this->name,
"id" => $this->id(),
'disabled' => $this->disabled
)
);
$html .= $this->createTag("input",
array(
"type" => "hidden",
"name" => "MAX_FILE_SIZE",
"value" => $maxfilesize
)
);
$html .= "</div>";
return $html;
}

/**
* Saves uplaoded image into file.
* The function can handle three different ways of uploading an image: XHR, iframe, form post,
* where the first two are done via the fileuploader script.
*
* A dummy file array is created for Upload to properly process.
*
*/
public function save($data = null, $form = null) {
$json = array();
$this->upload->setValidator(new XHRUpload_Validator()); //hack solution to allow XHR uploads
$fileparts = null;

Requirements::customScript($script, 'uploader'.$this->id());

if ($this->form) {
$record = $this->form->getRecord();
}
$fieldName = $this->name;
if (isset($record) && $record) {
$imageField = $record->$fieldName();
} else {
$imageField = "";
}

$html = "<div id=\"$htmlid\">";
if ($imageField && $imageField->exists()) {
$html .= '<div class="thumbnail">';
if ($imageField->hasMethod('Thumbnail') && $imageField->Thumbnail()) {
$html .= "<img src=\"".$imageField->Thumbnail()->getURL()."\" />";
} elseif ($imageField->CMSThumbnail()) {
$html .= "<img src=\"".$imageField->CMSThumbnail()->getURL()."\" />";
}
$html .= '</div>';
}
$html .= $this->createTag("input",
array(
"type" => "file",
"name" => $this->name,
"id" => $this->id(),
'disabled' => $this->disabled
)
);
$html .= $this->createTag("input",
array(
"type" => "hidden",
"name" => "MAX_FILE_SIZE",
"value" => $maxfilesize
)
);
$html .= "</div>";
return $html;
}

/**
* Saves uplaoded image into file.
* The function can handle three different ways of uploading an image: XHR, iframe, form post,
* where the first two are done via the fileuploader script.
*
* A dummy file array is created for Upload to properly process.
*
*/
public function save($data = null, $form = null)
{
$json = array();
$this->upload->setValidator(new XHRUpload_Validator()); //hack solution to allow XHR uploads
$fileparts = null;

if (isset($_GET['qqfile'])) {
$fileparts = $this->saveXHR($_GET['qqfile']);
} elseif (isset($_FILES['qqfile'])) { //TODO: this could probably be replaced by setting the field name in javascript
$fileparts = $_FILES['qqfile'];
} elseif(isset($_FILES[$this->Name()])) {
$fileparts = $_FILES[$this->Name()];
}
if(!$fileparts){
return $this->returnJSON(array("error","No file was uploaded."));
}
//create database entry for image
// $desiredClass = $this->dataClass();
$desiredClass = "Image"; //TODO: temp - make a subclass, or handle image uploads
$fileObject = Object::create($desiredClass);
$this->upload->loadIntoFile($fileparts, $fileObject, $this->folderName);
if (isset($_GET['qqfile'])) {
$fileparts = $this->saveXHR($_GET['qqfile']);
} elseif (isset($_FILES['qqfile'])) { //TODO: this could probably be replaced by setting the field name in javascript
$fileparts = $_FILES['qqfile'];
} elseif (isset($_FILES[$this->Name()])) {
$fileparts = $_FILES[$this->Name()];
}
if (!$fileparts) {
return $this->returnJSON(array("error", "No file was uploaded."));
}
//create database entry for image
// $desiredClass = $this->dataClass();
$desiredClass = "Image"; //TODO: temp - make a subclass, or handle image uploads
$fileObject = Object::create($desiredClass);
$this->upload->loadIntoFile($fileparts, $fileObject, $this->folderName);

if($this->upload->isError()){
$errors = $this->upload->getErrors();
$json = array('error',implode(",",$errors));
return $this->returnJSON($json);
}
if ($this->upload->isError()) {
$errors = $this->upload->getErrors();
$json = array('error',implode(",", $errors));
return $this->returnJSON($json);
}

$file = $this->upload->getFile();
if($member = Member::currentUser()){
$file->OwnerID = $member->ID;
}
$file->write();
$file = $this->upload->getFile();
if ($member = Member::currentUser()) {
$file->OwnerID = $member->ID;
}
$file->write();

if($this->sessionstoreid){
Session::set($this->sessionstoreid.".".$file->ID, $file->ID);
}
if ($this->sessionstoreid) {
Session::set($this->sessionstoreid.".".$file->ID, $file->ID);
}

//TODO: record linking
//TODO: record linking

//if ajax, then return file details
$json = $file->toMap();
if($file instanceof Image){
$json['thumbnailurl'] = $file->CMSThumbnail()->getURL();
}
//if ajax, then return file details
$json = $file->toMap();
if ($file instanceof Image) {
$json['thumbnailurl'] = $file->CMSThumbnail()->getURL();
}

return $this->returnJSON($json);
}

function returnJSON($jsonarray){
return htmlspecialchars(json_encode($jsonarray), ENT_NOQUOTES);
}

/**
* Save XmlHttpRequest (ajax) submitted image into new file.
* (only supported by newer browsers)
*
* @todo: performance handling - see http://lenss.nl/2010/09/drag-drop-uploads-with-xmlhttprequest2-and-php-revised/
*
*/
function saveXHR($filename){
//TODO: use base_convert(uniqid(),10,36) to add uniqueness to each file
$tempfilepath = TEMP_FOLDER.'/'.$filename;
$upload = file_put_contents($tempfilepath,file_get_contents('php://input'));
$size = (isset($_SERVER["CONTENT_LENGTH"]))? (int)$_SERVER["CONTENT_LENGTH"] : 0;
if(!$size) return null; //TODO: throwing an error message would help here
return array(
'tmp_name' => $tempfilepath,
'name' => $filename,
'size' => $size
);
}

}
return $this->returnJSON($json);
}

public function returnJSON($jsonarray)
{
return htmlspecialchars(json_encode($jsonarray), ENT_NOQUOTES);
}

/**
* Save XmlHttpRequest (ajax) submitted image into new file.
* (only supported by newer browsers)
*
* @todo: performance handling - see http://lenss.nl/2010/09/drag-drop-uploads-with-xmlhttprequest2-and-php-revised/
*
*/
public function saveXHR($filename)
{
//TODO: use base_convert(uniqid(),10,36) to add uniqueness to each file
$tempfilepath = TEMP_FOLDER.'/'.$filename;
$upload = file_put_contents($tempfilepath, file_get_contents('php://input'));
$size = (isset($_SERVER["CONTENT_LENGTH"]))? (int)$_SERVER["CONTENT_LENGTH"] : 0;
if (!$size) {
return null;
} //TODO: throwing an error message would help here
return array(
'tmp_name' => $tempfilepath,
'name' => $filename,
'size' => $size
);
}
}
Loading

0 comments on commit 0b52f95

Please sign in to comment.