Skip to content

Commit

Permalink
Further fixes for ATK4 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dtemaster committed Oct 20, 2021
1 parent 5ab2bc7 commit 6fbd2e5
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 102 deletions.
20 changes: 10 additions & 10 deletions demos/uploadtest-filestore.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace atk4\multiupload;
namespace Atk4\Multiupload;

use Atk4\Ui\Form;

Expand All @@ -19,32 +19,32 @@ class PersistenceSql extends \Atk4\Data\Persistence\Sql
}

// change this as needed
$app->db = $app->add(new \atk4\multiupload\PersistenceSql('mysql://root:root@localhost/atk4'));
$app->db = new \atk4\multiupload\PersistenceSql('mysql://root:root@localhost/atk4');
$app->db->setApp($app);

$adapter = new \League\Flysystem\Adapter\Local(__DIR__.'/localfiles');
$adapter = new \League\Flysystem\Local\LocalFilesystemAdapter(__DIR__.'/localfiles');
$app->filesystem = new \League\Flysystem\Filesystem($adapter);



class Friend extends \Atk4\Data\Model {

use \atk4\core\AppScopeTrait;


public $table = 'friend';

function init() : void {
protected function init() : void {
parent::init();

$this->addField('name'); // friend's name
$this->addField('file', new \atk4\multiupload\Field\File($this->app->filesystem)); // storing file here
$this->addField('file', new \Atk4\Multiupload\Field\File($this->persistence->getApp()->filesystem)); // storing file here

}
}

$form = Form::addTo($app);
$model = new Friend($app->db);
$entity = $model->tryLoad(7);
$form->setModel($entity);

$form->setModel(new Friend($app->db));
$form->model->tryLoad(7);

$gr = $app->add([\Atk4\Ui\Grid::class, 'menu'=>false, 'paginator'=>false]);
$gr->setModel(new \Atk4\Filestore\Model\File($app->db));
Expand Down
4 changes: 2 additions & 2 deletions demos/uploadtest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

$control2->set('a_new_token', 'an-img-file-name');

$control2->onDelete(function ($fileId) use ($img) {
$control2->onDelete(function ($fileId) {

return new \Atk4\Ui\JsToast([
'title' => 'Delete successfully',
Expand All @@ -78,7 +78,7 @@



$control2->onUpload(function ($files) use ($form, $img) {
$control2->onUpload(function ($files) use ($form) {
if ($files === 'error') {
return $form->error('img', 'Error uploading image.');
}
Expand Down
18 changes: 9 additions & 9 deletions src/Field/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

// vim:ts=4:sw=4:et:fdm=marker:fdl=0

namespace atk4\multiupload\Field;
namespace Atk4\Multiupload\Field;

class File extends \Atk4\Data\FieldSql
{
use \atk4\core\InitializerTrait {
use \Atk4\Core\InitializerTrait {
init as _init;
}


public $ui = ['form' => [\atk4\multiupload\Form\Control\Upload::class]];
public $ui = ['form' => [\Atk4\Multiupload\Form\Control\Upload::class]];

/**
* Set a custom model for File
Expand All @@ -34,18 +34,18 @@ class File extends \Atk4\Data\FieldSql
public $fieldFilename;
public $fieldURL;

public function init(): void
protected function init(): void
{
$this->_init();

if (!$this->model) {
$this->model = new \atk4\filestore\Model\File($this->owner->persistence);
$this->model = new \Atk4\Filestore\Model\File($this->getOwner()->persistence);
$this->model->flysystem = $this->flysystem;
}

$this->normalizedField = preg_replace('/_id$/', '', $this->short_name);

$this->reference = $this->owner->addRef($this->short_name, function($m, $c, $d) {
$this->reference = $this->getOwner()->addRef($this->short_name, ['model' => function($m, $c, $d) {
$archive = $this->model->newInstance();

// only show records of currently loaded record
Expand All @@ -66,11 +66,11 @@ public function init(): void
}

return $archive;
});
}]);

// $this->importFields();

$this->owner->onHook(\Atk4\Data\Model::HOOK_BEFORE_SAVE, function($m) {
$this->getOwner()->onHook(\Atk4\Data\Model::HOOK_BEFORE_SAVE, function($m) {
if ($m->isDirty($this->short_name)) {
$oldtokens = $m->dirty[$this->short_name];
$newtokens = $m->get($this->short_name);
Expand All @@ -92,7 +92,7 @@ public function init(): void
}
}
});
$this->owner->onHook(\Atk4\Data\Model::HOOK_BEFORE_DELETE, function($m) {
$this->getOwner()->onHook(\Atk4\Data\Model::HOOK_BEFORE_DELETE, function($m) {
$tokens = $m->get($this->short_name);
if ($tokens) {
foreach (explode(',', $tokens) as $token) {
Expand Down
42 changes: 21 additions & 21 deletions src/Form/Control/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class Upload extends \Atk4\Multiupload\MultiUpload
protected function init(): void {
parent::init();

$this->onUpload([$this, 'uploaded']);
$this->onDelete([$this, 'deleted']);
$this->onDownload([$this, 'downloaded']);
$this->onUpload(\Closure::fromCallable([$this, 'uploaded']));
$this->onDelete(\Closure::fromCallable([$this, 'deleted']));
$this->onDownload(\Closure::fromCallable([$this, 'downloaded']));

$this->renderRowFunction = function($record) {
return [
Expand All @@ -27,7 +27,7 @@ public function uploaded($file)
{
// provision a new file for specified flysystem
$f = $this->field->model;
$f->newFile($this->field->flysystem);
$entity = $f->newFile($this->field->flysystem);

// add (or upload) the file
$stream = fopen($file['tmp_name'], 'r+');
Expand All @@ -37,47 +37,47 @@ public function uploaded($file)
}

// get meta from browser
$f->set('meta_mime_type', $file['type']);
$entity->set('meta_mime_type', $file['type']);

// store meta-information
$is = getimagesize($file['tmp_name']);
$f->set('meta_is_image', (bool) $is);
$entity->set('meta_is_image', (bool) $is);
if ($is){
$f->set('meta_mime_type', $is['mime']);
$f->set('meta_image_width', $is[0]);
$f->set('meta_image_height', $is[1]);
$entity->set('meta_mime_type', $is['mime']);
$entity->set('meta_image_width', $is[0]);
$entity->set('meta_image_height', $is[1]);
//$m['extension'] = $is['mime'];
}
$f->set('meta_md5',md5_file($file['tmp_name']));
$f->set('meta_filename', $file['name']);
$f->set('meta_size', $file['size']);
$entity->set('meta_md5',md5_file($file['tmp_name']));
$entity->set('meta_filename', $file['name']);
$entity->set('meta_size', $file['size']);


$f->save();
$this->setFileId($f->get('token'));
$entity->save();
$this->setFileId($entity->get('token'));

$js = new \Atk4\Ui\JsNotify(['content' => $f->get('meta_filename').' uploaded!', 'color' => 'green']);
$js = new \Atk4\Ui\JsNotify(['content' => $entity->get('meta_filename').' uploaded!', 'color' => 'green']);
return $js;
}

public function deleted($token)
{
$f = $this->field->model;
$f->tryLoadBy('token', $token);
$entity = $f->tryLoadBy('token', $token);

$js = new \Atk4\Ui\JsNotify(['content' => $f->get('meta_filename').' has been removed!', 'color' => 'green']);
if ($f->get('status') == 'draft') {
$f->delete();
$js = new \Atk4\Ui\JsNotify(['content' => $entity->get('meta_filename').' has been removed!', 'color' => 'green']);
if ($entity->get('status') == 'draft') {
$entity->delete();
}

return $js;
}

public function downloaded($token)
{ $f = $this->field->model;
$f->tryLoadBy('token', $token);
$entity = $f->tryLoadBy('token', $token);

$js = [ new \Atk4\Ui\JsNotify(['content' => $f->get('meta_filename').' is being downloaded!', 'color' => 'green']),
$js = [ new \Atk4\Ui\JsNotify(['content' => $entity->get('meta_filename').' is being downloaded!', 'color' => 'green']),
];

return $js;
Expand Down
98 changes: 38 additions & 60 deletions src/MultiUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ class MultiUpload extends \Atk4\Ui\Form\Control\Dropdown
* @var bool
*/
public $isJsLoaded = false;

public const UPLOAD_ACTION = 'upload';
public const DELETE_ACTION = 'delete';
public const DOWNLOAD_ACTION = 'download';

/** @var bool check if callback is trigger by one of the action. */
private $_isCbRunning = false;
Expand Down Expand Up @@ -181,30 +185,22 @@ public function addJsAction($action)
*/
public function onDelete(\Closure $fx)
{
/* NEW STYLE 3.0

$this->hasDeleteCb = true;
if (($_POST['f_upload_action'] ?? null) === self::DELETE_ACTION) {
$this->cb->set(function () use ($fx) {
$fileId = $_POST['f_upload_id'] ?? null;
/* $fileName = $_POST['f_name'] ?? null;
* $this->addJsAction($fx($fileName));
* vorher: $this->addJsAction(call_user_func_array($fx, [$fileName]));
*
*/
$this->addJsAction($fx($fileId));

return $this->jsActions;
});
}

*/
if (is_callable($fx)) {
$this->hasDeleteCb = true;
$action = $_POST['action'] ?? null;
if ($this->cb->triggered() && $action === 'delete') {
$this->_isCbRunning = true;
$fileName = $_POST['f_name'] ?? null;
$this->cb->set(function () use ($fx, $fileName) {
$this->addJsAction(call_user_func_array($fx, [$fileName]));

return $this->jsActions;
});
}
}
}

Expand All @@ -214,10 +210,8 @@ public function onDelete(\Closure $fx)
*
* @param callable $fx
*/
public function onUpload($fx = null)
{ /* NEW STLYE ATK 3.0
$this->hasUploadCb = true;
public function onUpload(\Closure $fx)
{ $this->hasUploadCb = true;
if (($_POST['f_upload_action'] ?? null) === self::UPLOAD_ACTION) {
$this->cb->set(function () use ($fx) {
$postFiles = [];
Expand Down Expand Up @@ -252,32 +246,6 @@ public function onUpload($fx = null)
return $this->jsActions;
});
}
*/
if (is_callable($fx)) {
$this->hasUploadCb = true;
if ($this->cb->triggered()) {
$this->_isCbRunning = true;
$action = $_POST['action'] ?? null;
$files = $_FILES ?? null;

if ($action === 'upload' && !$files['file']['error']) {
$this->cb->set(function () use ($fx, $files) {
foreach ($files as $file) {

$this->addJsAction(call_user_func_array($fx, [$file]));
$this->addJsAction([
$this->js()->atkmultiFileUpload('updateField', [$this->fileId, $file['name']])
]);
}
return $this->jsActions;
});
} elseif ($action === null || isset($files['file']['error'])) {
$this->cb->set(function () use ($fx, $files) {
return call_user_func($fx, 'error');
});
}
}
}
}

/**
Expand All @@ -288,18 +256,22 @@ public function onUpload($fx = null)
*/
public function onDownload($fx = null)
{
if (is_callable($fx)) {
$this->hasDownloadCb = true;
$action = $_POST['action'] ?? null;
if ($this->cb->triggered() && $action === 'download') {
$this->_isCbRunning = true;
$fileName = $_POST['f_name'] ?? null;
$this->cb->set(function () use ($fx, $fileName) {
$this->addJsAction(call_user_func_array($fx, [$fileName]));

return $this->jsActions;
});
}

$this->hasDeleteCb = true;
if (($_POST['f_upload_action'] ?? null) === self::DOWNLOAD_ACTION) {
$this->cb->set(function () use ($fx) {
$fileId = $_POST['f_upload_id'] ?? null;
/* $fileName = $_POST['f_name'] ?? null;
* $this->addJsAction($fx($fileName));
* vorher: $this->addJsAction(call_user_func_array($fx, [$fileName]));
*
*/
$this->addJsAction($fx($fileId));

return $this->jsActions;
});

}
}

Expand All @@ -311,9 +283,15 @@ protected function renderView(): void
}
parent::renderView();

if (!$this->_isCbRunning && (!$this->hasUploadCb || !$this->hasDeleteCb)) {
throw new Exception('onUpload and onDelete callback must be called to use file upload. Missing one or both of them.');
if ($this->cb->canTerminate()) {
$uploadActionRaw = $_POST['f_upload_action'] ?? null;
if (!$this->hasUploadCb && ($uploadActionRaw === self::UPLOAD_ACTION)) {
throw new Exception('Missing onUpload callback.');
} elseif (!$this->hasDeleteCb && ($uploadActionRaw === self::DELETE_ACTION)) {
throw new Exception('Missing onDelete callback.');
}
}

if (!empty($this->accept)) {
$this->template->trySet('accept', implode(',', $this->accept));
}
Expand All @@ -326,7 +304,7 @@ protected function renderView(): void
}

if (!$this->isJsLoaded) {
$this->app->requireJs('../public/atkmultiupload.js');
$this->getApp()->requireJs('../public/atkmultiupload.js');
}


Expand Down

0 comments on commit 6fbd2e5

Please sign in to comment.