function perch_events_custom($opts = false, $return = false) { if (isset($opts['skip-template']) && $opts['skip-template'] == true) { $return = true; $postpro = false; } else { $postpro = true; } $API = new PerchAPI(1.0, 'perch_events'); $Events = new PerchEvents_Events($API); $out = $Events->get_custom($opts); // Post processing - if there are still <perch:x /> tags if ($postpro && !is_array($out) && strpos($out, '<perch:') !== false) { $Template = new PerchTemplate(); $out = $Template->apply_runtime_post_processing($out); } if ($return) { return $out; } echo $out; }
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; }
private function build_message_with_perch() { if (isset($this->app_id)) { $API = new PerchAPI($this->version, $this->app_id); $Template = $API->get('Template'); $Template->set($this->template, $this->template_ns); } else { $Template = new PerchTemplate($this->template, 'email'); } $html = $Template->render_group(array($this->vars), true); $html = $Template->apply_runtime_post_processing($html); $this->subject($this->find_subject_from_html($html)); return $html; }
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(); }