Stash::get_bundle PHP Method

get_bundle() public method

Restore values for a given bundle
public get_bundle ( $set = TRUE ) : string
return string
    public function get_bundle($set = TRUE)
    {
        /* Sample use
           ---------------------------------------------------------
           {exp:stash:get_bundle name="contact_form" context="@" limit="5"}
               {contact_name}
           {/exp:stash:get_bundle}
           --------------------------------------------------------- */
        $out = $this->EE->TMPL->tagdata;
        if (!!($bundle = $this->EE->TMPL->fetch_param('name', FALSE))) {
            // get the bundle id, cache to memory for efficient reuse later
            $bundle_id = $this->EE->stash_model->get_bundle_by_name($bundle);
            // does this bundle already exist?
            if ($bundle_id) {
                $bundle_array = array();
                $tpl = $this->EE->TMPL->tagdata;
                $this->bundle_id = $bundle_id;
                // get params
                $unique = (bool) preg_match('/1|on|yes|y/i', $this->EE->TMPL->fetch_param('unique', 'yes'));
                $index = $this->EE->TMPL->fetch_param('index', NULL);
                $context = $this->EE->TMPL->fetch_param('context', NULL);
                $scope = strtolower($this->EE->TMPL->fetch_param('scope', 'user'));
                // user|site
                // if this is a unique bundle, restore the bundled variables to static bundles array
                if ($unique || !is_null($index)) {
                    if ($index !== NULL && $index > 0) {
                        $bundle .= '_' . $index;
                        $this->EE->TMPL->tagparams['name'] = $bundle;
                    }
                    // get bundle var
                    $bundle_entry_key = $bundle;
                    if ($bundle !== NULL && count(explode(':', $bundle) == 1)) {
                        $bundle_entry_key = $context . ':' . $bundle;
                    }
                    $session_id = $scope === 'user' ? $this->_session_id : '';
                    $bundle_entry_key = $this->_parse_context($bundle_entry_key);
                    // look for our key
                    if ($bundle_value = $this->EE->stash_model->get_key($bundle_entry_key, $this->bundle_id, $session_id, $this->site_id)) {
                        $bundle_array[0] = unserialize($bundle_value);
                        foreach ($bundle_array[0] as $key => $val) {
                            self::$bundles[$bundle][$key] = $val;
                        }
                    }
                } else {
                    // FUTURE FEATURE: get all entries for a bundle with *multiple* rows
                }
                // replace into template
                if (!empty($tpl)) {
                    // take care of any unparsed current context pointers '@'
                    if (!is_null($context)) {
                        $tpl = str_replace(LD . '@:', LD . $context . ':', $tpl);
                    }
                    if (!empty($bundle_array)) {
                        $out = '';
                        foreach ($bundle_array as $vars) {
                            $out .= $this->EE->functions->var_swap($tpl, $vars);
                            // set variables
                            if ($set) {
                                foreach ($vars as $name => $value) {
                                    $this->EE->TMPL->tagparams['name'] = $name;
                                    $this->EE->TMPL->tagparams['type'] = 'variable';
                                    $this->EE->TMPL->tagdata = $value;
                                    $this->replace = TRUE;
                                    $this->_run_tag('set', array('name', 'type', 'scope', 'context'));
                                }
                            }
                        }
                    }
                    // prep 'IN' conditionals if the retreived var is a delimited string
                    if ($this->parse_if_in) {
                        $out = $this->_prep_in_conditionals($out);
                    }
                }
                $this->EE->TMPL->log_item("Stash: RETRIEVED bundle " . $bundle);
            }
        }
        return $out;
    }