View_CRUD::addAction PHP Method

addAction() public method

Assuming that your model contains a certain method, this allows you to create a frame which will pop you a new frame with a form representing model method arguments. Once the form is submitted, the action will be evaluated.
public addAction ( string $method_name, array $options = [] )
$method_name string
$options array
    public function addAction($method_name, $options = array())
    {
        if (!$this->model) {
            throw $this->exception('Must set CRUD model first');
        }
        if ($options == 'toolbar') {
            $options = array('column' => false);
        }
        if ($options == 'column') {
            $options = array('toolbar' => false);
        }
        $descr = $options['descr'] ?: ucwords(str_replace('_', ' ', $method_name));
        $icon = $options['icon'] ?: 'target';
        $show_toolbar = isset($options['toolbar']) ? $options['toolbar'] : true;
        $show_column = isset($options['column']) ? $options['column'] : true;
        if ($this->isEditing($method_name)) {
            /** @type View_Console $c */
            $c = $this->virtual_page->getPage()->add('View_Console');
            $self = $this;
            // Callback for the function
            $c->set(function ($c) use($show_toolbar, $show_column, $options, $self, $method_name) {
                if ($show_toolbar && !$self->id) {
                    $self->model->unload();
                } elseif ($show_column && $self->id) {
                    $c->out('Loading record ' . $self->id, array('class' => 'atk-effect-info'));
                    $self->model->load($self->id);
                } else {
                    return;
                }
                $ret = $self->model->{$method_name}();
                $c->out('Returned: ' . json_encode($ret, JSON_UNESCAPED_UNICODE), array('class' => 'atk-effect-success'));
                /*
                if (isset($options['args'])) {
                    $params = $options['args'];
                } elseif (!method_exists($self->model, $method_name)) {
                    // probably a dynamic method
                    $params = array();
                } else {
                    $reflection = new ReflectionMethod($self->model, $method_name);
                
                    $params = $reflection->getParameters();
                }
                */
            });
            return;
            /* unused code below
            
                        $has_parameters = (bool) $params;
                        foreach ($params as $i => $param) {
                            $this->form->addField($param->name);
                            $this->has_parameters = true;
                        }
            
                        if (!$has_parameters) {
                            $this->form->destroy();
                            $ret = $this->model->$method_name();
                            if (is_object($ret) && $ret == $this->model) {
                                $this->virtual_page->getPage()->add('P')->set('Executed successfully');
                                $this->virtual_page->getPage()->js(true, $this->js_reload);
                            } else {
                                $this->virtual_page->getPage()->js(true, $this->js_reload);
                                if (is_object($ret)) {
                                    $ret = (string) $ret;
                                }
                                $this->virtual_page->getPage()
                                    ->add('P')->set('Returned: '.json_encode($ret, JSON_UNESCAPED_UNICODE));
                            }
                            $this->virtual_page->getPage()
                                ->add('Button')->set(array('Close', 'icon' => 'cross', 'swatch' => 'green'))
                                ->js('click')->univ()->closeDialog();
            
                            return true;
                        }
            
                        $this->form->addSubmit('Execute');
                        if ($this->form->isSubmitted()) {
                            $ret = call_user_func_array(array($this->model, $method_name), array_values($this->form->get()));
                            if (is_object($ret)) {
                                $ret = (string) $ret;
                            }
                            $this->js(null, $this->js()->reload())->univ()
                                ->successMessage('Returned: '.json_encode($ret, JSON_UNESCAPED_UNICODE))
                                ->closeDialog()
                                ->execute();
                        }
            
                        return true;
                        */
        } elseif ($this->isEditing()) {
            return;
        }
        $frame_options = array_merge(array(), $this->frame_options ?: array());
        if ($show_column) {
            $this->virtual_page->addColumn($method_name, $descr . ' ' . $this->entity_name, array('descr' => $descr, 'icon' => $icon), $this->grid);
        }
        if ($show_toolbar) {
            $button = $this->addButton(array($descr, 'icon' => $icon));
            // Configure Add Button on Grid and JS
            $button->js('click')->univ()->frameURL($this->app->_($this->entity_name . '::' . $descr), $this->virtual_page->getURL($method_name), $frame_options);
        }
    }