Exemplo n.º 1
0
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;
 }
Exemplo n.º 3
0
 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;
 }
Exemplo n.º 4
0
 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));
     }
 }
Exemplo n.º 5
0


    <?php 
if (PerchUtil::count($items)) {
    echo '<table class="d itemlist">';
    echo '<thead>';
    echo '<tr>';
    foreach ($cols as $col) {
        echo '<th>' . PerchUtil::html($col['title']) . '</th>';
    }
    echo '<th class="last action"></th>';
    echo '</tr>';
    echo '</thead>';
    echo '<tbody>';
    $Template = new PerchTemplate();
    $i = 1;
    foreach ($items as $item) {
        echo '<tr>';
        $first = true;
        foreach ($cols as $col) {
            if ($first) {
                echo '<td class="primary">';
                echo '<a href="' . PerchUtil::html(PERCH_LOGINPATH) . '/core/apps/content/edit/?id=' . PerchUtil::html($Region->id()) . '&amp;itm=' . PerchUtil::html($item['itemID']) . '">';
            } else {
                echo '<td>';
            }
            if ($col['id'] == '_title') {
                if (isset($item['_title'])) {
                    $title = $item['_title'];
                } else {
 private function _get_template_content()
 {
     if ($this->templateContent === false) {
         $content = file_get_contents(PerchUtil::file_path(PERCH_PATH . $this->templatePath));
         // parse subtemplates
         $Template = new PerchTemplate();
         $this->templateContent = $Template->load($content, true);
     }
     return $this->templateContent;
 }
 private function _get_template_content()
 {
     if ($this->templateContent === false) {
         $file = PerchUtil::file_path(PERCH_PATH . $this->templatePath);
         if (file_exists($file)) {
             $content = file_get_contents($file);
             // parse subtemplates
             $Template = new PerchTemplate();
             $this->templateContent = $Template->load($content, true);
         } else {
             PerchUtil::debug('Template file not found: ' . $file, 'error');
         }
     }
     return $this->templateContent;
 }
Exemplo n.º 8
0
 protected function render_related($type, $opening_tag, $condition_contents, $exact_match, $template_contents, $content_vars, $index_in_group)
 {
     $Tag = new PerchXMLTag($opening_tag);
     $out = '';
     if ($Tag->suppress()) {
         return str_replace($exact_match, '', $template_contents);
     }
     if (is_array($content_vars) && isset($content_vars[$Tag->id()]) && PerchUtil::count($content_vars[$Tag->id()])) {
         if (!class_exists('PerchContent_Collections', false)) {
             include_once PERCH_CORE . '/runway/apps/content/PerchContent_Collections.class.php';
             include_once PERCH_CORE . '/runway/apps/content/PerchContent_Collection.class.php';
             include_once PERCH_CORE . '/runway/apps/content/PerchContent_CollectionItems.class.php';
             include_once PERCH_CORE . '/runway/apps/content/PerchContent_CollectionItem.class.php';
         }
         $Collections = $this->_get_cached_object('PerchContent_Collections');
         $value = $Collections->get_data_from_ids_runtime($Tag->collection(), $content_vars[$Tag->id()], $Tag->sort());
         $RelatedTemplate = new PerchTemplate(false, $this->namespace);
         $RelatedTemplate->load($condition_contents);
         if (PerchUtil::bool_val($Tag->scope_parent())) {
             $vars_for_cat = array();
             if (PerchUtil::count($content_vars)) {
                 foreach ($content_vars as $key => $val) {
                     if ($key != $Tag->id() && $key != 'itemJSON') {
                         $vars_for_cat['parent.' . $key] = $val;
                     }
                 }
             }
             $vars_for_cat = array_merge($vars_for_cat, $content_vars[$Tag->id()]);
             foreach ($value as &$item) {
                 $item = array_merge($item, $vars_for_cat);
             }
         }
         $out = $RelatedTemplate->render_group($value, true);
     } else {
         if (strpos($condition_contents, 'perch:noresults')) {
             $s = '/<perch:noresults[^>]*>(.*?)<\\/perch:noresults>/s';
             $count = preg_match_all($s, $condition_contents, $matches, PREG_SET_ORDER);
             if ($count > 0) {
                 foreach ($matches as $match) {
                     $out .= $match[1];
                 }
             }
         }
     }
     return str_replace($exact_match, $out, $template_contents);
 }
 public function get_template_tag_ids()
 {
     $Template = new PerchTemplate('content/' . $this->regionTemplate(), 'content');
     return $Template->find_all_tag_ids();
 }
 private function build_message_with_perch()
 {
     $Template = new PerchTemplate($this->template, 'email');
     $html = $Template->render($this->vars);
     return $html;
 }
Exemplo n.º 11
0
 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;
 }
Exemplo n.º 12
0
    
        <div class="field">
            <?php 
    echo $Form->label('addToTop', 'New items are');
    ?>
            <?php 
    $opts = array();
    $opts[] = array('label' => PerchLang::get('Added to the top'), 'value' => 1);
    $opts[] = array('label' => PerchLang::get('Added to the bottom'), 'value' => 0);
    echo $Form->select('addToTop', $opts, $Form->get($options, 'addToTop', 0));
    ?>
        </div>

        <?php 
    // Used by column_ids and sortField
    $Template = new PerchTemplate('content/' . $Region->regionTemplate(), 'content');
    $tags = $Template->find_all_tags('(content|categories)');
    ?>


        <div class="field last">
            <?php 
    echo $Form->label('column_ids', 'Item list column IDs');
    ?>
            <?php 
    echo $Form->text('column_ids', $Form->get($options, 'column_ids'), 'xl');
    echo $Form->hint(PerchLang::get('Enter field IDs to list when editing in list and detail mode. Comma separated.'));
    $suggestions = array();
    $suggestions[] = '_title';
    $seen_tags = array();
    if (PerchUtil::count($tags)) {
 public function get_navigation($opts, $current_page)
 {
     $from_path = $opts['from-path'];
     $levels = $opts['levels'];
     $hide_extensions = $opts['hide-extensions'];
     $hide_default_doc = $opts['hide-default-doc'];
     $flat = $opts['flat'];
     $templates = $opts['template'];
     $include_parent = $opts['include-parent'];
     $skip_template = $opts['skip-template'];
     $siblings = $opts['siblings'];
     $only_expand_selected = $opts['only-expand-selected'];
     $add_trailing_slash = $opts['add-trailing-slash'];
     $navgroup = $opts['navgroup'];
     $access_tags = $opts['access-tags'];
     $include_hidden = $opts['include-hidden'];
     $from_level = $opts['from-level'];
     $expand_attributes = $opts['use-attributes'];
     if ($access_tags == false) {
         $access_tags = array();
     }
     if (!is_array($templates)) {
         $templates = array($templates);
     }
     foreach ($templates as &$template) {
         $template = 'navigation/' . $template;
     }
     // navgroup
     if ($navgroup) {
         $groupID = $this->db->get_value('SELECT groupID FROM ' . PERCH_DB_PREFIX . 'navigation WHERE groupSlug=' . $this->db->pdb($navgroup) . ' LIMIT 1');
     } else {
         $groupID = false;
     }
     // from path
     if ($from_path && $from_path != '/') {
         $from_path = rtrim($from_path, '/');
         if ($navgroup) {
             $sql = 'SELECT np.pageID, np.pageParentID, np.pageDepth, np.pageTreePosition
                     FROM ' . PERCH_DB_PREFIX . 'navigation_pages np, ' . $this->table . ' p
                     WHERE p.pageID=np.pageID AND np.groupID=' . $this->db->pdb((int) $groupID) . ' AND (p.pagePath=' . $this->db->pdb($from_path) . ' OR p.pageSortPath=' . $this->db->pdb($from_path) . ') LIMIT 1';
         } else {
             $sql = 'SELECT pageID, pageParentID, pageDepth, pageTreePosition FROM ' . $this->table . ' WHERE pagePath=' . $this->db->pdb($from_path) . ' OR pageSortPath=' . $this->db->pdb($from_path) . ' LIMIT 1';
         }
         $root = $this->db->get_row($sql);
         if ($siblings) {
             // show siblings, so we actually want to select the parent page
             if ($navgroup) {
                 $sql = 'SELECT pageID, pageParentID, pageDepth, pageTreePosition FROM ' . PERCH_DB_PREFIX . 'navigation_pages WHERE groupID=' . $this->db->pdb((int) $groupID) . ' AND pageID=' . $this->db->pdb((int) $root['pageParentID']) . ' LIMIT 1';
             } else {
                 $sql = 'SELECT pageID, pageParentID, pageDepth, pageTreePosition FROM ' . $this->table . ' WHERE pageID=' . $this->db->pdb((int) $root['pageParentID']) . ' LIMIT 1';
             }
             $root = $this->db->get_row($sql);
         }
         if ($from_level !== false) {
             $parts = explode('-', $root['pageTreePosition']);
             if (PerchUtil::count($parts)) {
                 $new_root_tree_position = implode('-', array_slice($parts, 0, (int) $from_level + 1));
                 if ($new_root_tree_position) {
                     if ($navgroup) {
                         $sql = 'SELECT pageID, pageParentID, pageDepth, pageTreePosition FROM ' . PERCH_DB_PREFIX . 'navigation_pages WHERE groupID=' . $this->db->pdb((int) $groupID) . ' AND pageTreePosition=' . $this->db->pdb($new_root_tree_position) . ' LIMIT 1';
                     } else {
                         $sql = 'SELECT pageID, pageParentID, pageDepth, pageTreePosition FROM ' . $this->table . ' WHERE pageTreePosition=' . $this->db->pdb($new_root_tree_position) . ' LIMIT 1';
                     }
                     $root = $this->db->get_row($sql);
                 }
             }
         }
         $min_level = (int) $root['pageDepth'];
         $max_level = $min_level + $levels;
     } else {
         $root = false;
         $min_level = 0;
         $max_level = $min_level + $levels;
     }
     // cache page list
     if ($navgroup) {
         $sql = 'SELECT np.pageID, np.pageParentID, p.pagePath, p.pageTitle, p.pageNavText, p.pageNew, p.pageOrder, np.pageDepth, p.pageSortPath, np.pageTreePosition, p.pageAccessTags, p.pageAttributes
                 FROM ' . PERCH_DB_PREFIX . 'navigation_pages np, ' . $this->table . ' p WHERE p.pageID=np.pageID AND np.groupID=' . $this->db->pdb((int) $groupID) . ' AND p.pageNew=0 ';
         // if from path is set
         if ($root) {
             $sql .= ' AND np.pageTreePosition LIKE ' . $this->db->pdb($root['pageTreePosition'] . '%') . ' ';
         }
         // levels
         if ($levels) {
             $sql .= ' AND np.pageDepth >=' . $min_level . ' AND np.pageDepth<=' . $max_level . ' ';
         }
         $sql .= ' ORDER BY np.pageTreePosition ASC';
     } else {
         $sql = 'SELECT * FROM ' . $this->table . ' WHERE pageNew=0 ';
         if (!$include_hidden) {
             $sql .= ' AND pageHidden=0 ';
         }
         // if from path is set
         if ($root) {
             $sql .= ' AND pageTreePosition LIKE ' . $this->db->pdb($root['pageTreePosition'] . '%') . ' ';
         }
         // levels
         if ($levels) {
             $sql .= ' AND pageDepth >=' . $min_level . ' AND pageDepth<=' . $max_level . ' ';
         }
         $sql .= ' ORDER BY pageTreePosition ASC';
     }
     $this->nav_page_cache = $this->db->get_rows($sql);
     if (PerchUtil::count($this->nav_page_cache)) {
         $selected_ids = array();
         $ext_length = strlen(PERCH_DEFAULT_EXT);
         // select the tree
         $selected_ids = $this->find_parent_page_ids_by_path($current_page, $groupID);
         // loop-de-loop-de-pages
         $chosen_ones = array();
         foreach ($this->nav_page_cache as &$page) {
             // find current page
             if ($page['pagePath'] == $current_page) {
                 if (is_array($selected_ids)) {
                     array_unshift($selected_ids, $page['pageID']);
                 } else {
                     $selected_ids = array($page['pageID']);
                 }
             }
             // hide default doc
             if ($hide_default_doc) {
                 $page['pagePath'] = preg_replace('/' . preg_quote(PERCH_DEFAULT_DOC) . '$/', '', $page['pagePath']);
             }
             // hide extensions
             if ($hide_extensions) {
                 $page['pagePath'] = preg_replace('/' . preg_quote(PERCH_DEFAULT_EXT) . '$/', '', $page['pagePath']);
             }
             // trailing slash
             if ($add_trailing_slash) {
                 $page['pagePath'] = rtrim($page['pagePath'], '/') . '/';
             }
             if (trim($page['pageAccessTags']) == '') {
                 $chosen_ones[] = $page;
             } else {
                 $intersection = array_intersect($access_tags, explode(',', $page['pageAccessTags']));
                 if (PerchUtil::count($intersection)) {
                     $chosen_ones[] = $page;
                 }
             }
         }
         $this->nav_page_cache = $chosen_ones;
         $chosen_ones = null;
         if ($flat) {
             // Template them all flat.
             $rows = $this->nav_page_cache;
             foreach ($rows as &$row) {
                 if (is_array($selected_ids) && in_array($row['pageID'], $selected_ids)) {
                     if ($selected_ids[0] == $row['pageID']) {
                         $row['current_page'] = true;
                     } else {
                         $row['ancestor_page'] = true;
                     }
                 }
                 if ($expand_attributes && isset($row['pageAttributes']) && $row['pageAttributes'] != '') {
                     $dynamic_fields = PerchUtil::json_safe_decode($row['pageAttributes'], true);
                     if (PerchUtil::count($dynamic_fields)) {
                         foreach ($dynamic_fields as $key => $value) {
                             $row[$key] = $value;
                         }
                     }
                     $row = array_merge($dynamic_fields, $row);
                 }
             }
             if ($skip_template) {
                 return $rows;
             }
             $Template = new PerchTemplate($templates[0], 'pages');
             return $Template->render_group($rows, true);
         } else {
             // Template nested
             if ($root) {
                 if ($include_parent) {
                     $parentID = $root['pageParentID'];
                 } else {
                     $parentID = $root['pageID'];
                 }
             } else {
                 $parentID = 0;
             }
             if ($skip_template) {
                 $templates = false;
             }
             return $this->_template_nav($templates, $selected_ids, $parentID, $level = 0, $skip_template, $only_expand_selected, $expand_attributes);
         }
     }
     return '';
 }
Exemplo n.º 14
0
 public function to_array($opts = false)
 {
     $Perch = Perch::fetch();
     $request_uri = PerchUtil::html($Perch->get_page(1));
     #PerchUtil::debug('Pagination base url: '.$request_uri);
     if (is_array($opts)) {
         if (isset($opts['hide-extensions']) && $opts['hide-extensions'] == true) {
             if (strpos($request_uri, '.')) {
                 $query = '';
                 if ($qpos = strpos($request_uri, '?')) {
                     $query = substr($request_uri, $qpos);
                 }
                 $parts = explode('.', $request_uri);
                 array_pop($parts);
                 $request_uri = implode('.', $parts);
                 $request_uri .= $query;
             }
         }
     }
     $qs_char = '?';
     if (strpos($request_uri, $qs_char) !== false) {
         $qs_char = '&amp;';
     }
     if (!$this->use_qs) {
         $qs_char = '';
     }
     $out = array();
     $out['paging'] = $this->number_of_pages() > 1 ? true : false;
     $out['total'] = $this->total();
     $out['number_of_pages'] = $this->number_of_pages();
     $out['total_pages'] = $this->number_of_pages();
     $out['per_page'] = $this->per_page();
     $out['current_page'] = $this->current_page();
     $out['lower_bound'] = $this->lower_bound() + 1;
     $out['upper_bound'] = $this->upper_bound() + 1;
     if ($this->total != 0 && $out['upper_bound'] > $this->total) {
         $out['upper_bound'] = $this->total;
     }
     $out['prev_url'] = '';
     $out['next_url'] = '';
     $out['prev_page_number'] = '';
     $out['next_page_number'] = '';
     if (!$this->is_first_page()) {
         $prev_page_number = $this->current_page() - 1;
         if ($prev_page_number == 1) {
             // page 1, so don't include page=1
             $out['prev_url'] = preg_replace('#' . $this->page_pattern . '#', '', $request_uri);
         } else {
             $out['prev_url'] = preg_replace('#' . $this->page_pattern . '#', sprintf($this->page_replacement, $prev_page_number), $request_uri);
         }
         // remove any trailing '?'
         $out['prev_url'] = rtrim($out['prev_url'], '?');
         // remove any trailing '&amp;'
         if (substr($out['prev_url'], -5) == '&amp;') {
             $out['prev_url'] = substr($out['prev_url'], 0, strlen($out['prev_url']) - 5);
         }
         $out['not_first_page'] = true;
         $out['prev_page_number'] = $prev_page_number;
     }
     if (!$this->is_last_page()) {
         $next_page_number = $this->current_page() + 1;
         if (preg_match('#' . $this->page_pattern . '#', $request_uri)) {
             $out['next_url'] = preg_replace('#' . $this->page_pattern . '#', sprintf($this->page_replacement, $next_page_number), $request_uri);
             $out['next_page_number'] = $next_page_number;
         } else {
             $out['next_url'] = rtrim($request_uri) . $qs_char . sprintf($this->page_replacement, $next_page_number);
             $out['next_page_number'] = $next_page_number;
         }
         $out['not_last_page'] = true;
     }
     // Page links
     if (isset($opts['page-links']) && $opts['page-links']) {
         $this->base_url = $request_uri;
         $this->qs_char = $qs_char;
         if (isset($opts['page-link-style']) && $opts['page-link-style'] == 'all') {
             $page_links = $this->get_page_links();
         } else {
             $page_links = $this->get_page_links(true);
         }
         if (PerchUtil::count($page_links)) {
             $template = 'page-links.html';
             if (isset($opts['page-link-template'])) {
                 $template = $opts['page-link-template'];
             }
             $Template = new PerchTemplate('pagination/' . $template, 'pages');
             $out['page_links'] = $Template->render_group($page_links, true);
         }
     }
     return $out;
 }
Exemplo n.º 15
0
            if (isset($item_id) && $item_id) {
                $details = $Region->get_items_for_editing($item_id);
            } else {
                $details = $Region->get_items_for_editing();
            }
            $Perch->event('page.publish', $Page);
            $Alert->set('success', PerchLang::get('Your most recent change has been reverted.'));
        } else {
            $Alert->set('error', PerchLang::get('There was nothing to undo.'));
        }
    }
}
/* --------- Edit Form ----------- */
if ($Region->regionTemplate() != '') {
    $Resources = new PerchResources();
    $Template = new PerchTemplate('content/' . $Region->regionTemplate(), 'content');
    if ($Template->status == 404) {
        $Alert->set('error', PerchLang::get('The template for this region (%s) cannot be found.', '<code>' . $Region->regionTemplate() . '</code>'));
    }
    $tags = $Template->find_all_tags_and_repeaters('content');
    //PerchUtil::debug($tags);
    $template_help_html = $Template->find_help();
    $Form = new PerchForm('edit');
    $req = array();
    // initialise field types (add head javascript)
    $all_tags = $Template->find_all_tags('content');
    if (PerchUtil::count($all_tags)) {
        foreach ($all_tags as $tag) {
            $FieldType = PerchFieldTypes::get($tag->type(), $Form, $tag, $all_tags);
        }
    }
Exemplo n.º 16
0
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();
}