View_CRUD::addRef PHP Method

addRef() public method

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. 'view_model' => model or callback // Use custom model for sub-View, by default ref($name) will be used '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 )
public addRef ( string $name, array $options = [] ) : View_CRUD | null
$name string Name of the reference. If you leave blank adds all
$options array Customizations, see above
return View_CRUD | null Returns crud object, when expanded page is rendered
    public 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->grid || $this->grid instanceof Dummy)return;
        $s = $this->app->normalizeName($name);
        if ($this->isEditing('ex_' . $s)) {
            $n = $this->virtual_page->name . '_' . $s;
            if ($_GET[$n]) {
                $this->id = $_GET[$n];
                $this->app->stickyGET($n);
            }
            $idfield = $this->model->table . '_' . $this->model->id_field;
            if ($_GET[$idfield]) {
                $this->id = $_GET[$idfield];
                $this->app->stickyGET($idfield);
            }
            $view_class = is_null($options['view_class']) ? get_class($this) : $options['view_class'];
            $subview = $this->virtual_page->getPage()->add($view_class, $options['view_options']);
            $this->model->load($this->id);
            $subview->setModel($options['view_model'] ? is_callable($options['view_model']) ? call_user_func($options['view_model'], $this->model) : $options['view_model'] : $this->model->ref($name), $options['fields'], $options['grid_fields'] ?: $options['extra_fields']);
            return $subview;
        } elseif ($this->grid instanceof Grid) {
            $this->grid->addColumn('expander', 'ex_' . $s, $options['label'] ?: $s);
            $this->grid->columns['ex_' . $s]['page'] = $this->virtual_page->getURL('ex_' . $s);
            // unused: $idfield = $this->grid->columns['ex_'.$s]['refid'].'_'.$this->model->id_field;
        }
        if ($this->isEditing()) {
            return;
        }
    }