Skip to content

Commit

Permalink
added ajax uploader to osclass and bender theme by default.
Browse files Browse the repository at this point in the history
issues #60 , #849
  • Loading branch information
conejoninja committed Nov 20, 2013
1 parent 85fc063 commit a1cee6c
Show file tree
Hide file tree
Showing 9 changed files with 344 additions and 47 deletions.
53 changes: 53 additions & 0 deletions oc-content/themes/bender/css/ajax-uploader.css
@@ -0,0 +1,53 @@
.ajax_preview_img img {
max-width: 120px;
max-height: 120px;
display: block;
margin-left: auto;
margin-right: auto;
}

#restricted-fine-uploader {
padding-top: 1em;
padding-left: 30px;
margin-bottom: 20px;
}

.photos_div div, .ajax_preview_img {
margin-right: 15px;
padding: 10px;
border: 1px black solid;
width: 125px;
height: 125px;
background: white;
margin-top: 10px;
}
.qq-upload-success div.ajax_preview_img img {
width: initial;
}

.qq-upload-list li {
float: left;
background-color: #dff0d8!important;
color: #333333!important;
display: block;
min-height: 200px;
}

/* width: 47%;*/

.qq-upload-button {
background-color: #f2dede;
color: #333333;
margin-bottom: 10px;
}

.qq-upload-file {
width: 145px;
height: 1em;
display: block;
overflow: hidden;
}

.qq-uploader {
width: 100%;
}
4 changes: 4 additions & 0 deletions oc-content/themes/bender/functions.php
Expand Up @@ -33,6 +33,10 @@
osc_enqueue_script('fancybox');
// used for date/dateinterval custom fields
osc_enqueue_script('php-date');
osc_enqueue_style('fine-uploader-css', osc_assets_url('js/fineuploader/fineuploader.css'));
osc_enqueue_style('bender-fine-uploader-css', osc_current_web_theme_url('css/ajax-uploader.css'));
osc_enqueue_script('jquery-fineuploader');


/**
Expand Down
9 changes: 6 additions & 3 deletions oc-content/themes/bender/item-post.php
Expand Up @@ -30,10 +30,11 @@
$action = 'item_edit_post';
$edit = true;
}

?>
<?php osc_current_web_theme_path('header.php') ; ?>
<?php ItemForm::location_javascript_new(); ?>
<?php if(osc_images_enabled_at_items()) ItemForm::photos_javascript(); ?>
<?php /*if(osc_images_enabled_at_items()) ItemForm::photos_javascript();*/ ?>
<div class="form-container form-horizontal">
<div class="resp-wrapper">
<div class="header">
Expand Down Expand Up @@ -76,7 +77,9 @@
</div>
</div>
<?php } ?>
<?php if( osc_images_enabled_at_items() ) { ?>
<?php if( osc_images_enabled_at_items() ) {
ItemForm::ajax_photos();
/* ?>
<div class="box photos">
<h2><?php _e('Photos', 'bender'); ?></h2>
<div class="control-group">
Expand All @@ -91,7 +94,7 @@
</div>
</div>
</div>
<?php } ?>
<?php */ } ?>
<div class="box location">
<h2><?php _e('Listing Location', 'bender'); ?></h2>

Expand Down
30 changes: 16 additions & 14 deletions oc-includes/AjaxUploader.php
Expand Up @@ -5,18 +5,18 @@ class AjaxUploader {
private $_sizeLimit;
private $_file;

function __construct($variable, array $allowedExtensions = null, $sizeLimit = null){
function __construct(array $allowedExtensions = null, $sizeLimit = null){
if($allowedExtensions===null) { $allowedExtensions = osc_allowed_extension(); }
if($sizeLimit===null) { $sizeLimit = 1024*osc_max_size_kb(); }
$this->_allowedExtensions = array_map("strtolower", $allowedExtensions);
$this->_allowedExtensions = $allowedExtensions;
$this->_sizeLimit = $sizeLimit;

if(!isset($_SERVER['CONTENT_TYPE'])) {
$this->_file = false;
} else if (strpos(strtolower($_SERVER['CONTENT_TYPE']), 'multipart/') === 0) {
$this->_file = new AjaxUploadedFileForm($variable);
$this->_file = new AjaxUploadedFileForm();
} else {
$this->_file = new AjaxUploadedFileXhr($variable);
$this->_file = new AjaxUploadedFileXhr();
}
}

Expand All @@ -29,21 +29,24 @@ function handleUpload($uploadFilename, $replace = false){
if($size == 0) { return array('error' => __('File is empty')); }
if($size > $this->_sizeLimit) { return array('error' => __('File is too large')); }

$pathinfo = pathinfo($this->_file->getName());
$pathinfo = pathinfo($this->_file->getOriginalName());
$ext = @$pathinfo['extension'];
$uuid = pathinfo($uploadFilename);

if($this->_allowedExtensions && !in_array(strtolower($ext), $this->_allowedExtensions)){
$these = implode(', ', $this->_allowedExtensions);
return array('error' => sprintf(__('File has an invalid extension, it should be one of %s.'), $these));
if($this->_allowedExtensions && stripos($this->_allowedExtensions, strtolower($ext))===false){
return array('error' => sprintf(__('File has an invalid extension, it should be one of %s.'), $this->_allowedExtensions));
}

if(!$replace){
if(file_exists($$uploadFilename)) {
if(file_exists($uploadFilename)) {
return array('error' => 'Could not save uploaded file. File already exists');
}
}

if($this->file->save($uploadFilename)){
if($this->_file->save($uploadFilename)){
$files = Session::newInstance()->_get('ajax_files');
$files[Params::getParam('qquuid')] = $uuid['basename'];
Session::newInstance()->_set('ajax_files', $files);
return array('success' => true);
} else {
return array('error' => 'Could not save uploaded file. The upload was cancelled, or server error encountered');
Expand All @@ -53,8 +56,7 @@ function handleUpload($uploadFilename, $replace = false){
}

class AjaxUploadedFileXhr {
private $_variable;
function __construct($variable) { $this->_variable = $variable; }
function __construct() {}

public function save($path) {
$input = fopen("php://input", "r");
Expand All @@ -69,7 +71,7 @@ public function save($path) {
return true;
}

public function getOriginalName() { return Params::getParam($this->_variable); }
public function getOriginalName() { return Params::getParam('qqfile'); }
public function getSize() {
if(isset($_SERVER["CONTENT_LENGTH"])){
return (int)$_SERVER["CONTENT_LENGTH"];
Expand All @@ -81,7 +83,7 @@ public function getSize() {

class AjaxUploadedFileForm {
private $_file;
function __construct($variable) { $this->_file = Params::getFiles($variable); }
function __construct() { $this->_file = Params::getFiles('qqfile'); }
public function save($path) { return move_uploaded_file($this->_file['tmp_name'], $path); }
public function getOriginalName() { return $this->_file['name']; }
public function getSize() { return $this->_file['size']; }
Expand Down
13 changes: 13 additions & 0 deletions oc-includes/osclass/ItemActions.php
Expand Up @@ -1141,10 +1141,23 @@ public function prepareData( $is_add )
$aItem['title'] = Params::getParam('title');
$aItem['description'] = Params::getParam('description');
$aItem['photos'] = Params::getFiles('photos');
$ajax_photos = Params::getParam('ajax_photos');
$aItem['s_ip'] = get_ip();
$aItem['d_coord_lat'] = (Params::getParam('d_coord_lat') != '') ? Params::getParam('d_coord_lat') : null;
$aItem['d_coord_long'] = (Params::getParam('d_coord_long') != '') ? Params::getParam('d_coord_long') : null;

if(is_array($ajax_photos)) {
foreach($ajax_photos as $photo) {
if(file_exists(osc_content_path().'uploads/temp/'.$photo)) {
$aItem['photos']['name'][] = $photo;
$aItem['photos']['type'][] = 'image/*';
$aItem['photos']['tmp_name'][] = osc_content_path().'uploads/temp/'.$photo;
$aItem['photos']['error'][] = UPLOAD_ERR_OK;
$aItem['photos']['size'][] = 0;
}
}
}

if($is_add || $this->is_admin) {
$dt_expiration = Params::getParam('dt_expiration');
if($dt_expiration==-1) {
Expand Down
59 changes: 50 additions & 9 deletions oc-includes/osclass/controller/ajax.php
Expand Up @@ -66,12 +66,31 @@ function doModel()
echo json_encode($cities);
break;
case 'delete_image': // Delete images via AJAX
$id = Params::getParam('id');
$item = Params::getParam('item');
$code = Params::getParam('code');
$secret = Params::getParam('secret');
$ajax_photo = Params::getParam('ajax_photo');
$id = Params::getParam('id');
$item = Params::getParam('item');
$code = Params::getParam('code');
$secret = Params::getParam('secret');
$json = array();

if($ajax_photo!='') {
$files = Session::newInstance()->_get('ajax_files');
$success = false;

foreach($files as $uuid => $file) {
if($file==$ajax_photo) {
$filename = $files[$uuid];
unset($files[$uuid]);
Session::newInstance()->_set('ajax_files', $files);
$success = @unlink(osc_content_path().'uploads/temp/'.$filename);
break;
}
}

echo json_encode(array('success' => $success, 'msg' => $success?_m('The selected photo has been successfully deleted'):_m("The selected photo couldn't be deleted")));
return false;
}

if( Session::newInstance()->_get('userId') != '' ){
$userId = Session::newInstance()->_get('userId');
$user = User::newInstance()->findByPrimaryKey($userId);
Expand Down Expand Up @@ -254,15 +273,37 @@ function doModel()
break;
case 'ajax_upload':
// Include the uploader class
$variable = Params::getParam('variable')?Params::getParam('variable'):'fu_images';
$uploader = new AjaxUploader($variable);
@mkdir(osc_content_path().'/uploads/temp/');
require_once(LIB_PATH."AjaxUploader.php");
$uploader = new AjaxUploader();
@mkdir(osc_content_path().'uploads/temp/');
$original = pathinfo($uploader->getOriginalName());
$filename = uniqid($variable."_").".".$original['extension'];
$result = $uploader->handleUpload(osc_content_path().'/uploads/temp/');
$filename = uniqid("qqfile_").".".$original['extension'];
$result = $uploader->handleUpload(osc_content_path().'uploads/temp/'.$filename);
$result['uploadName'] = $filename;
echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
break;
case 'ajax_validate':
$id = Params::getParam('id');
if(!is_numeric($id)) { echo json_encode(array('success' => false)); die();}
$secret = Params::getParam('secret');
$item = Item::newInstance()->findByPrimaryKey($id);
if($item['s_secret']!=$secret) { echo json_encode(array('success' => false)); die();}
$nResources = ItemResource::newInstance()->countResources($id);
$result = array('success' => ($nResources<osc_max_images_per_item()), 'count' => $nResources);
echo json_encode($result);
break;
case 'delete_ajax_upload':
$files = Session::newInstance()->_get('ajax_files');
$success = false;
$filename = '';
if(isset($files[Params::getParam('qquuid')]) && $files[Params::getParam('qquuid')]!='') {
$filename = $files[Params::getParam('qquuid')];
unset($files[Params::getParam('qquuid')]);
Session::newInstance()->_set('ajax_files', $files);
$success = @unlink(osc_content_path().'uploads/temp/'.$filename);
};
echo json_encode(array('success' => $success, 'uploadName' => $filename));
break;
default:
echo json_encode(array('error' => __('no action defined')));
break;
Expand Down
7 changes: 7 additions & 0 deletions oc-includes/osclass/cron.php
Expand Up @@ -52,6 +52,13 @@
}
}

$files = glob(osc_content_path().'uploads/temp/qqfile_*');
foreach($files as $file) {
if((time()-filectime($file))>(2*3600)) {
@unlink($file);
}
}

osc_run_hook('cron_hourly');
}
}
Expand Down

0 comments on commit a1cee6c

Please sign in to comment.