public function get_categories_from_ids_runtime($ids, $sort = false)
 {
     if (PerchUtil::count($this->from_ids_cache)) {
         $out = array_filter($this->from_ids_cache, function ($a) use($ids) {
             return in_array($a['catID'], $ids);
         });
         if ($sort !== false) {
             $parts = explode(' ', trim($sort));
             $sort_field = $parts[0];
             if (isset($parts[1]) && ($parts[1] == 'ASC' || $parts[1] == 'DESC')) {
                 $sort_dir = $parts[1];
             } else {
                 $sort_dir = 'ASC';
             }
             $out = PerchUtil::array_sort($out, $sort_field, $sort_dir == 'DESC' ? true : false);
         }
         return $this->return_flattened_instances($out);
     }
     if (!$sort) {
         $sort = 'catTreePosition ASC';
     }
     $sql = 'SELECT * FROM ' . $this->table . ' ORDER BY ' . $sort;
     $rows = $this->db->get_rows($sql);
     if (PerchUtil::count($rows)) {
         $this->from_ids_cache = $rows;
         return $this->get_categories_from_ids_runtime($ids, $sort);
     }
 }
 public function get_templates($path = false, $include_hidden = false, $initial_path = false)
 {
     $Perch = Perch::fetch();
     if ($path === false) {
         $path = PERCH_TEMPLATE_PATH . '/categories';
     }
     if ($initial_path === false) {
         $initial_path = $path;
     }
     $a = array();
     if (is_dir($path)) {
         if ($dh = opendir($path)) {
             while (($file = readdir($dh)) !== false) {
                 if (substr($file, 0, 1) != '.' && ($include_hidden || substr($file, 0, 1) != '_') && !preg_match($Perch->ignore_pattern, $file)) {
                     $extension = PerchUtil::file_extension($file);
                     if ($extension == 'html' || $extension == 'htm') {
                         $p = str_replace($initial_path, '', $path);
                         if (!$p) {
                             $a[PerchLang::get('Categories')][] = array('filename' => $file, 'path' => $file, 'label' => $this->template_display_name($file));
                         } else {
                             $a[] = array('filename' => $file, 'path' => ltrim($p, '/') . '/' . $file, 'label' => $this->template_display_name($file));
                         }
                     } else {
                         $a[$this->template_display_name($file)] = $this->get_templates($path . '/' . $file, $include_hidden, $initial_path);
                     }
                 }
             }
             closedir($dh);
         }
         if (PerchUtil::count($a)) {
             $a = PerchUtil::array_sort($a, 'label');
         }
     }
     return $a;
 }
예제 #3
0
 public function get_list($filter_mode = false, $sort = true)
 {
     $db = PerchDB::fetch();
     $sql = 'SELECT *	FROM ' . $this->table;
     // Extend this later...
     // switch ($filter_mode) {
     // 	default:
     // 		# code...
     // 		break;
     // }
     $results = $db->get_rows($sql);
     if ($sort && PerchUtil::count($results) > 0) {
         $results = PerchUtil::array_sort($results, 'Location');
     }
     return $this->return_instances($results);
 }
 /**
  * Get an array of templates in the content folder.
  *
  * @param string $path 
  * @return void
  * @author Drew McLellan
  */
 public function get_templates($path = false, $include_hidden = false, $initial_path = false)
 {
     $Perch = Perch::fetch();
     if ($path === false) {
         $path = PERCH_TEMPLATE_PATH . '/forms/emails';
     }
     if ($initial_path === false) {
         $initial_path = $path;
     }
     $a = array();
     $groups = array();
     $p = false;
     if (is_dir($path)) {
         if ($dh = opendir($path)) {
             while (($file = readdir($dh)) !== false) {
                 if (substr($file, 0, 1) != '.' && ($include_hidden || substr($file, 0, 1) != '_') && !preg_match($Perch->ignore_pattern, $file)) {
                     $extension = PerchUtil::file_extension($file);
                     if ($extension == 'html' || $extension == 'htm') {
                         $p = str_replace($initial_path, '', $path);
                         if (!$p) {
                             $a[PerchLang::get('Templates')][] = array('filename' => $file, 'value' => $file, 'path' => $file, 'label' => $this->template_display_name($file));
                         } else {
                             $a[] = array('filename' => $file, 'value' => ltrim($p, '/') . '/' . $file, 'path' => ltrim($p, '/') . '/' . $file, 'label' => $this->template_display_name($file));
                         }
                     } else {
                         // Use this one of infinite recursive nesting. Group stuff below normalised for HTML select optgroups that only do one level
                         //$a[$this->template_display_name($file)] = $this->get_templates($path.'/'.$file, $include_hidden, $initial_path);
                         if ($p) {
                             $group_name = $this->template_display_name(trim($p, '/\\') . '/' . $file);
                         } else {
                             $group_name = $this->template_display_name($file);
                         }
                         $groups[$group_name] = $this->get_templates($path . '/' . $file, $include_hidden, $initial_path);
                     }
                 }
             }
             closedir($dh);
         }
         if (PerchUtil::count($a)) {
             $a = PerchUtil::array_sort($a, 'label');
         }
     }
     return $a + $groups;
 }
}
// If a success or failure message has been set, output that here
echo $message;
// Sub head
echo $HTML->heading2('Details');
// Output the edit form
echo $Form->form_start();
$details = array();
if (is_object($Set)) {
    $details = $Set->to_array();
}
echo $Form->fields_from_template($Template, $details, array(), false);
$opts = array();
$templates = $Sets->get_templates();
if (PerchUtil::count($templates)) {
    $opts = array();
    foreach ($templates as $group_name => $group) {
        $tmp = array();
        $group = PerchUtil::array_sort($group, 'label');
        foreach ($group as $file) {
            $tmp[] = array('label' => $file['label'], 'value' => $file['path']);
        }
        $opts[$group_name] = $tmp;
    }
}
echo $Form->grouped_select_field('setTemplate', 'Set template', $opts, isset($details['setTemplate']) ? $details['setTemplate'] : 'set.html');
echo $Form->grouped_select_field('setCatTemplate', 'Category template', $opts, isset($details['setCatTemplate']) ? $details['setCatTemplate'] : 'category.html');
echo $Form->submit_field();
echo $Form->form_end();
// Footer
include PERCH_PATH . '/core/inc/main_end.php';
 public function get_custom($parentID, $opts)
 {
     $filter_type = 'php';
     $single_mode = false;
     $select = 'SELECT ';
     $where = array();
     $order = array();
     $limit = '';
     $sql = ' * FROM ' . $this->table;
     $where[] = 'parentID=' . $this->db->pdb($parentID);
     $where[] = 'commentStatus=' . $this->db->pdb('LIVE');
     if (isset($opts['_id'])) {
         $where[] = 'commentID=' . $this->db->pdb((int) $opts['_id']);
         $single_mode = true;
     }
     if (!$single_mode && isset($opts['filter']) && in_array($opts['filter'], $this->static_fields)) {
         $filter_type = 'sql';
         $key = $opts['filter'];
         $raw_value = $opts['value'];
         $value = $this->db->pdb($opts['value']);
         $match = isset($opts['match']) ? $opts['match'] : 'eq';
         switch ($match) {
             case 'eq':
             case 'is':
             case 'exact':
                 $where[] = $key . '=' . $value;
                 break;
             case 'neq':
             case 'ne':
             case 'not':
                 $where[] = $key . '!=' . $value;
                 break;
             case 'gt':
                 $where[] = $key . '>' . $value;
                 break;
             case 'gte':
                 $where[] = $key . '>=' . $value;
                 break;
             case 'lt':
                 $where[] = $key . '<' . $value;
                 break;
             case 'lte':
                 $where[] = $key . '<=' . $value;
                 break;
             case 'contains':
                 $v = str_replace('/', '\\/', $raw_value);
                 $where[] = $key . " REGEXP '/\\b" . $v . "\\b/i'";
                 break;
             case 'regex':
             case 'regexp':
                 $v = str_replace('/', '\\/', $raw_value);
                 $where[] = $key . " REGEXP '" . $v . "'";
                 break;
             case 'between':
             case 'betwixt':
                 $vals = explode(',', $raw_value);
                 if (PerchUtil::count($vals) == 2) {
                     $where[] = $key . '>' . trim($this->db->pdb($vals[0]));
                     $where[] = $key . '<' . trim($this->db->pdb($vals[1]));
                 }
                 break;
             case 'eqbetween':
             case 'eqbetwixt':
                 $vals = explode(',', $raw_value);
                 if (PerchUtil::count($vals) == 2) {
                     $where[] = $key . '>=' . trim($this->db->pdb($vals[0]));
                     $where[] = $key . '<=' . trim($this->db->pdb($vals[1]));
                 }
                 break;
             case 'in':
             case 'within':
                 $vals = explode(',', $raw_value);
                 if (PerchUtil::count($vals)) {
                     $where[] = $key . ' IN (' . $this->implode_for_sql_in($vals) . ') ';
                 }
                 break;
         }
         // limit
         if (isset($opts['count'])) {
             $count = (int) $opts['count'];
             if (isset($opts['start'])) {
                 $start = (int) $opts['start'] - 1 . ',';
             } else {
                 $start = '';
             }
             $limit = $start . $count;
         }
     }
     // sort
     if (isset($opts['sort'])) {
         $desc = false;
         if (isset($opts['sort-order']) && $opts['sort-order'] == 'DESC') {
             $desc = true;
         } else {
             $desc = false;
         }
         $order[] = $opts['sort'] . ' ' . ($desc ? 'DESC' : 'ASC');
     }
     if (isset($opts['sort-order']) && $opts['sort-order'] == 'RAND') {
         $order[] = 'RAND()';
     }
     // Paging
     $Paging = $this->api->get('Paging', $opts['pagination-var']);
     if ($opts['paginate'] == false || isset($opts['start']) && $opts['start'] != '') {
         $Paging->disable();
     } else {
         $Paging->set_per_page($opts['count']);
         if (isset($opts['start']) && $opts['start'] != '') {
             $Paging->set_start_position($opts['start']);
         }
     }
     $sql = $Paging->select_sql() . $sql;
     $sql .= ' WHERE 1=1 ';
     if (count($where)) {
         $sql .= ' AND ' . implode(' AND ', $where);
     }
     if (count($order)) {
         $sql .= ' ORDER BY ' . implode(', ', $order);
     }
     if ($filter_type == 'sql' && $Paging->enabled()) {
         $sql .= ' ' . $Paging->limit_sql();
     } else {
         if ($limit && $limit != '') {
             $sql .= ' LIMIT ' . $limit;
         }
     }
     $rows = $this->db->get_rows($sql);
     if ($Paging->enabled()) {
         $Paging->set_total($this->db->get_count($Paging->total_count_sql()));
     }
     $objects = $this->return_instances($rows);
     $comments = array();
     if (PerchUtil::count($objects)) {
         foreach ($objects as $Object) {
             $comments[] = $Object->to_array();
         }
     }
     // if not filtering by a column in SQL
     if ($filter_type == 'php') {
         // if not picking an _id, check for a filter
         if (isset($opts['filter']) && isset($opts['value'])) {
             if (PerchUtil::count($comments)) {
                 $out = array();
                 $key = $opts['filter'];
                 $val = $opts['value'];
                 $match = isset($opts['match']) ? $opts['match'] : 'eq';
                 foreach ($comments as $item) {
                     if (!isset($item[$key])) {
                         $item[$key] = false;
                     }
                     if (isset($item[$key])) {
                         switch ($match) {
                             case 'eq':
                             case 'is':
                             case 'exact':
                                 if ($item[$key] == $val) {
                                     $out[] = $item;
                                 }
                                 break;
                             case 'neq':
                             case 'ne':
                             case 'not':
                                 if ($item[$key] != $val) {
                                     $out[] = $item;
                                 }
                                 break;
                             case 'gt':
                                 if ($item[$key] > $val) {
                                     $out[] = $item;
                                 }
                                 break;
                             case 'gte':
                                 if ($item[$key] >= $val) {
                                     $out[] = $item;
                                 }
                                 break;
                             case 'lt':
                                 if ($item[$key] < $val) {
                                     $out[] = $item;
                                 }
                                 break;
                             case 'lte':
                                 if ($item[$key] <= $val) {
                                     $out[] = $item;
                                 }
                                 break;
                             case 'contains':
                                 $value = str_replace('/', '\\/', $val);
                                 if (preg_match('/\\b' . $value . '\\b/i', $item[$key])) {
                                     $out[] = $item;
                                 }
                                 break;
                             case 'regex':
                             case 'regexp':
                                 if (preg_match($val, $item[$key])) {
                                     $out[] = $item;
                                 }
                                 break;
                             case 'between':
                             case 'betwixt':
                                 $vals = explode(',', $val);
                                 if (PerchUtil::count($vals) == 2) {
                                     if ($item[$key] > trim($vals[0]) && $item[$key] < trim($vals[1])) {
                                         $out[] = $item;
                                     }
                                 }
                                 break;
                             case 'eqbetween':
                             case 'eqbetwixt':
                                 $vals = explode(',', $val);
                                 if (PerchUtil::count($vals) == 2) {
                                     if ($item[$key] >= trim($vals[0]) && $item[$key] <= trim($vals[1])) {
                                         $out[] = $item;
                                     }
                                 }
                                 break;
                             case 'in':
                             case 'within':
                                 $vals = explode(',', $val);
                                 if (PerchUtil::count($vals)) {
                                     foreach ($vals as $value) {
                                         if ($item[$key] == trim($value)) {
                                             $out[] = $item;
                                             break;
                                         }
                                     }
                                 }
                                 break;
                         }
                     }
                 }
                 $comments = $out;
             }
         }
         // sort
         if (isset($opts['sort'])) {
             if (isset($opts['sort-order']) && $opts['sort-order'] == 'DESC') {
                 $desc = true;
             } else {
                 $desc = false;
             }
             $comments = PerchUtil::array_sort($comments, $opts['sort'], $desc);
         }
         if (isset($opts['sort-order']) && $opts['sort-order'] == 'RAND') {
             shuffle($comments);
         }
         // Pagination
         if (isset($opts['paginate'])) {
             $Paging->set_per_page(isset($opts['count']) ? (int) $opts['count'] : 10);
             $opts['count'] = $Paging->per_page();
             $opts['start'] = $Paging->lower_bound() + 1;
             $Paging->set_total(PerchUtil::count($comments));
         } else {
             $Paging = false;
         }
         // limit
         if (isset($opts['count']) || isset($opts['start'])) {
             // count
             if (isset($opts['count']) && $opts['count']) {
                 $count = (int) $opts['count'];
             } else {
                 $count = PerchUtil::count($comments);
             }
             // start
             if (isset($opts['start'])) {
                 if ($opts['start'] === 'RAND') {
                     $start = rand(0, PerchUtil::count($comments) - 1);
                 } else {
                     $start = (int) $opts['start'] - 1;
                 }
             } else {
                 $start = 0;
             }
             // loop through
             $out = array();
             for ($i = $start; $i < $start + $count; $i++) {
                 if (isset($comments[$i])) {
                     $out[] = $comments[$i];
                 } else {
                     break;
                 }
             }
         }
         $comments = $out;
     }
     if (isset($opts['skip-template']) && $opts['skip-template'] == true) {
         return $comments;
     }
     // template
     if (isset($opts['template'])) {
         $template = 'comments/' . $opts['template'];
     } else {
         $template = 'comments/comment.html';
     }
     // Paging to template
     if (is_object($Paging)) {
         $paging_array = $Paging->to_array($opts);
         // merge in paging vars
         foreach ($comments as &$item) {
             foreach ($paging_array as $key => $val) {
                 $item[$key] = $val;
             }
         }
     }
     $Template = $this->api->get("Template");
     $Template->set($template, 'comments');
     $html = $Template->render_group($comments, true);
     return $html;
 }
 public function get_custom($opts)
 {
     $API = new PerchAPI(1.0, 'perch_blog');
     $blogSQL = '';
     if ($opts['blog']) {
         $BlogPosts = new PerchBlog_Posts($API);
         $Blogs = new PerchBlog_Blogs($API);
         $Blog = $Blogs->get_one_by('blogSlug', $opts['blog']);
         if ($Blog) {
             $blogID = (int) $Blog->id();
             $blogSQL = ' AND blogID=' . $this->db->pdb($blogID) . ' ';
         }
     }
     if ($opts['include-empty']) {
         $sql = 'SELECT *, sectionID AS _id, sectionPostCount as qty FROM ' . $this->table . ' WHERE 1=1 ' . $blogSQL . ' ORDER BY sectionTitle ASC';
     } else {
         $sql = 'SELECT *, sectionID AS _id, sectionPostCount as qty FROM ' . $this->table . ' WHERE sectionPostCount>0 ' . $blogSQL . ' ORDER BY sectionTitle ASC';
     }
     $rows = $this->db->get_rows($sql);
     $sections = $this->return_instances($rows);
     $content = array();
     if (PerchUtil::count($sections)) {
         foreach ($sections as $Section) {
             $content[] = $Section->to_array();
         }
     }
     if (isset($opts['filter']) && (isset($opts['value']) || is_array($opts['filter']))) {
         if (PerchUtil::count($content)) {
             $out = array();
             // 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'));
                 $filter_mode = 'AND';
             } else {
                 $filters = $opts['filter'];
                 $filter_mode = 'AND';
                 if (isset($opts['match']) && strtolower($opts['match']) == 'or') {
                     $filter_mode = 'OR';
                 }
             }
             $filter_content = $content;
             foreach ($filters as $filter) {
                 $key = $filter['filter'];
                 $val = $filter['value'];
                 $match = isset($filter['match']) ? $filter['match'] : 'eq';
                 foreach ($filter_content as $item) {
                     // If 'AND' mode, remove the item, as we only want it if it's added by this filter too.
                     // ninja code.
                     if ($filter_mode == 'AND' && isset($out[$item['_id']])) {
                         unset($out[$item['_id']]);
                     }
                     if (!isset($item[$key])) {
                         $item[$key] = false;
                     }
                     if (isset($item[$key])) {
                         $this_item = $this->_resolve_to_value($item[$key]);
                         switch ($match) {
                             case 'eq':
                             case 'is':
                             case 'exact':
                                 if ($this_item == $val) {
                                     $out[$item['_id']] = $item;
                                 }
                                 break;
                             case 'neq':
                             case 'ne':
                             case 'not':
                                 if ($this_item != $val) {
                                     $out[$item['_id']] = $item;
                                 }
                                 break;
                             case 'gt':
                                 if ($this_item > $val) {
                                     $out[$item['_id']] = $item;
                                 }
                                 break;
                             case 'gte':
                                 if ($this_item >= $val) {
                                     $out[$item['_id']] = $item;
                                 }
                                 break;
                             case 'lt':
                                 if ($this_item < $val) {
                                     $out[$item['_id']] = $item;
                                 }
                                 break;
                             case 'lte':
                                 if ($this_item <= $val) {
                                     $out[$item['_id']] = $item;
                                 }
                                 break;
                             case 'contains':
                                 $value = str_replace('/', '\\/', $val);
                                 if (preg_match('/\\b' . $value . '\\b/i', $this_item)) {
                                     $out[$item['_id']] = $item;
                                 }
                                 break;
                             case 'regex':
                             case 'regexp':
                                 if (preg_match($val, $this_item)) {
                                     $out[$item['_id']] = $item;
                                 }
                                 break;
                             case 'between':
                             case 'betwixt':
                                 $vals = explode(',', $val);
                                 if (PerchUtil::count($vals) == 2) {
                                     if ($this_item > trim($vals[0]) && $this_item < trim($vals[1])) {
                                         $out[$item['_id']] = $item;
                                     }
                                 }
                                 break;
                             case 'eqbetween':
                             case 'eqbetwixt':
                                 $vals = explode(',', $val);
                                 if (PerchUtil::count($vals) == 2) {
                                     if ($this_item >= trim($vals[0]) && $this_item <= trim($vals[1])) {
                                         $out[$item['_id']] = $item;
                                     }
                                 }
                                 break;
                             case 'in':
                             case 'within':
                                 $vals = explode(',', $val);
                                 if (PerchUtil::count($vals)) {
                                     foreach ($vals as $value) {
                                         if ($this_item == trim($value)) {
                                             $out[$item['_id']] = $item;
                                             break;
                                         }
                                     }
                                 }
                                 break;
                         }
                     }
                 }
                 // if 'AND' mode, run the next filter against the already filtered list
                 if ($filter_mode == 'AND') {
                     $filter_content = $out;
                 } else {
                     $filter_content = $content;
                 }
             }
             $content = $out;
         }
     }
     // reindex array
     $new_content = array();
     foreach ($content as $c) {
         $new_content[] = $c;
     }
     $content = $new_content;
     // sort
     if (isset($opts['sort'])) {
         if (isset($opts['sort-order']) && $opts['sort-order'] == 'DESC') {
             $desc = true;
         } else {
             $desc = false;
         }
         $content = PerchUtil::array_sort($content, $opts['sort'], $desc);
     }
     if (isset($opts['sort-order']) && $opts['sort-order'] == 'RAND') {
         shuffle($content);
     }
     // Pagination
     if (isset($opts['paginate'])) {
         $Paging = $API->get('Paging');
         if (isset($opts['pagination-var'])) {
             $Paging->set_qs_param($opts['pagination-var']);
         }
         $Paging->set_per_page(isset($opts['count']) ? (int) $opts['count'] : 10);
         $opts['count'] = $Paging->per_page();
         $opts['start'] = $Paging->lower_bound() + 1;
         $Paging->set_total(PerchUtil::count($content));
     } else {
         $Paging = false;
     }
     // limit
     if (isset($opts['count']) || isset($opts['start'])) {
         // count
         if (isset($opts['count'])) {
             $count = (int) $opts['count'];
         } else {
             $count = PerchUtil::count($content);
         }
         // start
         if (isset($opts['start'])) {
             if ($opts['start'] === 'RAND') {
                 $start = rand(0, PerchUtil::count($content) - 1);
             } else {
                 $start = (int) $opts['start'] - 1;
             }
         } else {
             $start = 0;
         }
         // loop through
         $out = array();
         for ($i = $start; $i < $start + $count; $i++) {
             if (isset($content[$i])) {
                 $out[] = $content[$i];
             } else {
                 break;
             }
         }
         $content = $out;
     }
     if (isset($opts['skip-template']) && $opts['skip-template'] == true) {
         if (isset($opts['raw']) && $opts['raw'] == true) {
             if (PerchUtil::count($content)) {
                 foreach ($content as &$item) {
                     if (PerchUtil::count($item)) {
                         foreach ($item as &$field) {
                             if (is_array($field) && isset($field['raw'])) {
                                 $field = $field['raw'];
                             }
                         }
                     }
                 }
             }
             return $content;
         }
     }
     // template
     $Template = $API->get('Template');
     $Template->set('blog/' . $opts['template'], 'blog');
     // post process
     $tags = $Template->find_all_tags_and_repeaters('blog');
     $processed_vars = array();
     $used_items = array();
     foreach ($content as $item) {
         $tmp = $item;
         if (PerchUtil::count($tags)) {
             foreach ($tags as $Tag) {
                 if (isset($item[$Tag->id()])) {
                     $used_items[] = $item;
                 }
             }
         }
         if ($tmp) {
             $processed_vars[] = $tmp;
         }
     }
     // Paging to template
     if (is_object($Paging) && $Paging->enabled()) {
         $paging_array = $Paging->to_array($opts);
         // merge in paging vars
         foreach ($processed_vars as &$item) {
             foreach ($paging_array as $key => $val) {
                 $item[$key] = $val;
             }
         }
     }
     if (PerchUtil::count($processed_vars)) {
         $html = $Template->render_group($processed_vars, true);
     } else {
         $Template->use_noresults();
         $html = $Template->render(array());
     }
     if (isset($opts['skip-template']) && $opts['skip-template'] == true) {
         $out = array();
         if (PerchUtil::count($processed_vars)) {
             foreach ($processed_vars as &$item) {
                 if (PerchUtil::count($item)) {
                     foreach ($item as &$field) {
                         if (is_array($field) && isset($field['processed'])) {
                             $field = $field['processed'];
                         }
                         if (is_array($field) && isset($field['_default'])) {
                             $field = $field['_default'];
                         }
                     }
                 }
             }
         }
         for ($i = 0; $i < PerchUtil::count($content); $i++) {
             $out[] = array_merge($content[$i], $processed_vars[$i]);
         }
         if (isset($opts['return-html']) && $opts['return-html'] == true) {
             $out['html'] = $html;
         }
         return $out;
     }
     return $html;
 }
 public function get_custom($albumID, $opts = array(), $Versions = false)
 {
     if ($albumID === false) {
         $sql = 'SELECT i.* FROM ' . $this->table . ' i, ' . PERCH_DB_PREFIX . 'gallery_albums a WHERE i.albumID=a.albumID ORDER BY a.albumOrder ASC, i.imageOrder ASC';
     } else {
         $sql = 'SELECT * FROM ' . $this->table . ' WHERE albumID = ' . $this->db->pdb($albumID) . ' ORDER BY ' . $this->default_sort_column . ' ASC';
     }
     $rows = $this->db->get_rows($sql);
     $objects = $this->return_instances($rows);
     $content = array();
     if (PerchUtil::count($objects)) {
         foreach ($objects as $Object) {
             $content[] = $Object->to_array(false, $Versions);
         }
     }
     // find specific _id
     if (isset($opts['_id'])) {
         if (PerchUtil::count($content)) {
             $out = array();
             foreach ($content as $item) {
                 if (isset($item['_id']) && $item['_id'] == $opts['_id']) {
                     $out[] = $item;
                     break;
                 }
             }
             $content = $out;
         }
     } else {
         // if not picking an _id, check for a filter
         if (isset($opts['filter']) && isset($opts['value'])) {
             if (PerchUtil::count($content)) {
                 $out = array();
                 $key = $opts['filter'];
                 $val = $opts['value'];
                 $match = isset($opts['match']) ? $opts['match'] : 'eq';
                 foreach ($content as $item) {
                     if (isset($item[$key])) {
                         switch ($match) {
                             case 'eq':
                             case 'is':
                             case 'exact':
                                 if ($item[$key] == $val) {
                                     $out[] = $item;
                                 }
                                 break;
                             case 'neq':
                             case 'ne':
                             case 'not':
                                 if ($item[$key] != $val) {
                                     $out[] = $item;
                                 }
                                 break;
                             case 'gt':
                                 if ($item[$key] > $val) {
                                     $out[] = $item;
                                 }
                                 break;
                             case 'gte':
                                 if ($item[$key] >= $val) {
                                     $out[] = $item;
                                 }
                                 break;
                             case 'lt':
                                 if ($item[$key] < $val) {
                                     $out[] = $item;
                                 }
                                 break;
                             case 'lte':
                                 if ($item[$key] <= $val) {
                                     $out[] = $item;
                                 }
                                 break;
                             case 'contains':
                                 $value = str_replace('/', '\\/', $val);
                                 if (preg_match('/\\b' . $value . '\\b/i', $item[$key])) {
                                     $out[] = $item;
                                 }
                                 break;
                             case 'regex':
                             case 'regexp':
                                 if (preg_match($val, $item[$key])) {
                                     $out[] = $item;
                                 }
                                 break;
                             case 'between':
                             case 'betwixt':
                                 $vals = explode(',', $val);
                                 if (PerchUtil::count($vals) == 2) {
                                     if ($item[$key] > trim($vals[0]) && $item[$key] < trim($vals[1])) {
                                         $out[] = $item;
                                     }
                                 }
                                 break;
                             case 'eqbetween':
                             case 'eqbetwixt':
                                 $vals = explode(',', $val);
                                 if (PerchUtil::count($vals) == 2) {
                                     if ($item[$key] >= trim($vals[0]) && $item[$key] <= trim($vals[1])) {
                                         $out[] = $item;
                                     }
                                 }
                                 break;
                             case 'in':
                             case 'within':
                                 $vals = explode(',', $val);
                                 if (PerchUtil::count($vals)) {
                                     foreach ($vals as $value) {
                                         if ($item[$key] == trim($value)) {
                                             $out[] = $item;
                                             break;
                                         }
                                     }
                                 }
                                 break;
                         }
                     }
                 }
                 $content = $out;
             }
         }
     }
     // sort
     if (isset($opts['sort'])) {
         if (isset($opts['sort-order']) && $opts['sort-order'] == 'DESC') {
             $desc = true;
         } else {
             $desc = false;
         }
         $content = PerchUtil::array_sort($content, $opts['sort'], $desc);
     }
     if (isset($opts['sort-order']) && $opts['sort-order'] == 'RAND') {
         shuffle($content);
     }
     // Pagination
     if (isset($opts['paginate'])) {
         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;
         $Paging->set_total(PerchUtil::count($content));
     } else {
         $Paging = false;
     }
     // limit
     if (isset($opts['count']) || isset($opts['start'])) {
         // count
         if (isset($opts['count'])) {
             $count = (int) $opts['count'];
         } else {
             $count = PerchUtil::count($content);
         }
         // start
         if (isset($opts['start'])) {
             if ($opts['start'] === 'RAND') {
                 $start = rand(0, PerchUtil::count($content) - 1);
             } else {
                 $start = (int) $opts['start'] - 1;
             }
         } else {
             $start = 0;
         }
         // loop through
         $out = array();
         for ($i = $start; $i < $start + $count; $i++) {
             if (isset($content[$i])) {
                 $out[] = $content[$i];
             } else {
                 break;
             }
         }
         $content = $out;
     }
     // Paging to template
     if (is_object($Paging) && $Paging->enabled()) {
         $paging_array = $Paging->to_array($opts);
         // merge in paging vars
         foreach ($content as &$item) {
             foreach ($paging_array as $key => $val) {
                 $item[$key] = $val;
             }
         }
     }
     return $content;
 }
 public function receive_from_template_fields($Template, $previous_values, $Factory = false, $Item = false, $clear_post = true, $strip_static = true)
 {
     $tags = $Template->find_all_tags_and_repeaters();
     if (is_object($Item)) {
         $Item->squirrel('itemID', '');
         $Item->squirrel('itemRowID', '');
     } else {
         $Item = $Factory->return_instance(array('itemID' => '', 'itemRowID' => ''));
         $Item->set_null_id();
     }
     $Item->squirrel('itemJSON', PerchUtil::json_safe_encode($previous_values));
     $subprefix = '';
     $postitems = $this->find_items('perch_');
     $form_vars = array();
     $options = array();
     $search_text = ' ';
     $API = new PerchAPI(1.0, $this->app_id);
     $Resources = $API->get('Resources');
     list($form_vars, $search_text) = PerchContent_Util::read_items_from_post($Item, $tags, $subprefix, $form_vars, $postitems, $this, $search_text, $options, $Resources, false, $Template);
     if (isset($form_vars['_blocks'])) {
         $form_vars['_blocks'] = PerchUtil::array_sort($form_vars['_blocks'], '_block_index');
     }
     $out = array();
     if ($strip_static) {
         if (PerchUtil::count($form_vars)) {
             foreach ($form_vars as $key => $val) {
                 if (!in_array($key, $Factory->static_fields)) {
                     $out[$key] = $val;
                 }
             }
         }
     } else {
         $out = $form_vars;
     }
     // Clear values from Post (for reordering of blocks etc)
     if ($clear_post) {
         $_POST = array();
     }
     return $out;
 }
예제 #10
0
 public function find_all_tags_and_repeaters($type = 'content', $contents = false)
 {
     $template = $this->template;
     $path = $this->file;
     if ($contents === false) {
         $contents = $this->load();
     }
     $untouched_content = $contents;
     $out = array();
     // Excluded tags are discarded
     $tag_pairs_to_exclude = array();
     $tag_pairs_with_empty_openers = array('blocks');
     //	List of tags to process. Blocks needs to come before others, as blocks can contain e.g. repeaters.
     $tag_pairs_to_process = array('blocks', 'repeater');
     if (PERCH_RUNWAY) {
         $tag_pairs_to_process[] = 'related';
     }
     // Add excluded tags so they can be discarded.
     $tag_pairs_to_process = array_merge($tag_pairs_to_process, $tag_pairs_to_exclude);
     foreach ($tag_pairs_to_process as $tag_type) {
         // parse out tag pairs
         $empty_opener = in_array($tag_type, $tag_pairs_with_empty_openers);
         $close_tag = '</perch:' . $tag_type . '>';
         $close_tag_len = mb_strlen($close_tag);
         $open_tag = '<perch:' . $tag_type . ($empty_opener ? '' : ' ');
         $order = 1;
         // escape hatch
         $i = 0;
         $max_loops = 100;
         // loop through while we have closing tags
         while ($close_pos = mb_strpos($contents, $close_tag)) {
             // we always have to go from the start, as the string length changes,
             // but stop at the closing tag
             $chunk = mb_substr($contents, 0, $close_pos);
             // search from the back of the chunk for the opening tag
             $open_pos = mb_strrpos($chunk, $open_tag);
             // get the pair html chunk
             $len = $close_pos + $close_tag_len - $open_pos;
             $pair_html = mb_substr($contents, $open_pos, $len);
             // find the opening tag - it's right at the start
             $opening_tag_end_pos = mb_strpos($pair_html, '>') + 1;
             $opening_tag = mb_substr($pair_html, 0, $opening_tag_end_pos);
             // condition contents
             $condition_contents = mb_substr($pair_html, $opening_tag_end_pos, 0 - $close_tag_len);
             // Do the business
             $OpeningTag = new PerchXMLTag($opening_tag);
             $tmp = array();
             if ($tag_type == 'repeater') {
                 $Repeater = new PerchRepeater($OpeningTag->attributes);
                 $Repeater->set('id', $OpeningTag->id());
                 //$Repeater->tags = $this->find_all_tags($type, $condition_contents);
                 $Repeater->tags = $this->find_all_tags_and_repeaters($type, $condition_contents);
                 $tmp['tag'] = $Repeater;
             } elseif ($tag_type == 'blocks') {
                 $OpeningTag->set('id', '_blocks');
                 $OpeningTag->set('type', 'PerchBlocks');
                 $tmp['tag'] = $OpeningTag;
             } else {
                 $tmp['tag'] = $OpeningTag;
             }
             // Set the order
             if ($OpeningTag->order()) {
                 $tmp['order'] = (int) $OpeningTag->order();
             } else {
                 $tmp['order'] = strpos($untouched_content, $opening_tag);
             }
             // If the tag isn't one to strip/exclude, add it to the list.
             if (!in_array($tag_type, $tag_pairs_to_exclude)) {
                 $out[] = $tmp;
             }
             // Remove the pair so we can parse the next one
             $contents = str_replace($pair_html, '', $contents);
             // escape hatch counter
             $i++;
             if ($i > $max_loops) {
                 return $contents;
             }
         }
     }
     $s = '/<perch:(' . $type . '|categories)[^>]*>/';
     $count = preg_match_all($s, $contents, $matches);
     if ($count > 0) {
         $i = 100;
         if (is_array($matches[0])) {
             foreach ($matches[0] as $match) {
                 $tmp = array();
                 $tmp['tag'] = new PerchXMLTag($match);
                 if (!in_array('categories', $this->disabled_features)) {
                     if ($tmp['tag']->tag_name() == 'perch:categories') {
                         $tmp['tag']->set('type', 'category');
                     }
                 }
                 if ($tmp['tag']->tag_name() == 'perch:related') {
                     $tmp['tag']->set('type', 'related');
                 }
                 if ($tmp['tag']->type() != 'repeater') {
                     if ($tmp['tag']->order()) {
                         $tmp['order'] = (int) $tmp['tag']->order();
                     } else {
                         $tmp['order'] = strpos($untouched_content, $match);
                         #PerchUtil::debug('Setting order to: '.$tmp['order']);
                         $i++;
                     }
                     $out[] = $tmp;
                 }
             }
         }
     }
     if (PerchUtil::count($out)) {
         // sort tags using 'order' attribute
         $out = PerchUtil::array_sort($out, 'order');
         $final = array();
         foreach ($out as $tag) {
             $final[] = $tag['tag'];
         }
         return $final;
     }
     return false;
 }
 /**
  * Reorder the items within a region by the given field
  *
  * @param string $regionID
  * @param string $rev
  * @param string $field
  * @param string $desc
  * @return void
  * @author Drew McLellan
  */
 public function sort_for_region($regionID, $rev, $field, $desc = false)
 {
     $items = $this->get_flat_for_region($regionID, $rev);
     if (PerchUtil::count($items)) {
         $sorted = PerchUtil::array_sort($items, $field, $desc);
         $i = 0;
         foreach ($sorted as $item) {
             $sql = 'UPDATE ' . $this->table . ' SET itemOrder=' . (1000 + $i) . ' WHERE itemID=' . $this->db->pdb((int) $item['itemID']) . ' AND itemRev=' . $this->db->pdb((int) $rev) . ' LIMIT 1';
             $this->db->execute($sql);
             $i++;
         }
         return true;
     }
     return false;
 }
예제 #12
0
 // Keep note of edited items
 $edited_items = array();
 if (is_array($tags)) {
     if (PerchUtil::count($items)) {
         foreach ($items as $Item) {
             $Item->clear_resources();
             $id = $Item->itemID();
             $form_vars = array();
             $file_paths = array();
             $search_text = ' ';
             $form_vars['_id'] = $id;
             $postitems = $Form->find_items('perch_' . $id . '_');
             $subprefix = '';
             list($form_vars, $search_text) = PerchContent_Util::read_items_from_post($Item, $tags, $subprefix, $form_vars, $postitems, $Form, $search_text, $options, $Resources, false, $Template);
             if (isset($form_vars['_blocks'])) {
                 $form_vars['_blocks'] = PerchUtil::array_sort($form_vars['_blocks'], '_block_index');
             }
             $data = array();
             $data['itemJSON'] = PerchUtil::json_safe_encode($form_vars);
             $data['itemSearch'] = $search_text;
             //PerchUtil::debug($form_vars, 'success');
             $Item->update($data);
             $edited_items[] = $id;
         }
     }
 }
 // Sort based on region options
 $Region->sort_items();
 // Publish (or not if draft)
 if (isset($_POST['save_as_draft'])) {
     $Alert->set('success', PerchLang::get('Draft successfully updated'));
 private function get_template_files($path = false, $include_hidden = false, $initial_path = false)
 {
     $Perch = Perch::fetch();
     if ($path === false) {
         $path = PERCH_TEMPLATE_PATH . '/pages';
     }
     if ($initial_path === false) {
         $initial_path = $path;
     }
     $a = array();
     if (is_dir($path)) {
         if ($dh = opendir($path)) {
             while (($file = readdir($dh)) !== false) {
                 if (substr($file, 0, 1) != '.' && $file != 'attributes' && ($include_hidden || substr($file, 0, 1) != '_') && !preg_match($Perch->ignore_pattern, $file)) {
                     $extension = PerchUtil::file_extension($file);
                     if ($extension == 'php' || $extension == str_replace('.', '', PERCH_DEFAULT_EXT)) {
                         $p = str_replace($initial_path, '', $path);
                         if (!$p) {
                             $a[] = array('filename' => $file, 'path' => $file, 'label' => $this->template_display_name($file), 'group' => 'general', 'sort' => '0_' . $file);
                         } else {
                             $out_path = ltrim($p, '/') . '/' . $file;
                             $a[] = array('filename' => $file, 'path' => $out_path, 'label' => $this->template_display_name($file), 'group' => trim($p, '/'), 'sort' => 'x_' . $out_path);
                         }
                     } else {
                         $a = array_merge($a, $this->get_template_files($path . '/' . $file, $include_hidden, $initial_path));
                     }
                 }
             }
             closedir($dh);
         }
         if (PerchUtil::count($a)) {
             $a = PerchUtil::array_sort($a, 'sort');
         }
     }
     return $a;
 }