Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issues with Card and CardDeck #996

Merged
merged 3 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions demos/card-action.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,22 @@
$app->add(['Header', 'Models', 'size' => 1, 'subHeader' => 'Card may display information from many models.']);

$stats = new Stat($db);

$stats->loadAny();

$c = $app->add('Card');

$c->setModel($stats, ['client_name', 'description']);

$c->addSection('Project: ', $stats, ['start_date', 'finish_date'], true);

$client = $stats->ref('client_country_iso')->loadAny();
$notify = $client->addAction('Notify',
['args' => [
$notify = $client->addAction('Notify', [
'args' => [
'note'=> ['type'=>'string', 'required'=>true],
],
'callback' => function ($m) {
return 'Note to client is sent.';
},
]);

'callback' => function ($m) {
return 'Note to client is sent.';
},
]);
$c->addSection('Client Country:', $client, ['iso', 'numcode', 'phonecode'], true);

$c->addClickAction($notify, null, [$client->get('id')]);
18 changes: 9 additions & 9 deletions demos/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ public function init()
$this->addField('client_address', ['type' => 'string', 'ui' => ['form' => [new \atk4\ui\FormField\TextArea(), 'rows' => 4]]]);

$this->hasOne('client_country_iso', [
new Country(),
'their_field' => 'iso',
'ui' => [
'display' => [
'form' => 'Line',
new Country(),
'their_field' => 'iso',
'ui' => [
'display' => [
'form' => 'Line',
],
],
],
])
->addField('client_country', 'name');
])
->addField('client_country', 'name');

$this->addField('is_commercial', ['type' => 'boolean']);
$this->addField('currency', ['enum' => ['EUR', 'USD', 'GBP']]);
Expand Down Expand Up @@ -165,7 +165,7 @@ public function importFromFilesystem($path)
]);

if ($fileinfo->isDir()) {
$this->ref('SubFolder')->importFromFilesystem($path.'/'.$fileinfo->getFilename());
$this->ref('SubFolder')->importFromFilesystem($path . '/' . $fileinfo->getFilename());
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public function addContent(View $view)
* to the main section of this card.
*
* @param \atk4\data\Model $m The model.
* @param array $fields An array of fields name to display in content.
* @param array|false $fields An array of fields name to display in content.
*
* @throws Exception
* @throws \atk4\data\Exception
Expand All @@ -208,8 +208,10 @@ public function setModel(Model $m, $fields = null)
$m = parent::setModel($m);
}

if (!$fields) {
if ($fields === null) {
$fields = array_keys($this->model->getFields(['editable', 'visible']));
} elseif ($fields === false) {
$fields = [];
}

$this->setDataId($this->model->get($this->model->id_field));
Expand Down Expand Up @@ -296,7 +298,7 @@ public function addSection(string $title = null, Model $model = null, array $fie
}

if ($model && $fields) {
$this->setModel($model);
$section->setModel($model);
$section->addFields($model, $fields, $useTable, $useLabel);
}

Expand Down Expand Up @@ -385,13 +387,13 @@ public function addClickAction(Generic $action, $button = null, $args = [], $con
*/
public function addExtraFields($m, $fields, $glue = null)
{
$this->setModel($m);
$this->setModel($m, false);

// display extra field in line.
if ($glue) {
$extra = '';
foreach ($fields as $field) {
$extra .= $m->get($field).$glue;
$extra .= $m->get($field) . $glue;
}
$extra = rtrim($extra, $glue);
$this->addExtraContent(new View([$extra, 'ui'=>'ui basic fitted segment']));
Expand Down
45 changes: 25 additions & 20 deletions src/CardDeck.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class CardDeck extends View
{
public $ui = '';

/** @var string Card type inside this deck. */
public $card = Card::class;
/** @var string|View Card type inside this deck. */
public $card = [Card::class];

/** @var string default template file. */
public $defaultTemplate = 'card-deck.html';
Expand All @@ -35,24 +35,24 @@ class CardDeck extends View
public $useAction = true;

/** @var null|View The container view. The view that is reload when page or data changed. */
public $container = ['View', 'ui'=> 'basic segment'];
public $container = [View::class, 'ui' => 'basic segment'];

/** @var View The view containing Cards. */
public $cardHolder = ['View', 'ui' => 'cards'];
public $cardHolder = [View::class, 'ui' => 'cards'];

/** @var null|View The paginator view. */
public $paginator = null;
public $paginator = [Paginator::class];

/** @var int The number of card to be display per page. */
public $ipp = 8;
/** @var int The number of cards to be displayed per page. */
public $ipp = 9;

/** @var null|array A menu seed for displaying button inside. */
public $menu = ['View', 'ui' => 'stackable grid'];
public $menu = [View::class, 'ui' => 'stackable grid'];

/** @var array|ItemSearch */
public $search = ['View', 'ui' => 'ui compact basic segment'];
public $search = [ItemSearch::class, 'ui' => 'ui compact basic segment'];

/** @var null A view container for buttons. Added into menu when menu is set. */
/** @var null|View A view container for buttons. Added into menu when menu is set. */
private $btns = null;

/** @var string Button css class for menu. */
Expand All @@ -65,7 +65,7 @@ class CardDeck extends View
public $jsExecutor = jsUserAction::class;

/** @var array Default notifier to perform when model action is successful * */
public $notifyDefault = ['jsToast', 'settings' => ['displayTime' => 5000]];
public $notifyDefault = [jsToast::class, 'settings' => ['displayTime' => 5000]];

/** @var array Model single scope action to include in table action column. Will include all single scope actions if empty. */
public $singleScopeActions = [];
Expand All @@ -83,7 +83,12 @@ class CardDeck extends View
public $defaultMsg = 'Done!';

/** @var array seed to create View for displaying when search result is empty. */
public $noRecordDisplay = [Message::class, 'content' => 'Result empty!', 'icon' => 'info circle', 'text' => 'Your search did not return any record or there is no record available.'];
public $noRecordDisplay = [
Message::class,
'content' => 'Result empty!',
'icon' => 'info circle',
'text' => 'Your search did not return any record or there is no record available.',
];

/** @var array A collection of menu button added in Menu. */
private $menuActions = [];
Expand Down Expand Up @@ -117,13 +122,14 @@ public function init()
*/
protected function addMenuBar()
{
$this->menu = $this->add($this->factory(View::class, $this->menu), 'Menu');
$this->menu = $this->add($this->factory($this->menu), 'Menu');

$left = $this->menu->add(['View', 'ui' => $this->search !== false ? 'twelve wide column' : 'sixteen wide column']);
$this->btns = $left->add(['View', 'ui' => 'buttons']);

if ($this->search !== false) {
$right = $this->menu->add(['View', 'ui' => 'four wide column']);
$this->search = $right->add($this->factory(ItemSearch::class, array_merge($this->search, ['context' => '#'.$this->container->name])));
$this->search = $right->add($this->factory($this->search, ['context' => '#' . $this->container->name]));
$this->search->reload = $this->container;
$this->query = $this->app->stickyGet($this->search->queryArg);
}
Expand All @@ -137,7 +143,7 @@ protected function addMenuBar()
protected function addPaginator()
{
$seg = $this->container->add(['View', 'ui'=> 'basic segment'])->addStyle('text-align', 'center');
$this->paginator = $seg->add($this->factory([Paginator::class, 'reload' => $this->container], $this->paginator, 'atk4\ui'));
$this->paginator = $seg->add($this->factory($this->paginator, ['reload' => $this->container]));
$this->page = $this->app->stickyGet($this->paginator->name);
}

Expand All @@ -151,11 +157,10 @@ public function setModel(Model $model, array $fields = null, array $extra = null

if ($count = $this->initPaginator()) {
$this->model->each(function ($m) use ($fields, $extra) {
// need model clone in order to keep it's loaded values.
// need model clone in order to keep it's loaded values
$m = clone $m;
$c = $this->cardHolder->add([$this->card])->addClass('segment');
$c->setModel($m);
$c->addSection($m->getTitle(), $m, $fields, $this->useLabel, $this->useTable);
$c = $this->cardHolder->add($this->factory($this->card, ['useLabel' => $this->useLabel, 'useTable' => $this->useTable]))->addClass('segment');
$c->setModel($m, $fields);
if ($extra) {
$c->addExtraFields($m, $extra, $this->extraGlue);
}
Expand Down Expand Up @@ -418,7 +423,7 @@ public function addMenuButton($button, $callback = null, $confirm = null, $isDis

if (!is_object($button)) {
if (is_string($button)) {
$button = [$button, 'ui' => 'button '.$this->menuBtnStyle];
$button = [$button, 'ui' => 'button ' . $this->menuBtnStyle];
}
$button = $this->factory('Button', $button, 'atk4\ui');
}
Expand Down
35 changes: 19 additions & 16 deletions src/Component/ItemSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace atk4\ui\Component;

use atk4\data\Model;
use atk4\ui\jsVueService;
use atk4\ui\View;

Expand All @@ -20,7 +21,7 @@ class ItemSearch extends View
/**
* The initial query.
*
* @var null
* @var string
*/
public $q = null;

Expand Down Expand Up @@ -59,6 +60,8 @@ public function init()

/**
* Return query string sent by request.
*
* @return string
*/
public function getQuery()
{
Expand All @@ -68,14 +71,14 @@ public function getQuery()
/**
* Set model condition base on search request.
*
* @param $m
* @param Model $m
*
* @return mixed
* @return Model
*/
public function setModelCondition($m)
public function setModelCondition(Model $m): Model
{
if ($q = $this->getQuery()) {
$m->addCondition('name', 'like', '%'.$q.'%');
$m->addCondition('name', 'like', '%' . $q . '%');
}

return $m;
Expand All @@ -95,16 +98,16 @@ public function renderView()
$reloadId = $this->reload;
}

$this->js(true, (new jsVueService())->createAtkVue('#'.$this->name,
'atk-item-search',
[
'reload' => $reloadId,
'queryArg' => $this->queryArg,
'url' => $this->reload->jsURL(),
'q' => $this->q,
'context' => $this->context,
]
)
);
$this->js(true, (new jsVueService())->createAtkVue(
'#' . $this->name,
'atk-item-search',
[
'reload' => $reloadId,
'queryArg' => $this->queryArg,
'url' => $this->reload->jsURL(),
'q' => $this->q,
'context' => $this->context,
]
));
}
}