public function get_filtered_listing_from_index($opts, $where_callback, $pre_template_callback = null) { $Perch = Perch::fetch(); $index_table = PERCH_DB_PREFIX . $this->index_table; $where = array(); $filter_mode = false; $single_mode = false; $content = array(); // find specific _id if (isset($opts['_id'])) { $item_id = (int) $opts['_id']; $Paging = false; $sql = 'SELECT main.* FROM ' . $this->table . ' main WHERE main.' . $this->pk . '=' . $this->db->pdb($item_id) . ' LIMIT 1 '; $rows = $this->db->get_rows($sql); $single_mode = true; } else { $sortval = ' idx2.indexValue as sortval '; if (isset($opts['paginate']) && $opts['paginate']) { if (isset($opts['pagination-var'])) { $Paging = new PerchPaging($opts['pagination-var']); } else { $Paging = new PerchPaging(); } $sql = $Paging->select_sql(); } else { $sql = 'SELECT'; } $sql .= ' tbl.* FROM ( SELECT idx.itemID, main.*, ' . $sortval . ' FROM ' . $index_table . ' idx JOIN ' . $this->table . ' main ON idx.itemID=main.' . $this->pk . ' AND idx.itemKey=' . $this->db->pdb($this->pk) . ' JOIN ' . $index_table . ' idx2 ON idx.itemID=idx2.itemID AND idx.itemKey=' . $this->db->pdb($this->pk) . ' '; if (isset($opts['sort'])) { $sql .= ' AND idx2.indexKey=' . $this->db->pdb($opts['sort']) . ' '; } else { $sql .= ' AND idx2.indexKey=' . $this->db->pdb('_id') . ' '; } $where_clause = ' idx.itemKey=' . $this->db->pdb($this->pk) . ' '; // Categories if (isset($opts['category']) && !$this->bypass_categories) { $cats = $opts['category']; if (!is_array($cats)) { $cats = array($cats); } $match = 'any'; if (isset($opts['category-match'])) { $match = strtolower($opts['category-match']) == 'any' ? 'any' : 'all'; } $pos = array(); $neg = array(); if (count($cats)) { foreach ($cats as $cat) { if (substr($cat, 0, 1) == '!') { $neg[] = substr($cat, 1); } else { $pos[] = $cat; } } $sql .= $this->_get_filter_sub_sql('_category', $pos, false, $match, true, $where_clause); $sql .= $this->_get_filter_sub_sql('_category', $neg, true, $match, true, $where_clause); } } // Tags if (isset($opts['tag']) && !$this->bypass_tags) { $cats = $opts['tag']; if (!is_array($cats)) { $cats = array($cats); } $match = 'any'; if (isset($opts['tag-match'])) { $match = strtolower($opts['tag-match']) == 'any' ? 'any' : 'all'; } $pos = array(); $neg = array(); if (count($cats)) { foreach ($cats as $cat) { if (substr($cat, 0, 1) == '!') { $neg[] = substr($cat, 1); } else { $pos[] = $cat; } } $sql .= $this->_get_filter_sub_sql('_tag', $pos, false, $match, false, $where_clause); $sql .= $this->_get_filter_sub_sql('_tag', $neg, true, $match, false, $where_clause); } } // Runtime restrictons if (!$Perch->admin && count($this->runtime_restrictons)) { foreach ($this->runtime_restrictons as $res) { $sql .= $this->_get_filter_sub_sql($res['field'], $res['values'], $res['negative_match'], $res['match'], $res['fuzzy'], $where_clause); } } // if not picking an _id, check for a filter if (isset($opts['filter']) && (isset($opts['value']) || is_array($opts['filter']))) { // if it's not a multi-filter, make it look like one to unify what we're working with if (!is_array($opts['filter']) && isset($opts['value'])) { $filters = array(array('filter' => $opts['filter'], 'value' => $opts['value'], 'match' => isset($opts['match']) ? $opts['match'] : 'eq', 'match-type' => isset($opts['match-type']) ? $opts['match-type'] : 'alpha')); $filter_mode = 'AND'; } else { $filters = $opts['filter']; $filter_mode = 'AND'; if (isset($opts['match']) && strtolower($opts['match']) == 'or') { $filter_mode = 'OR'; } } $where = array(); foreach ($filters as $filter) { $key = $filter['filter']; $val = $filter['value']; $match = isset($filter['match']) ? $filter['match'] : 'eq'; if (is_numeric($val)) { $val = (double) $val; } switch ($match) { case 'eq': case 'is': case 'exact': $where[] = '(idx.indexKey=' . $this->db->pdb($key) . ' AND idx.indexValue=' . $this->db->pdb($val) . ')'; break; case 'neq': case 'ne': case 'not': case '!eq': $where[] = '(idx.indexKey=' . $this->db->pdb($key) . ' AND idx.indexValue != ' . $this->db->pdb($val) . ')'; break; case 'gt': $where[] = '(idx.indexKey=' . $this->db->pdb($key) . ' AND idx.indexValue > ' . $this->db->pdb($val) . ')'; break; case '!gt': $where[] = '(idx.indexKey=' . $this->db->pdb($key) . ' AND idx.indexValue !> ' . $this->db->pdb($val) . ')'; break; case 'gte': $where[] = '(idx.indexKey=' . $this->db->pdb($key) . ' AND idx.indexValue >= ' . $this->db->pdb($val) . ')'; break; case '!gte': $where[] = '(idx.indexKey=' . $this->db->pdb($key) . ' AND idx.indexValue !>= ' . $this->db->pdb($val) . ')'; break; case 'lt': $where[] = '(idx.indexKey=' . $this->db->pdb($key) . ' AND idx.indexValue < ' . $this->db->pdb($val) . ')'; break; case '!lt': $where[] = '(idx.indexKey=' . $this->db->pdb($key) . ' AND idx.indexValue !< ' . $this->db->pdb($val) . ')'; break; case 'lte': $where[] = '(idx.indexKey=' . $this->db->pdb($key) . ' AND idx.indexValue <= ' . $this->db->pdb($val) . ')'; break; case '!lte': $where[] = '(idx.indexKey=' . $this->db->pdb($key) . ' AND idx.indexValue !<= ' . $this->db->pdb($val) . ')'; break; case 'contains': $v = str_replace('/', '\\/', $val); $where[] = '(idx.indexKey=' . $this->db->pdb($key) . ' AND idx.indexValue REGEXP ' . $this->db->pdb('[[:<:]]' . $v . '[[:>:]]') . ')'; break; case 'notcontains': case '!contains': $v = str_replace('/', '\\/', $val); $where[] = '(idx.indexKey=' . $this->db->pdb($key) . ' AND idx.indexValue NOT REGEXP ' . $this->db->pdb('[[:<:]]' . $v . '[[:>:]]') . ')'; break; case 'regex': case 'regexp': $v = str_replace('/', '\\/', $val); $where[] = '(idx.indexKey=' . $this->db->pdb($key) . ' AND idx.indexValue REGEXP ' . $this->db->pdb($v) . ')'; break; case 'between': case 'betwixt': $vals = explode(',', $val); if (PerchUtil::count($vals) == 2) { $vals[0] = trim($vals[0]); $vals[1] = trim($vals[1]); if (is_numeric($vals[0]) && is_numeric($vals[1])) { $vals[0] = (double) $vals[0]; $vals[1] = (double) $vals[1]; } $where[] = '(idx.indexKey=' . $this->db->pdb($key) . ' AND (idx.indexValue > ' . $this->db->pdb($vals[0]) . ' AND idx.indexValue < ' . $this->db->pdb($vals[1]) . '))'; } break; case '!between': case '!betwixt': $vals = explode(',', $val); if (PerchUtil::count($vals) == 2) { $vals[0] = trim($vals[0]); $vals[1] = trim($vals[1]); if (is_numeric($vals[0]) && is_numeric($vals[1])) { $vals[0] = (double) $vals[0]; $vals[1] = (double) $vals[1]; } $where[] = '(idx.indexKey=' . $this->db->pdb($key) . ' AND (idx.indexValue !> ' . $this->db->pdb($vals[0]) . ' AND idx.indexValue !< ' . $this->db->pdb($vals[1]) . '))'; } break; case 'eqbetween': case 'eqbetwixt': $vals = explode(',', $val); if (PerchUtil::count($vals) == 2) { $vals[0] = trim($vals[0]); $vals[1] = trim($vals[1]); if (is_numeric($vals[0]) && is_numeric($vals[1])) { $vals[0] = (double) $vals[0]; $vals[1] = (double) $vals[1]; } $where[] = '(idx.indexKey=' . $this->db->pdb($key) . ' AND (idx.indexValue >= ' . $this->db->pdb($vals[0]) . ' AND idx.indexValue <= ' . $this->db->pdb($vals[1]) . '))'; } break; case '!eqbetween': case '!eqbetwixt': $vals = explode(',', $val); if (PerchUtil::count($vals) == 2) { $vals[0] = trim($vals[0]); $vals[1] = trim($vals[1]); if (is_numeric($vals[0]) && is_numeric($vals[1])) { $vals[0] = (double) $vals[0]; $vals[1] = (double) $vals[1]; } $where[] = '(idx.indexKey=' . $this->db->pdb($key) . ' AND (idx.indexValue !>= ' . $this->db->pdb($vals[0]) . ' AND idx.indexValue !<= ' . $this->db->pdb($vals[1]) . '))'; } break; case 'in': case 'within': $vals = explode(',', $val); if (PerchUtil::count($vals)) { $where[] = '(idx.indexKey=' . $this->db->pdb($key) . ' AND idx.indexValue IN (' . $this->db->implode_for_sql_in($vals) . '))'; } break; case '!in': case '!within': $vals = explode(',', $val); if (PerchUtil::count($vals)) { $where[] = '(idx.indexKey=' . $this->db->pdb($key) . ' AND idx.indexValue NOT IN (' . $this->db->implode_for_sql_in($vals) . '))'; } break; } } } $sql .= ' WHERE 1=1 '; if (PerchUtil::count($where)) { $sql .= ' AND (' . implode($where, ' OR ') . ') '; } $sql .= ' AND idx.itemID=idx2.itemID AND idx.itemKey=idx2.itemKey GROUP BY idx.itemID ) as tbl '; $where = array(); if (is_callable($where_callback)) { // load up Query object $Query = new PerchQuery(); $Query->select = $sql; $Query->where = $where; // do callback $Query = $where_callback($Query); // retrieve $sql = $Query->select; $where = $Query->where; } if (PerchUtil::count($where)) { $sql .= ' WHERE (' . implode($where, ' AND ') . ') '; } $sql .= 'GROUP BY itemID '; if ($filter_mode == 'AND' && PerchUtil::count($filters) > 1) { $sql .= ' HAVING count(*)=' . PerchUtil::count($filters) . ' '; } // sort if (isset($opts['sort'])) { $direction = 'ASC'; if (isset($opts['sort-order'])) { switch ($opts['sort-order']) { case 'DESC': case 'desc': $direction = 'DESC'; break; case 'RAND': case 'rand': $direction = 'RAND'; break; default: $direction = 'ASC'; break; } } if ($direction == 'RAND') { $sql .= ' ORDER BY RAND()'; } else { if (isset($opts['sort-type']) && $opts['sort-type'] == 'numeric') { $sql .= ' ORDER BY sortval * 1 ' . $direction . ' '; } else { $sql .= ' ORDER BY sortval ' . $direction . ' '; } } } else { if (isset($opts['sort-type']) && $opts['sort-type'] == 'numeric') { $sql .= ' ORDER BY sortval * 1 ASC '; } else { $sql .= ' ORDER BY sortval ASC '; } } // Pagination if (isset($opts['paginate']) && $opts['paginate']) { if (is_object($opts['paginate'])) { $Paging = $opts['paginate']; } else { if (isset($opts['pagination-var'])) { $Paging = new PerchPaging($opts['pagination-var']); } else { $Paging = new PerchPaging(); } $Paging->set_per_page(isset($opts['count']) ? (int) $opts['count'] : 10); } $opts['count'] = $Paging->per_page(); $opts['start'] = $Paging->lower_bound() + 1; } else { $Paging = false; } // limit if (isset($opts['count']) || isset($opts['start'])) { // count if (isset($opts['count'])) { $count = (int) $opts['count']; } else { $count = false; } // start if (isset($opts['start'])) { $start = (int) $opts['start'] - 1; } else { $start = 0; } if (is_object($Paging)) { $sql .= $Paging->limit_sql(); } else { $sql .= ' LIMIT ' . $start; if ($count) { $sql .= ', ' . $count; } } } $rows = $this->db->get_rows($sql); if (is_object($Paging)) { $total_count = $this->db->get_value($Paging->total_count_sql()); $Paging->set_total($total_count); } // pre-template callback if (PerchUtil::count($rows) && $pre_template_callback && is_callable($pre_template_callback)) { $rows = $pre_template_callback($rows); } // each if (PerchUtil::count($rows) && isset($opts['each']) && is_callable($opts['each'])) { $content = array(); foreach ($rows as $item) { $tmp = $opts['each']($item); $content[] = $tmp; } $rows = $content; } $items = $this->return_instances($rows); } if (isset($opts['return-objects']) && $opts['return-objects']) { return $items; } $render_html = true; if (isset($opts['skip-template']) && $opts['skip-template'] == true) { $render_html = false; if (isset($opts['return-html']) && $opts['return-html'] == true) { $render_html = true; } } // template if (is_callable($opts['template'])) { $callback = $opts['template']; $template = $callback($items); } else { $template = $opts['template']; } if (is_object($this->api)) { $Template = $this->api->get('Template'); $Template->set($template, $this->namespace); } else { $Template = new PerchTemplate($template, $this->namespace); } if ($render_html) { if (isset($Paging) && is_object($Paging) && $Paging->enabled()) { $paging_array = $Paging->to_array($opts); // merge in paging vars if (PerchUtil::count($items)) { foreach ($items as &$Item) { foreach ($paging_array as $key => $val) { $Item->squirrel($key, $val); } } } } if (PerchUtil::count($items)) { if (isset($opts['split-items']) && $opts['split-items']) { $html = $Template->render_group($items, false); } else { $html = $Template->render_group($items, true); } } else { $Template->use_noresults(); $html = $Template->render(array()); } } if (isset($opts['skip-template']) && $opts['skip-template'] == true) { if ($single_mode) { return $Item->to_array(); } $processed_vars = array(); if (PerchUtil::count($items)) { foreach ($items as $Item) { $processed_vars[] = $Item->to_array(); } } if (PerchUtil::count($processed_vars)) { $category_field_ids = $Template->find_all_tag_ids('categories'); //PerchUtil::debug($category_field_ids, 'notice'); foreach ($processed_vars as &$item) { if (PerchUtil::count($item)) { foreach ($item as $key => &$field) { if (in_array($key, $category_field_ids)) { $field = $this->_process_category_field($field); } if (is_array($field) && isset($field['processed'])) { $field = $field['processed']; } if (is_array($field) && isset($field['_default'])) { $field = $field['_default']; } } } } } if (isset($opts['return-html']) && $opts['return-html'] == true) { $processed_vars['html'] = $html; } return $processed_vars; } if (is_array($html)) { // split-items if (PerchUtil::count($html)) { $Template = new PerchTemplate(); foreach ($html as &$html_item) { if (strpos($html_item, '<perch:') !== false) { $html_item = $Template->apply_runtime_post_processing($html_item); } } } } else { if (strpos($html, '<perch:') !== false) { $Template = new PerchTemplate(); $html = $Template->apply_runtime_post_processing($html); } } return $html; }
public function search_content($key, $opts, $admin = false) { PerchUtil::debug('Search term: ' . $key, 'success'); $this->mb_fallback(); $search_method = 'get_search_sql'; $format_method = 'format_result'; if ($admin) { $search_method = 'get_admin_search_sql'; $format_method = 'format_admin_result'; } $search_handlers = PerchSystem::get_registered_search_handlers(); $search_content = true; if (PerchUtil::count($opts['apps'])) { $search_content = in_array('PerchContent', $opts['apps']); if (PerchUtil::count($search_handlers)) { $new_handlers = array(); foreach ($search_handlers as $handler) { $short = str_replace('_SearchHandler', '', $handler); if (in_array($short, $opts['apps'])) { $new_handlers[] = $handler; } } $search_handlers = $new_handlers; } } $out = array(); if ($key != '') { if (!$this->api) { $this->api = new PerchAPI(1.0, 'content'); } $encoded_key = str_replace('"', '', PerchUtil::json_safe_encode($key)); $Paging = $this->api->get('Paging'); if (isset($opts['count'])) { $Paging->set_per_page($opts['count']); if (isset($opts['start']) && $opts['start'] != '') { $Paging->set_start_position($opts['start']); } } else { $Paging->disable(); } // Proper query using FULLTEXT $sql = $Paging->select_sql(); if (!$search_content) { $sql .= ' \'PerchContent_SearchHandler\' AS source, \'\' AS score, \'\' AS col1, \'\' AS col2, \'\' AS col3, \'\' AS col4, \'\' AS col5, \'\' AS col6, \'\' AS col7, \'\' AS col8 FROM ' . $this->table . ' WHERE 1=0 UNION '; } if (PerchUtil::count($search_handlers)) { $first = true; foreach ($search_handlers as $handler) { $handler_sql = false; if (method_exists($handler, $search_method)) { $handler_sql = call_user_func(array($handler, $search_method), $key, $opts); } if ($handler_sql) { if ($first) { $sql .= ' ' . $handler_sql . ' '; $first = false; } else { $sql .= ' UNION ' . $handler_sql . ' '; } } $handler_sql = false; } } $sql .= ' ORDER BY score DESC'; if ($Paging->enabled()) { $sql .= ' ' . $Paging->limit_sql(); } $rows = $this->db->get_rows($sql); if (PerchUtil::count($rows) == 0) { if ($search_content) { $sql = $Paging->select_sql(); } else { $sql = $Paging->select_sql() . ' \'PerchContent_SearchHandler\' AS source, \'\' AS score, \'\' AS col1, \'\' AS col2, \'\' AS col3, \'\' AS col4, \'\' AS col5, \'\' AS col6, \'\' AS col7, \'\' AS col8 FROM ' . $this->table . ' WHERE 1=0 UNION '; } if (PerchUtil::count($search_handlers)) { $first = true; foreach ($search_handlers as $handler) { $handler_sql = call_user_func(array($handler, 'get_backup_search_sql'), $key, $opts); if ($handler_sql) { if ($first) { $sql .= ' ' . $handler_sql . ' '; $first = false; } else { $sql .= ' UNION ' . $handler_sql . ' '; } } $handler_sql = false; } } $sql .= ' ORDER BY score ASC '; if ($Paging->enabled()) { $sql .= ' ' . $Paging->limit_sql(); } $rows = $this->db->get_rows($sql); } if ($Paging->enabled()) { $Paging->set_total($this->db->get_count($Paging->total_count_sql())); } if (PerchUtil::count($rows)) { foreach ($rows as $row) { $className = $row['source']; if (method_exists($className, $format_method)) { $r = call_user_func(array($className, $format_method), $key, $opts, $row); } else { $r = false; } if ($r) { $r['source'] = str_replace('_SearchHandler', '', $row['source']); // duplicate vals foreach ($r as $k => $val) { $r['result_' . $k] = $val; if ($opts['no-conflict']) { //unset($r[$k]); } } $r['search_key'] = $key; if (!$opts['no-conflict']) { $r['key'] = $key; } $out[] = $r; } } } } if (isset($opts['skip-template']) && $opts['skip-template']) { return $out; } $Template = new PerchTemplate('search/' . $opts['template'], 'search'); $Template->enable_encoding(); if (PerchUtil::count($out)) { foreach ($out as &$row) { // compat if (!$opts['no-conflict']) { $row['url'] = $row['result_url']; if (isset($row['result_result_url'])) { $row['result_url'] = $row['result_result_url']; } } // hide default doc if ($opts['hide-default-doc']) { $row['result_url'] = preg_replace('/' . preg_quote(PERCH_DEFAULT_DOC) . '$/', '', $row['result_url']); } if ($opts['hide-extensions'] && strpos($row['result_url'], '.')) { $parts = explode('.', $row['result_url']); $ext = array_pop($parts); $query = ''; if (strpos($ext, '?') !== false) { $qparts = explode('?', $ext); array_shift($qparts); if (PerchUtil::count($qparts)) { $query = '?' . implode('?', $qparts); } } $row['result_url'] = implode('.', $parts) . $query; } // trailing slash if ($opts['add-trailing-slash']) { $row['result_url'] = rtrim($row['result_url'], '/') . '/'; } } if (isset($Paging) && $Paging->enabled()) { $paging_array = $Paging->to_array($opts); // merge in paging vars foreach ($out as &$item) { foreach ($paging_array as $key => $val) { $item[$key] = $val; } } } return $Template->render_group($out, 1); } else { $Template->use_noresults(); return $Template->render(array('search_key' => $key, 'key' => $key)); } }
private function build_message_with_perch() { $Template = new PerchTemplate($this->template, 'email'); $html = $Template->render($this->vars); return $html; }
public function template_attribute($id, $opts) { $attr_vars = PerchSystem::get_attr_vars(); if (isset($attr_vars[$id])) { return $attr_vars[$id]; } if ($id == 'pageTitle' || $id == 'pageNavText') { return $this->details[$id]; } $Template = new PerchTemplate('pages/attributes/' . $opts['template'], 'pages'); $tag = $Template->find_tag($id, false, true); if ($tag) { $Template->load($tag); return $Template->render($this); } if (isset($this->details[$id])) { return $this->details[$id]; } return false; }
function perch_template($tpl, $vars = array(), $return = false) { $Template = new PerchTemplate($tpl); if (!is_array($vars)) { PerchUtil::debug('Non-array content value passed to perch_template.', 'error'); $vars = array(); } if (count($vars) == 0) { $Template->use_noresults(); } if (!PerchUtil::is_assoc($vars)) { $html = $Template->render_group($vars, true); } else { $html = $Template->render($vars); } $html = $Template->apply_runtime_post_processing($html); if ($return) { return $html; } echo $html; PerchUtil::flush_output(); }