|
| 1 | +<?php |
| 2 | + |
| 3 | +require 'init.php'; |
| 4 | +require 'database.php'; |
| 5 | + |
| 6 | +/** @var \atk4\ui\View $wizard */ |
| 7 | +$wizard = $app->add('Wizard'); |
| 8 | +$app->stickyGet($wizard->name); |
| 9 | + |
| 10 | +$wizard->addStep('Define User Action', function ($page) { |
| 11 | + /** @var \atk4\ui\Text $t */ |
| 12 | + |
| 13 | + \atk4\ui\Header::addTo($page, ['What are Actions?']); |
| 14 | + |
| 15 | + $t = $page->add('Text'); |
| 16 | + $t->addParagraph( |
| 17 | + <<< 'EOF' |
| 18 | +Since the early version ATK UI was about building generic UI capable of automatically read information about |
| 19 | +model Fields and visualising them correctly. Version 2.0 introduces support for "Actions" which can be declared |
| 20 | +in Data layer and can use generic UI for visualising and triggering. Models of Agile Data has always supported 3 |
| 21 | +basic actions: "save" (for new and existing records) and "delete". Historically any other interaction required |
| 22 | +tinkering with UI layer. Now ATK implements a generic support for arbitrary actions and then re-implements |
| 23 | +"save", "delete" and "add" on top. |
| 24 | +EOF |
| 25 | + ); |
| 26 | + |
| 27 | + $t->addParagraph( |
| 28 | + <<< 'EOF' |
| 29 | +This enables developer to easily add more actions in the Data layers and have the rest of ATK recognise |
| 30 | +and respect those actions. Actions can be added into the model just like you are adding fields: |
| 31 | +EOF |
| 32 | + ); |
| 33 | + |
| 34 | + $page->add(new Demo())->setCode( |
| 35 | + <<<'CODE' |
| 36 | +$country = new Country($app->app->db); |
| 37 | +
|
| 38 | +$country->addAction('send_message'); |
| 39 | +CODE |
| 40 | + ); |
| 41 | + |
| 42 | + $t = $page->add('Text'); |
| 43 | + $t->addParagraph( |
| 44 | + <<< 'EOF' |
| 45 | +Once defied - actions will be visualised in the Form, Grid, CRUD and CardDeck. Additionally add-ons will recognise |
| 46 | +your actions - for example 'Login' add-on introduces ACL system capable of enabling/disabling fields or actions |
| 47 | +on per-user basis. |
| 48 | +EOF |
| 49 | + ); |
| 50 | + |
| 51 | + $t->addParagraph( |
| 52 | + <<< 'EOF' |
| 53 | +Any actions you define will automatically appear in the UI. This is consistent with your field definitions. You can |
| 54 | +also "disable" or mark actions as "system". When action is executed, the response will appear to the user as a |
| 55 | +toast message, but this can also be customised. |
| 56 | +EOF |
| 57 | + ); |
| 58 | + |
| 59 | + $page->add(new Demo())->setCode( |
| 60 | + <<<'CODE' |
| 61 | +
|
| 62 | +$country = new Country($app->app->db); |
| 63 | +
|
| 64 | +$country->addAction('send_message', function() { |
| 65 | + return 'sent'; |
| 66 | +}); |
| 67 | +$country->tryLoadAny(); |
| 68 | +
|
| 69 | +$card = $app->add('Card'); |
| 70 | +$card->setModel($country, ['iso']); |
| 71 | +$card->addClickAction($country->getAction('send_message')); |
| 72 | + |
| 73 | +CODE |
| 74 | + ); |
| 75 | +}); |
| 76 | + |
| 77 | +$wizard->addStep('UI Integration', function ($page) { |
| 78 | + /** @var \atk4\ui\Text $t */ |
| 79 | + $t = $page->add('Text'); |
| 80 | + $t->addParagraph( |
| 81 | + <<< 'EOF' |
| 82 | +Agile UI introduces a new set of views called "Action Executors". Their job is to recognise all that meta-information |
| 83 | +that you have specified for the action and requesting it from the user. "edit" action is defined for models by default |
| 84 | +and you can trigger it on button-click with a very simple code: |
| 85 | +EOF |
| 86 | + ); |
| 87 | + |
| 88 | + $page->add(new Demo())->setCode( |
| 89 | + <<<'CODE' |
| 90 | +$country = new Country($app->app->db); |
| 91 | +$country->loadAny(); |
| 92 | +
|
| 93 | +$app->add(['Button', 'Edit some country']) |
| 94 | + ->on('click', $country->getAction('edit')); |
| 95 | +CODE |
| 96 | + ); |
| 97 | + |
| 98 | + $t = $page->add('Text'); |
| 99 | + $t->addParagraph( |
| 100 | + <<< 'EOF' |
| 101 | +It is not only the button, but any view can have "Action" passed as a second step of the on() call. Here the action |
| 102 | +is executed when you click on "World" menu item: |
| 103 | +EOF |
| 104 | + ); |
| 105 | + |
| 106 | + $page->add(new Demo())->setCode( |
| 107 | + <<<'CODE' |
| 108 | +$country = new Country($app->app->db); |
| 109 | +$country->loadAny(); |
| 110 | +
|
| 111 | +$menu = $app->add('Menu'); |
| 112 | +$menu->addItem('Hello'); |
| 113 | +$menu->addItem('World', $country->getAction('edit')); |
| 114 | +CODE |
| 115 | + ); |
| 116 | +}); |
| 117 | + |
| 118 | +$wizard->addStep('Arguments', function ($page) { |
| 119 | + $t = $page->add('Text'); |
| 120 | + $t->addParagraph( |
| 121 | + <<< 'EOF' |
| 122 | +Next demo defines an action that requires arguments. You can specify action when the action is invoked, but if not |
| 123 | +defined - user will be asked to supply an argument. Action will automatically validate argument types and it uses |
| 124 | +same type system as fields. |
| 125 | +EOF |
| 126 | + ); |
| 127 | + |
| 128 | + $page->add(new Demo())->setCode( |
| 129 | + <<<'CODE' |
| 130 | +$model = new \atk4\data\Model($app->db, 'test'); |
| 131 | +$model->addAction('greet', [ |
| 132 | + 'scope' => \atk4\data\UserAction\Generic::NO_RECORDS, |
| 133 | + 'args'=> [ |
| 134 | + 'age'=>[ |
| 135 | + 'type'=>'integer', |
| 136 | + 'required' => true |
| 137 | + ] |
| 138 | + ], |
| 139 | + 'callback'=>function ($m, $age) { |
| 140 | + return 'Age is '.$age; |
| 141 | + } |
| 142 | +]); |
| 143 | +
|
| 144 | +$app->add(new \atk4\ui\FormField\Line([ |
| 145 | + // TODO - how to specify watermark here? |
| 146 | + 'action' => $model->getAction('greet'), |
| 147 | +])); |
| 148 | +
|
| 149 | +$app->add(['ui'=>'divider']); |
| 150 | +
|
| 151 | +$app->add(['Button', 'Greet without Age argument']) |
| 152 | + ->on('click', $model->getAction('greet')); |
| 153 | +CODE |
| 154 | + ); |
| 155 | +}); |
| 156 | + |
| 157 | +/* |
| 158 | +$wizard->addStep('More Ways', function ($page) { |
| 159 | + $page->add(new Demo(['left_width'=>5, 'right_width'=>11]))->setCode( |
| 160 | + <<<'CODE' |
| 161 | +$m = new Stat($app->db); |
| 162 | +$m->addAction('mail', [ |
| 163 | + 'fields' => ['currency_field'], |
| 164 | + 'scope' => \atk4\data\UserAction\Generic::SINGLE_RECORD, |
| 165 | + 'callback' => function() { return 'testing'; }, |
| 166 | + 'description' => 'Email testing', |
| 167 | +]); |
| 168 | +$app->add('CardDeck') |
| 169 | + ->setModel( |
| 170 | + $m, |
| 171 | + ['description'] |
| 172 | + ); |
| 173 | +CODE |
| 174 | + ); |
| 175 | +}); |
| 176 | +*/ |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | +$wizard->addStep('CRUD integration', function ($page) { |
| 181 | + $t = $page->add('Text'); |
| 182 | + $t->addParagraph( |
| 183 | + <<< 'EOF' |
| 184 | +Compared to 1.x versions CRUD implementation has became much more lightweight, however you retain all the same |
| 185 | +functionality and more. Next example shows how you can disable action (add) entirely, or on per-row basis (delete) |
| 186 | +and how you could add your own action with a custom trigger button and even a preview. |
| 187 | +EOF |
| 188 | + ); |
| 189 | + |
| 190 | + |
| 191 | + $page->add(new Demo())->setCode( |
| 192 | + <<<'CODE' |
| 193 | +$country = new Country($app->app->db); |
| 194 | +$country->getAction('add')->enabled = false; |
| 195 | +$country->getAction('delete')->enabled = function() { return rand(1,2)>1; }; |
| 196 | +$country->addAction('mail', [ |
| 197 | + 'scope' => \atk4\data\UserAction\Generic::SINGLE_RECORD, |
| 198 | + 'preview' => function($m) { return 'here is email preview for '.$m['name']; }, |
| 199 | + 'callback' => function($m) { return 'email sent to '.$m['name']; }, |
| 200 | + 'description' => 'Email testing', |
| 201 | + 'ui' => ['icon'=>'mail', 'button'=>[null, 'icon'=>'green mail']], |
| 202 | +]); |
| 203 | +$crud = $app->add(['CRUD', 'ipp'=>5]); |
| 204 | +$crud->setModel($country, ['name','iso']); |
| 205 | +CODE |
| 206 | + ); |
| 207 | +}); |
| 208 | + |
| 209 | +$wizard->addFinish(function ($page) use ($wizard) { |
| 210 | + PromotionText::addTo($page); |
| 211 | + \atk4\ui\Button::addTo($wizard, ['Exit demo', 'primary', 'icon'=>'left arrow'], ['Left']) |
| 212 | + ->link(['begin'=>false, 'layout'=>false]); |
| 213 | +}); |
0 commit comments