diff --git a/demos/card-action.php b/demos/card-action.php index ec9239f8c2..4fc032661c 100644 --- a/demos/card-action.php +++ b/demos/card-action.php @@ -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')]); diff --git a/demos/database.php b/demos/database.php index bd06bafdad..dab983703e 100644 --- a/demos/database.php +++ b/demos/database.php @@ -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']]); @@ -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()); } } } diff --git a/src/Card.php b/src/Card.php index abe6ed2bed..f4268a6d0a 100644 --- a/src/Card.php +++ b/src/Card.php @@ -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 @@ -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)); @@ -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); } @@ -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'])); diff --git a/src/CardDeck.php b/src/CardDeck.php index d51fbfb804..9575f1afe6 100644 --- a/src/CardDeck.php +++ b/src/CardDeck.php @@ -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'; @@ -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. */ @@ -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 = []; @@ -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 = []; @@ -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); } @@ -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); } @@ -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); } @@ -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'); } diff --git a/src/Component/ItemSearch.php b/src/Component/ItemSearch.php index b89d23b97e..760d0400b0 100644 --- a/src/Component/ItemSearch.php +++ b/src/Component/ItemSearch.php @@ -2,6 +2,7 @@ namespace atk4\ui\Component; +use atk4\data\Model; use atk4\ui\jsVueService; use atk4\ui\View; @@ -20,7 +21,7 @@ class ItemSearch extends View /** * The initial query. * - * @var null + * @var string */ public $q = null; @@ -59,6 +60,8 @@ public function init() /** * Return query string sent by request. + * + * @return string */ public function getQuery() { @@ -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; @@ -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, + ] + )); } }