mahi /atk4/lib/View/CRUD.php

Language PHP Lines 479
MD5 Hash 315cea6b3f67ba948a86166293aff329
Repository https://github.com/mahimarathore/mahi.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
<?php // vim:ts=4:sw=4:et:fdm=marker
/**
==ATK4===================================================
   This file is part of Agile Toolkit 4
    http://agiletoolkit.org/

   (c) 2008-2013 Agile Toolkit Limited <info@agiletoolkit.org>
   Distributed under Affero General Public License v3 and
   commercial license.

   See LICENSE or LICENSE_COM for more information
 =====================================================ATK4=*/
/**
 * CRUD stands for Create, Read, Update and Delete. This view combines
 * both "Grid", "Form" and "VirtualPage" to bring you a seamless editing
 * control. You would need to supply a model.
 *
 * IMPORTANT NOTE: While you can disable adding and editing, if you do that
 * you must simply use Grid!
 *
 * @link http://agiletoolkit.org/
 */
class View_CRUD extends View
{
    /**
     * After CRUD is initialized, this will point to a form object IF
     * CRUD goes into editing mode. Typically the same code is initialized
     * for editing pop-up, but only form is rendered. You can enhance the
     * form all you want
     *
     * IMPORTANT: check isEditing() method
     */
    public $form=null;

    /**
     * After CRUD is initialized, this will point do a Grid object, IF
     * CRUD is in "read" mode. You can add more columns or actions to the
     * grid.
     *
     * IMPORTANT: check isEditing() method
     */
    public $grid=null;

    /**
     * By default, CRUD will simply use "Grid" class, but if you would like
     * to use your custom grid class for listing, specify it inside associative
     * array as second argument to add()
     *
     * $this->add('CRUD', array('grid_class'=>'MyGrid'));
     */
    public $grid_class='Grid';

    /**
     * By default, CRUD will simply use "Form" class for editing and adding,
     * but if you would like to use your custom form, specify it inside
     * associative array as second argument to add()
     *
     * $this->add('CRUD', array('grid_class'=>'MyGrid'));
     */
    public $form_class='Form';

    /**
     * Grid will contain an "Add X" button and will allow user to add records
     *
     * $this->add('CRUD', array('allow_add'=>false')); // to disable
     */
    protected $allow_add=true;

    /**
     * Grid will contain "EDIT" button for each row allowing usir to edit
     * records
     *
     * $this->add('CRUD', array('allow_edit'=>false')); // to disable
     */
    protected $allow_edit=true;

    /**
     * Grid will contain a "DELETE" button for each row. If you don't want
     * thes set this option to false
     *
     * $this->add('CRUD', array('allow_del'=>false')); // to disable
     */
    protected $allow_del=true;

    /**
     * For ->setModel('User'), your add button would contain "Add User". If
     * you want add button and frames to use different label, then change
     * this property.
     *
     * If you set this to 'false' then CRUD will not attempt to change
     * default label ("Add")
     */
    public $entity_name=null;

    /**
     * This points to a Button object, which you can change if you want
     * a different label or anything else on it
     */
    public $add_button;

    /**
     * VirtualPage object will be used to display popup content. That is to ensure
     * that none of your other content you put AROUND the CRUD would mess
     * with the forms.
     *
     * If isEditing() then you can add more stuff on this page, by calling
     * virtual_page->getPage()->add('Hello!');
     */
    public $virtual_page=null;

    /**
     * When clicking on EDIT or ADD the frameURL is used. If you want to pass
     * some arguments to it, put your hash here.
     */
    public $frame_options=null;

    /**
     * This is set to ID of the model when are in editing mode. In theory
     * this can also be 0, so use is_null()
     */
    public $id=null;

    /**
     * {@inheritdoc}
     *
     * CRUD's init() will create either a grid or form, depending on
     * isEditing(). You can then do the necessary changes after
     *
     * Note, that the form or grid will not be populated until you
     * call setModel()
     *
     * @return void
     */
    function init()
    {
        parent::init();

        // Virtual Page would receive 3 types of requests - add, delete, edit
        $this->virtual_page = $this->add('VirtualPage', array(
            'frame_options'=>$this->frame_options
        ));


        if ($_GET['edit']) {
            $_GET[$this->name.'_id'] = $_GET['edit'];
        }

        if (isset($_GET[$this->name.'_id'])) {
            $this->api->stickyGET($this->name.'_id');
            $this->id=$_GET[$this->name.'_id'];
        }

        if ($this->isEditing()) {
            $this->form = $this
                ->virtual_page
                ->getPage()
                ->add($this->form_class);

            return;
        }

        $this->grid=$this->add($this->grid_class);

        // Left for compatibility
        $this->js('reload', $this->grid->js()->reload());

        if ($this->allow_add) {
            $this->add_button = $this->grid->addButton('Add');
        }
    }

    /**
     * Returns if CRUD is in editing mode or not. It's preferable over
     * checking if($grid->form)
     *
     * @param string $mode Specify which editing mode you expect
     *
     * @return boolean true if editing.
     */
    function isEditing($mode = null)
    {
        $page_mode = $this->virtual_page->isActive();

        // Requested edit, but not allowed
        if ($page_mode=='edit' && !$this->allow_edit) {
            throw $this->exception('Editing is not allowed');
        }

        // Requested add but not allowed
        if ($page_mode=='add' && !$this->allow_add) {
            throw $this->exception('Adding is not allowed');
        }

        // Request matched argument exactly
        if (!is_null($mode)) {
            return $mode === $page_mode;
        }

        // Argument was blank, then edit/add is OK
        return $page_mode=='edit' || $page_mode=='add';
    }

    /**
     * Obsolete YEEE
     *
     * @obsolete
     */
    function setController($controller){
        if($this->form){
            $this->form->setController($controller);
            $this->form->addSubmit('Save');
        }elseif($this->grid){
            $this->grid->setController($controller);
        }
        $this->initComponents();
    }

    /**
     * Assign model to your CRUD and specify list of fields to use from model
     *
     * {@inheritdoc}
     *
     * @param string|object $model       Same as parent
     * @param array         $fields      Specify list of fields for form and grid
     * @param array         $grid_fields Overide list of fields for the grid
     *
     * @return AbstractModel $model
     */
    function setModel($model, $fields = null, $grid_fields = null)
    {
        $model=parent::setModel($model);

        if ($this->entity_name === null) {

            // Calculates entity name
            $this->entity_name = str_replace(
                array('Model_', '_'),
                array('', ' '),
                get_class($this->model)
            );
        }

        if (!$this->isEditing()) {
            $this->configureGrid(is_null($grid_fields)?$fields:$grid_fields);
        }

        if ($this->allow_add) {
            if ($this->configureAdd($fields)) {
                return $model;
            }
        }

        if ($this->allow_edit) {
            if ($this->configureEdit($fields)) {
                return $model;
            }
        }

        if ($this->allow_del) {
            $this->configureDel();
        }

        return $model;
    }

    /**
     * Adds expander to the crud, which edits references under the specified
     * name. Returns object of nested CRUD when active, or null
     *
     * The format of $options is the following:
     * array (
     *   'view_class' => 'CRUD',  // Which View to use inside expander
     *   'view_options' => ..     // Second arg when adding view.
     *   'fields' => array()      // Used as second argument for setModel()
     *   'extra_fields' => array() // Third arguments to setModel() used by CRUDs
     *   'label'=> 'Click Me'     // Label for a button inside a grid
     * )
     *
     * @param string $name    Name of the reference. If you leave blank adds all
     * @param array  $options Customizations, see above
     *
     * @return View_CRUD|null Returns crud object, when expanded page is rendered
     */
    function addRef($name, $options = array())
    {
        if (!$this->model) {
            throw $this->exception('Must set CRUD model first');
        }

        if (!is_array($options)) {
            throw $this->exception('Must be array');
        }

        if ($this->isEditing('ex_'.$name)) {

            if ($_GET['id']) {
                $this->id = $_GET[$this->name.'_id'] = $_GET['id'];
                $this->api->stickyGET($this->name.'_id');
            }

            $view_class = (is_null($options['view_class']))?
                get_class($this):
                $options['view_class'];

            $subview=$this->virtual_page->getPage()->add(
                $view_class,
                $options['view_options']
            );

            $subview->setModel(
                $this->model->load($this->id)->ref($name),
                $options['fields'],
                $options['extra_fields']
            );
            return $subview;
        }

        if ($this->isEditing()) {
            return;
        }

        $this->grid->addColumn('expander', 'ex_'.$name, $options['label']?:$name);
        $this->grid->columns['ex_'.$name]['page']
            = $this->virtual_page->getURL('ex_'.$name);
    }

    /**
     * Adds button to the crud, which opens a new frame and returns page to
     * you. Add anything into the page as you see fit. The ID of the record
     * will be inside $crud->id
     *
     * @param string $name    Unique name, also button and title default
     * @param array  $options Options
     *
     * @return Page|null Returns object if clicked on popup.
     */
    function addFrame($name, $options = array())
    {
        if (!$this->model) {
            throw $this->exception('Must set CRUD model first');
        }

        if (!is_array($options)) {
            throw $this->exception('Must be array');
        }

        if ($this->isEditing('fr_'.$name)) {

            if ($_GET['fr_'.$name]) {
                $this->id = $_GET[$this->name.'_id'] = $_GET['fr_'.$name];
                $this->api->stickyGET($this->name.'_id');
            }

            return $this->virtual_page->getPage();
        }

        if ($this->isEditing()) {
            return;
        }

        $this
            ->virtual_page
            ->addColumn('fr_'.$name, $options['title']?:$name, $options['label'], $this->grid)
            ;
    }

    /**
     * Configures necessary components when CRUD is in the adding mode
     *
     * @param array $fields List of fields for add form
     *
     * @return void|Model If model, then bail out, no greed needed
     */
    function configureAdd($fields)
    {
        // We are actually in the frame!
        if ($this->isEditing('add')) {
            $m = $this->form->setModel($this->model, $fields);
            $this->form->addSubmit('Add');
            $this->form->onSubmit(array($this,'formSubmit'));
            return $m;
        } elseif ($this->isEditing()) return;

        // Configure Add Button on Grid and JS
        $this->add_button->js('click')->univ()
            ->frameURL(
                $this->entity_name===false?
                'New Record':
                'Adding new '.$this->entity_name,
                $this->virtual_page->getURL('add')
            );

        if ($this->entity_name !== false) {
            $this->add_button->setLabel('Add '.$this->entity_name);
        }
    }

    /**
     * Configures necessary components when CRUD is in the editing mode
     *
     * @param array $fields List of fields for add form
     *
     * @return void|Model If model, then bail out, no greed needed
     */
    function configureEdit($fields)
    {
        // We are actually in the frame!
        if ($this->isEditing('edit')) {
            $m = $this->form->setModel($this->model, $fields);
            $m->load($this->id);
            $this->form->addSubmit();
            $this->form->onSubmit(array($this,'formSubmit'));
            return $m;
        } elseif ($this->isEditing()) return;


        $this
            ->virtual_page
            ->addColumn('edit', 'Editing '.$this->entity_name, 'Edit', $this->grid)
            ;
    }

    /**
     * Configures grid's model itself
     *
     * @param array $fields List of fields for grid
     *
     * @return void
     */
    function configureGrid($fields)
    {
        $this->grid->setModel($this->model, $fields);
    }

    /**
     * Configures deleting functionality for grid
     *
     * @return void
     */
    function configureDel()
    {
        $this->grid->addColumn('delete', 'delete');
    }


    /**
     * Called after on post-init hook when form is submitted
     *
     * @param Form $form Form which was submitted
     *
     * @return void
     */
    function formSubmit($form)
    {
        try {
            $form->update();
            $self = $this;
            $this->api->addHook('pre-render', function () use ($self) {
                $self->formSubmitSuccess()->execute();
            });
        } catch (Exception_ValidityCheck $e) {
            $form->displayError($e->getField(), $e->getMessage());
        }
    }


    /**
     * Returns JavaScript action which should be executed on form successfull
     * submission
     *
     * @return jQuery_Chain to be executed on successful submit
     */
    function formSubmitSuccess()
    {
        return $this->form->js(null, $this->js()->trigger('reload'))
            ->univ()->closeDialog();
    }
}
Back to Top