Skip to content

Instantly share code, notes, and snippets.

@ounziw
Last active November 21, 2017 05:59
Show Gist options
  • Save ounziw/56e2b4f2602cafa29f84020768e44b37 to your computer and use it in GitHub Desktop.
Save ounziw/56e2b4f2602cafa29f84020768e44b37 to your computer and use it in GitHub Desktop.
drag & drop image upload for concrete5.7.5.x. put this file into application/attributes/image_file/
<?php
// LICENSE: MIT
// by Fumito MIZUNO
// forked from controller/attribute/image_file/controller.php
namespace Application\Attribute\ImageFile;
use Core;
use Database;
use File;
use FileImporter;
class Controller extends \Concrete\Attribute\ImageFile\Controller
{
public function form()
{
$bf = false;
$imgurl = \Core::getApplicationURL() . '/concrete/images/testbg.png';
$value = 0;
if ($this->getAttributeValueID() > 0) {
$bf = $this->getValue();
if(is_object($bf)) {
$img = $bf->getApprovedVersion();
if (in_array($img->getMimeType(),array('image/png','image/jpeg','image/gif'))) {
$imgurl = $img->getURL();
} else {
$mime = new \Concrete\Core\File\Service\Mime;
$fileext = $mime->mimeToExtension($img->getMimeType());
$imgurl = \Core::getApplicationURL() . '/concrete/images/icons/filetypes/' . $fileext . '.png';
}
if ($img->getFileID()) {
$value = $img->getFileID();
}
}
}
$form = '<div class="ccm-attribute ccm-attribute-image-file">';
$form .= Core::make('helper/form')->number($this->field('value'), $value, array('style' => 'display:none'));
$form .= '<input type="file" accept="image/* style="height:60px;padding:10px;margin-bottom:10px" name="akID' . $this->attributeKey->getAttributeKeyID() . '" id="akID' . $this->attributeKey->getAttributeKeyID() . '" class="" />';
$form .= '</div>';
$form .= '<div><img id="thumbnail' . $this->attributeKey->getAttributeKeyID() . '" src="' . $imgurl . '" width="100" height="100"></div>';
$form .= '<div><input type="checkbox" name="akID' . $this->attributeKey->getAttributeKeyID() . 'delete" id="akID' . $this->attributeKey->getAttributeKeyID() . 'delete" value="delete" />画像を削除する</div>';
$form .= '<script>
$("#akID' . $this->attributeKey->getAttributeKeyID() . '").change(function(){
if (this.files.length > 0) {
var file = this.files[0];
// https://developer.mozilla.org/ja/docs/Web/API/FileReader/readAsDataURL
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function() {
$("#thumbnail' . $this->attributeKey->getAttributeKeyID() . '").attr("src", reader.result );
}
}
});
</script>';
print $form;
}
public function saveForm($data)
{
$id = 'akID' . $this->attributeKey->getAttributeKeyID();
$delete = $id . 'delete';
$db = Database::connection();
$value = $db->GetOne("select fID from atFile where avID = ?", array($this->getAttributeValueID()));
$f = File::getByID($value);
if (is_object($f)) {
$filename = $f->getFileName();
}
// concrete5/blocks/form/controller::action_submit_form
if ($data['value'] > 0 && $_POST[$delete] == 'delete') {
$db = Database::connection();
$db->Replace('atFile', array('avID' => $this->getAttributeValueID(), 'fID' => 0), 'avID', true);
} elseif (isset($_FILES[$id]) && ($filename != $_FILES[$id]['name']) && $_FILES[$id]['tmp_name']) {
$fi = new FileImporter();
$resp = $fi->import($_FILES[$id]['tmp_name'], $_FILES[$id]['name']);
if (!($resp instanceof Version)) {
switch ($resp) {
case FileImporter::E_FILE_INVALID_EXTENSION:
$errors['fileupload'] = t('Invalid file extension.');
break;
case FileImporter::E_FILE_INVALID:
$errors['fileupload'] = t('Invalid file.');
break;
}
}
$db = Database::connection();
$db->Replace('atFile', array('avID' => $this->getAttributeValueID(), 'fID' => $resp->getFileID()), 'avID', true);
} else if ($data['value'] > 0) {
$db = Database::connection();
$db->Replace('atFile', array('avID' => $this->getAttributeValueID(), 'fID' => intval($data['value'])), 'avID', true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment