function run() { /**@var tf_sat $sat */ $sat = core::module('sat'); $site = $sat->get_current_site(); $json_file = loader::get_public('assets/' . $site->domain . '.json'); $last_mod = @filemtime($json_file); $tree_last_mod = 0; if ($last_mod) { $last_node = $sat->get_node_handle()->set_where('site_id = %d', $sat->get_current_site_id())->set_order('updated_at DESC')->load_first(); $tree_last_mod = $last_node ? $last_node->updated_at : 0; core::dprint(__METHOD__ . ': cached'); // uptodate if ($tree_last_mod <= $last_mod) { $this->renderer->set_ajax_answer(file_get_contents($json_file))->ajax_flush(); return; } } core::dprint(__METHOD__ . ': fetch!'); $tree = $sat->get_current_site_tree(); $allowedKeys = array('title', 'url'); array_walk($tree, function (&$v) use($allowedKeys) { $v = array_intersect_key($v, array_flip($allowedKeys)); }); $tree = array_values($tree); // cache file_put_contents($json_file, functions::json_encode($tree)); $this->renderer->set_ajax_answer($tree)->ajax_flush(); }
/** * Convert to json * @return string json */ function as_json($fields = false, $assoc = false) { $out = array(); if (!empty($this->items)) { foreach ($this->items as $id => $v) { $row = $v->as_json($fields); if ($assoc) { $out[$id] = $row; } else { $out[] = $row; } } } return functions::json_encode($out); }
/** * Triggers in ajax mode call * Template assign through: @see get_main_template() * called from @see core::shutdown */ public function output_ajax($is_return = false) { /* //disabled in core if ($console = core::lib('console')) { $console->disable(); } */ $this->output_begin(array('in_ajax' => true)); // output_ajax conflict in editor // @todo test this if (loader::in_ajax() === 'json') { $this->set_ajax_type('json'); } if (core::in_editor()) { $this->output_editor(); } $tpl = $this->get_main_template(); if ($tpl) { $tpl = substr($tpl, 0, -4); // output_end will append .TPL } if (!$is_return && self::AJAX_JSON == @$this->ajax_answer['type']) { $this->content_type_header('application/json', 'utf-8'); } /** * JSON answer */ if (!empty($tpl) && self::AJAX_JSON == @$this->ajax_answer['type']) { // parse template, if any $buffer = $this->output_end($tpl, true); // output ajax json data $return = is_array($this->ajax_answer['data']) ? $this->ajax_answer['data'] : array(); // if rendered with template if (isset($buffer)) { $return['data'] = $buffer; } // send message if (empty($tpl) && !empty($this->_message['message'])) { $return['message'] = $this->_message['message']; } echo json_encode($return); } elseif (empty($tpl)) { // allow prepared json echo self::AJAX_JSON == $this->ajax_answer['type'] ? is_string($this->ajax_answer['data']) ? $this->ajax_answer['data'] : functions::json_encode($this->ajax_answer['data']) : @$this->ajax_answer['data']['data']; } else { // plain output $this->output_end($tpl); } }