public function index() { /* - Figure the state of things by URL URLS: /marks /marks/VALID_LOOKUP_KEY /marks/DATE(/DATE)? /marks/archive /marks/search*?q=QUERY /marks/tag* /marks/label* */ // Get URI segments $segments = $this->uri->segment_array(); $lookup = isset($segments[2]) && !empty($segments[2]) ? strtolower(trim(urldecode($segments[2]))) : 'all'; $finish = isset($segments[3]) && !empty($segments[3]) ? strtolower(trim(urldecode($segments[3]))) : null; $lookup = is_numeric($lookup) && empty($finish) ? 'all' : $lookup; // Get limit $this->limit = isset($this->clean->limit) && is_numeric($this->clean->limit) && $this->clean->limit < $this->limit ? $this->clean->limit : $this->limit; // Set sort $sort = isset($this->clean->sort) && !empty($this->clean->sort) ? strtolower($this->clean->sort) : 'newest'; $sort = $sort != 'newest' && $sort != 'oldest' ? 'newest' : $sort; $sort = $sort == 'oldest' ? 'ASC' : 'DESC'; $this->user_marks->sort = $lookup == 'archive' ? 'archived_on ' . $sort : 'created_on ' . $sort; // Set allowable textual starts $valid_lookups = array('all' => array('start' => null, 'finish' => null), 'archive' => array('start' => null, 'finish' => null), 'search' => array('start' => null, 'finish' => null), 'today' => array('start' => strtotime('today'), 'finish' => strtotime('tomorrow')), 'yesterday' => array('start' => strtotime('yesterday'), 'finish' => strtotime('today')), 'last-week' => array('start' => strtotime('6 days ago'), 'finish' => strtotime('tomorrow')), 'last-month' => array('start' => strtotime('29 days ago'), 'finish' => strtotime('tomorrow')), 'last-three-months' => array('start' => strtotime('89 days ago'), 'finish' => strtotime('tomorrow')), 'last-six-months' => array('start' => strtotime('179 days ago'), 'finish' => strtotime('tomorrow')), 'last-year' => array('start' => strtotime('364 days ago'), 'finish' => strtotime('tomorrow')), 'ages-ago' => array('start' => strtotime('20 years ago'), 'finish' => strtotime('364 days ago'))); // If $lookup is one of the following, search by time is disabled $no_time = array('all', 'archive', 'search'); $options = array(); // Figure when $where_time = null; if (array_key_exists($lookup, $valid_lookups)) { $where_time .= !in_array($lookup, $no_time) ? " AND users_to_marks.created_on >= '" . date('Y-m-d', $valid_lookups[$lookup]['start']) . "'" : ''; $where_time .= !in_array($lookup, $no_time) ? " AND users_to_marks.created_on < '" . date('Y-m-d', $valid_lookups[$lookup]['finish']) . "'" : ''; $this->data['lookup_type'] = $lookup; } elseif ($lookup == 'label') { // Get label ID $label_id = $finish; $this->load->model('labels_model', 'label'); if (!is_numeric($label_id)) { $label = $this->label->read("slug = '" . $this->db->escape_str($label_id) . "'", 1, 1, 'label_id, name'); $label_id = isset($label->label_id) ? $label->label_id : 0; $label_name = isset($label->name) ? $label->name : null; } else { $label = $this->label->read("label_id = '" . $this->db->escape_str($label_id) . "'", 1, 1, 'name'); $label_name = isset($label->name) ? $label->name : null; } // Set the new where clause // Set lookup type $where_time = " AND users_to_marks.label_id = '" . $label_id . "'"; $this->data['lookup_type'] = 'label'; // Give Tim Tim his Active Label Array already! if (parent::isWebView() === true || parent::isPJAX() === true) { $this->data['active_label'] = array('label_id' => $label_id, 'label_name' => $label_name); } } elseif ($lookup == 'tag') { // Get label ID $tag_id = $finish; $this->load->model('tags_model', 'tag'); if (!is_numeric($tag_id)) { $tag = $this->tag->read("slug = '" . $this->db->escape_str($tag_id) . "'", 1, 1, '*'); $tag_id = isset($tag->tag_id) ? $tag->tag_id : 0; $tag_slug = isset($tag->slug) ? $tag->slug : null; $tag_name = isset($tag->name) ? $tag->name : null; } else { $tag = $this->tag->read("tag_id = '" . $this->db->escape_str($tag_id) . "'", 1, 1, '*'); $tag_slug = isset($tag->slug) ? $tag->slug : null; $tag_name = isset($tag->name) ? $tag->name : null; } // Set the new where clause // Set lookup type $this->data['lookup_type'] = 'tag'; $options['tag_id'] = $tag_id; // Set active tag if (parent::isWebView() === true || parent::isPJAX() === true) { $this->data['active_tag'] = array('tag_id' => $tag_id, 'tag_name' => $tag_name, 'tag_slug' => $tag_slug); } } else { // Check for valid dates $dates = findStartFinish($lookup, $finish); $where_time .= " AND users_to_marks.created_on >= '" . $dates['start'] . "'"; $where_time .= " AND users_to_marks.created_on < '" . $dates['finish'] . "'"; $this->data['lookup_type'] = 'custom_date'; } // Figure the page number $page = findPage(); // Archives $search_archives = isset($this->clean->archive) && !empty($this->clean->archive) && $lookup == 'search' ? true : false; $archive = $lookup == 'archive' || $search_archives == true ? 'IS NOT NULL' : 'IS NULL'; // Search it up $search = null; if (isset($this->db_clean->q) && !empty($this->db_clean->q)) { $search = " AND users_to_marks.notes LIKE '%" . $this->db_clean->q . "%'"; $options['search'] = $this->db_clean->q; $options['user_id'] = $this->user_id; $options['archive'] = $archive; } // Set where $where = "users_to_marks.user_id='" . $this->user_id . "' AND users_to_marks.active = '1' AND users_to_marks.archived_on " . $archive . $where_time . $search; // Set actual sort // Get all the marks $marks = $this->user_marks->readComplete($where, $this->limit, $page, null, $options); // Check for marks // If false, return error; set total to 0 if ($marks === false) { $this->data['errors'] = formatErrors(2); $this->data['total'] = 0; } else { // Set marks $this->data['marks'] = $marks; // If a search, get totals here if (isset($options['search'])) { $this->data = $this->user_marks->getTotalsSearch($page, $this->limit, $this->data, $options['search'], $options['user_id'], $options['archive']); } else { $join = isset($options['tag_id']) && !empty($options['tag_id']) ? "INNER JOIN user_marks_to_tags UMTT ON users_to_marks.users_to_mark_id = UMTT.users_to_mark_id AND UMTT.tag_id = '" . $options['tag_id'] . "'" : null; $this->data = $this->user_marks->getTotals($where, $page, $this->limit, $this->data, $join); } } // If web view // Get stats, labels and tags // else skip this section and just return the marks if (parent::isWebView() === true) { self::getStats(); self::getLabels(); self::getTags(); } // Figure if web, redirect, internal ajax call or API $this->figureView('marks/index'); }
public function getTotal($type, $user_id, $start = null, $finish = null) { $type = trim(strtolower($type)); $types = array('archived' => 'archived_on', 'saved' => 'created_on', 'marks' => 'created_on'); // If type not found, return 0 if (!array_key_exists($type, $types)) { return 0; } // Set column from type $column = $types[$type]; // Figure start & finish if (!empty($start)) { $dates = findStartFinish($start, $finish); } // Figure date range $when = " AND active = '1'"; // If from is not empty, figure timestamp if (isset($dates['start']) && !empty($dates['start'])) { //$when .= " AND UNIX_TIMESTAMP(" . $column . ") >= '" . $dates['start'] . "'"; $when .= " AND " . $column . " >= '" . $dates['start'] . "'"; } // if to is not empty, figure timestamp if (isset($dates['finish']) && !empty($dates['finish'])) { //$when .= " AND UNIX_TIMESTAMP(" . $column . ") <= '" . $dates['finish'] . "'"; $when .= " AND " . $column . " < '" . $dates['finish'] . "'"; } // If when is empty, set to IS NOT NULL if ($type == 'marks') { $when .= " AND archived_on IS NULL"; } elseif ($type == 'archived') { $when .= " AND archived_on IS NOT NULL"; } return $this->count("user_id='" . $user_id . "'" . $when); }