function build_autos($items, $data, $user) { foreach ($items as $index => &$item) { if (isset($item['auto'])) { if (isset($data['urls'][$item['auto']])) { $item['path'] = $data['urls'][$item['auto']]; } else { if ($item['auto'] === 'set') { $item['path'] = ''; } } if ($item['auto'] === 'profile') { switch ($item['id']) { case 'twitter': $item['path'] = 'https://twitter.com/' . $user->twitter; break; default: $item['path'] = $user->{$item['id']}; if (empty($item['path'])) { unset($items[$index]); continue; } break; } if (!isset($item['label']) || empty($item['label'])) { $item['label'] = ucwords($item['id']) . ($item['id'] === 'google' ? '+' : ''); } } else { if ($item['auto'] === 'rss') { $item['path'] = '/feed/' . $item['id'] . ($item['id'] === 'essay' ? 's' : '') . '/recent.rss'; if (!isset($item['label'])) { $item['label'] = $data['url_data'][$item['id']]['plural'] . ' RSS'; } } else { if (preg_match('/s$/', $item['auto']) || $item['auto'] === 'timeline') { if ($item['auto'] === 'timeline' && isset($item['year'])) { $item['path'] .= $item['year'] . '/'; if (isset($item['month']) && $item['month'] !== false && $item['month'] !== 'any') { $m = str_pad($item['month'], 2, '0', STR_PAD_LEFT); $item['path'] .= $m . '/'; } } if (strpos($item['auto'], '_') !== false) { foreach (array('id', 'slug', 'month', 'year', 'day') as $id) { if ($id === 'month') { if (!isset($item['month']) || $item['month'] === 'any' || $item['month'] === false) { $item['month'] = ''; } else { $item['month'] = str_pad($item['month'], 2, '0', STR_PAD_LEFT); } } if ($id === 'day' && !isset($item['day'])) { $item['day'] = ''; } if ($id === 'slug' && !isset($item['slug']) && isset($item['id'])) { if (strpos($item['auto'], 'tag_') === 0) { $item['slug'] = $item['id']; } else { $c = new Category(); if (is_numeric($item['id'])) { $c->select('slug')->get_by_id($item['id']); $item['slug'] = $c->slug; } else { $item['slug'] = $item['id']; } } } if (isset($item[$id])) { $item['path'] = str_replace(":{$id}", $item[$id], $item['path']); } } } else { if (!isset($item['label'])) { $item['label'] = $data['url_data'][$item['auto'] === 'categories' ? 'category' : rtrim($item['auto'], 's')]['plural']; } } } else { if ($item['auto'] === 'home') { if (!isset($item['label'])) { $item['label'] = $data['url_data']['home']; } $item['path'] = '/home/'; } else { if ($item['auto'] === 'album' || $item['auto'] === 'set') { $a = new Album(); $a->select('id,slug,created_on,title'); if (is_numeric($item['id'])) { $a->where('id', $item['id']); } else { $a->where('slug', $item['id'])->or_where('internal_id', $item['id']); } $a->get(); if (!$a->exists()) { unset($items[$index]); continue; } $item['path'] = str_replace(':id', $a->id, $item['path']); $item['path'] = str_replace(':slug', $a->slug, $item['path']); $item['path'] = str_replace(':year', date('Y', $a->created_on), $item['path']); $item['path'] = str_replace(':month', date('m', $a->created_on), $item['path']); $item['path'] = str_replace(':day', date('d', $a->created_on), $item['path']); if (!isset($item['label'])) { $item['label'] = $a->title; } } else { if ($item['auto'] === 'page' || $item['auto'] === 'essay') { $t = new Text(); $t->select('id,slug,published_on,title'); if (is_numeric($item['id'])) { $t->where('id', $item['id']); } else { $t->where('slug', $item['id']); } $t->get(); if (!$t->exists()) { unset($items[$index]); continue; } $item['path'] = str_replace(':id', $t->id, $item['path']); $item['path'] = str_replace(':slug', $t->slug, $item['path']); $item['path'] = str_replace(':year', date('Y', $t->published_on), $item['path']); $item['path'] = str_replace(':month', date('m', $t->published_on), $item['path']); $item['path'] = str_replace(':day', date('d', $t->published_on), $item['path']); if (!isset($item['label'])) { $item['label'] = $t->title; } } else { if ($item['auto'] === 'content') { $c = new Content(); $c->select('id,slug,captured_on,title'); if (isset($item['album_id'])) { $item['path'] = preg_replace('/:(id|slug)/', ':album_$1', $data['urls']['album']) . substr(str_replace(':year/:month/', '', $data['urls']['content']), 1); $a = new Album(); $a->select('id,slug,created_on,title'); if (is_numeric($item['album_id'])) { $a->where('id', $item['album_id']); } else { $a->where('slug', $item['album_id'])->or_where('internal_id', $item['album_id']); } $a->get(); if (!$a->exists()) { unset($items[$index]); continue; } $item['path'] = str_replace(':album_id', $a->id, $item['path']); $item['path'] = str_replace(':album_slug', $a->slug, $item['path']); $date = $a->created_on; } else { $date = $c->captured_on; } if (is_numeric($item['id'])) { $c->where('id', $item['id']); } else { $c->where('slug', $item['id'])->or_where('internal_id', $item['id']); } $c->get(); if (!$c->exists()) { unset($items[$index]); continue; } $item['path'] = str_replace(':id', $c->id, $item['path']); $item['path'] = str_replace(':slug', $c->slug, $item['path']); $item['path'] = str_replace(':year', date('Y', $date), $item['path']); $item['path'] = str_replace(':month', date('m', $date), $item['path']); $item['path'] = str_replace(':day', date('d', $date), $item['path']); if (!isset($item['label'])) { $item['label'] = $c->title; } if (isset($item['lightbox']) && $item['lightbox']) { $item['path'] .= 'lightbox/'; } } else { if ($item['auto'] === 'tag') { $item['path'] = str_replace(':slug', $item['id'], $item['path']); } } } } } } } } if ($item['auto'] !== 'profile') { $item['path'] = str_replace(array(':year', ':month'), '', $item['path']); $item['path'] = preg_replace('/[\\(\\)\\?\\:]/', '', $item['path']); $item['path'] = preg_replace('~[/]+~', '/', $item['path']); } } } return $items; }
/** * Display a listing of the resource. * * @return Response */ public function index($id) { $input = Input::all(); $data = Text::where('id', $id)->select(['title', 'description'])->get(); return Common::returnData(200, SUCCESS, $input['user_id'], $input['session_id'], $data); }
<?php $t = new Text(); $fields = $this->db->query("SHOW COLUMNS FROM {$t->table}"); $result = $fields->result(); $columns = array(); foreach ($result as $field) { $columns[] = $field->Field; } if (in_array('tags_migrated', $columns)) { foreach ($t->where('tags_migrated', 0)->limit(100)->get_iterated() as $text) { $text->_format_tags(trim($text->tags_old, ',')); $text->tags_migrated = 1; $text->save(); } if ($t->where('tags_migrated', 0)->count() === 0) { $done = true; } } else { $done = true; }
<?php $s = new Slug(); $slug_count = $s->like('id', 'page.', 'after')->count(); $c = new Text(); $content_count = $c->where('page_type', 1)->count(); if ($slug_count < $content_count) { $slugs = array(); $c = new Text(); foreach ($c->where('page_type', 1)->select('slug')->get_iterated() as $content) { $slugs[] = "('page." . $content->slug . "')"; } $slugs = join(', ', $slugs); $this->db->query("INSERT INTO {$s->table}(id) VALUES {$slugs} ON DUPLICATE KEY UPDATE id=id;"); } $done = true;
function members() { list($params, $id, $slug) = $this->parse_params(func_get_args()); $cat = new Category(); if (isset($params['content'])) { $getter = new Content(); $model = $url_bit = 'content'; } else { if (isset($params['albums'])) { $getter = new Album(); $model = 'album'; $url_bit = 'albums'; } else { if (isset($params['essays'])) { $getter = new Text(); $model = $url_bit = 'text'; } } } if (is_null($id) && !$slug) { $this->error('403', 'Required parameter "id" not present.'); return; } else { if (is_array($id)) { list($id, $content_id) = $id; } } if ($this->method != 'get') { $id = explode(',', $id); if (!isset($content_id)) { $this->error('403', 'Required content id not present.'); return; } if (strpos($content_id, ',') !== FALSE) { $ids = explode(',', $content_id); } else { $ids = array($content_id); } if (isset($params['content'])) { $c = new Content(); } else { if (isset($params['albums'])) { $c = new Album(); } else { $c = new Text(); $c->where('page_type', 0); } } $categories = $cat->where_in('id', $id)->get_iterated(); $first_category_id = false; foreach ($categories as $category) { if (!$first_category_id) { $first_category_id = $category->id; } $members = $category->{$model . 's'}->select('id')->get_iterated(); $member_ids = array(); foreach ($members as $member) { $member_ids[] = $member->id; } $contents = $c->where_in('id', $ids)->order_by('id ASC')->get_iterated(); foreach ($contents as $content) { if ($content->exists()) { switch ($this->method) { case 'post': case 'put': $category->save($content); break; case 'delete': $category->delete($content); break; } } } $category->update_counts($c->model); } if (count($categories) > 1 || $this->method == 'delete') { exit; } else { $this->redirect("/categories/{$first_category_id}/{$url_bit}"); } } if (!is_null($id)) { $category = $cat->get_by_id($id); } else { if ($slug) { $category = $cat->get_by_slug($slug); } } if (!$category->exists()) { $this->error('404', 'Category not found.'); return; } $params['auth'] = $this->auth; if ($model === 'album') { $final = $getter->listing(array_merge($params, array('category' => $category->id))); } else { $params['category'] = $category->id; $final = $getter->listing($params); } $final['category'] = $category->to_array(); $this->set_response_data($final); }
<?php $c = new Text(); $c->where('slug', NULL)->or_where('slug', '')->limit(100)->get_iterated(); foreach ($c as $content) { $content->slug = '__generate__'; $content->save(); } $c = new Text(); if ($c->where('slug', NULL)->or_where('slug', '')->count() === 0) { $done = true; }
function aggregate($type, $options = array()) { $options = array_merge(array('featured' => false), $options); $shared_params = array(); if ($type === 'tag') { $shared_params['tags'] = $options['tag_slug']; } else { if ($type === 'category') { $shared_params['category'] = $options['category']; } } $album_params = $shared_params; $date_marker = false; if ($type === 'date') { $s = new Setting(); $s->where('name', 'site_timezone')->get(); $tz = new DateTimeZone($s->value); $offset = $tz->getOffset(new DateTime('now', new DateTimeZone('UTC'))); if ($offset === 0) { $shift = ''; } else { $shift = ($offset < 0 ? '-' : '+') . abs($offset); } // Need to - the offset here, as we need to shift this timestamp by the inverse of the offset to match DB UTC time. // For example. Midnight in user's time (say, CT -5) is UTC+5. $album_params['before'] = $date_marker = strtotime("{$options['year']}-{$options['month']}-{$options['day']} 23:59:59") - $offset; } $aggregate = $essay_ids = $album_ids = $content_ids = $updated_album_ids = $exclude_albums = $exclude_content = $sets = $range = array(); $t = new Text(); $t->select('id, featured, featured_image_id, published_on')->where('page_type', 0)->where('published', 1); if ($type === 'date') { $t->where("YEAR(FROM_UNIXTIME({$t->table}.published_on{$shift}))", $options['year'])->where("MONTH(FROM_UNIXTIME({$t->table}.published_on{$shift}))", $options['month'])->where("DAY(FROM_UNIXTIME({$t->table}.published_on{$shift}))", $options['day']); } else { if ($type === 'tag') { $t->where_related('tag', 'id', $options['tag']); } else { $t->where_related('category', 'id', $options['category']); } } if ($options['featured']) { $t->where('featured', 1); } $t->include_related('album', 'id')->order_by($t->table . '.published_on DESC')->get_iterated(); foreach ($t as $essay) { $essay_ids[$essay->id] = $essay->published_on; $aggregate[] = array('type' => 'essay', 'id' => $essay->id, 'date' => $essay->published_on, 'featured' => $essay->featured); if ($essay->album_id) { $exclude_albums[] = $essay->album_id; } if (is_numeric($essay->featured_image_id)) { $exclude_content[] = $essay->featured_image_id; } } $a = new Album(); $a->select('id, featured, published_on, left_id, right_id, level')->where('visibility', 0)->where('deleted', 0)->where('total_count >', 0); if ($type === 'date') { $a->where("YEAR(FROM_UNIXTIME({$a->table}.published_on{$shift}))", $options['year'])->where("MONTH(FROM_UNIXTIME({$a->table}.published_on{$shift}))", $options['month'])->where("DAY(FROM_UNIXTIME({$a->table}.published_on{$shift}))", $options['day']); } else { if ($type === 'tag') { $a->where_related('tag', 'id', $options['tag']); } else { $a->where_related('category', 'id', $options['category']); } } if ($options['featured']) { $a->where('featured', 1); } $a->include_related('content', 'id')->order_by($a->table . '.published_on DESC')->get_iterated(); foreach ($a as $album) { if (is_numeric($album->content_id)) { $exclude_content[] = $album->content_id; } if (!array_key_exists($album->id, $album_ids) && !in_array($album->id, $exclude_albums)) { $album_ids[$album->id] = $album->published_on; $aggregate[] = array('type' => 'album', 'id' => $album->id, 'date' => $album->published_on, 'featured' => $album->featured); } if ($album->level < 2) { $range = array_merge($range, range($album->left_id, $album->right_id)); } if ($album->level > 1) { $sets[$album->id] = $album->left_id; } } foreach ($sets as $id => $left) { if (in_array($left, $range)) { unset($album_ids[$id]); foreach ($aggregate as $i => $info) { if ($info['type'] === 'album' && $info['id'] == $id) { unset($aggregate[$i]); } } } } $c = new Content(); $c->select('id, published_on, featured'); if (!empty($exclude_content)) { $c->where_not_in('id', $exclude_content); } $c->where('visibility', 0)->where('deleted', 0); if ($type === 'date') { $c->include_related('album')->where("YEAR(FROM_UNIXTIME({$c->table}.published_on{$shift}))", $options['year'])->where("MONTH(FROM_UNIXTIME({$c->table}.published_on{$shift}))", $options['month'])->where("DAY(FROM_UNIXTIME({$c->table}.published_on{$shift}))", $options['day'])->group_start()->where($a->table . '.id', null)->or_where($a->table . '.deleted', 0)->group_end(); } else { if ($type === 'tag') { $c->where_related('tag', 'id', $options['tag']); } else { $c->where_related('category', 'id', $options['category']); } } if ($options['featured']) { $c->where('featured', 1); } $c->order_by($c->table . '.published_on DESC')->get_iterated(); foreach ($c as $content) { if ($content->album_id && $content->album_visibility < 1 && $content->album_published_on <= $date_marker) { if (!isset($updated_album_ids[$content->album_id])) { $updated_album_ids[$content->album_id] = array('items' => array($content->id), 'date' => $content->published_on, 'featured' => $content->album_featured); } else { $updated_album_ids[$content->album_id]['items'][] = $content->id; $updated_album_ids[$content->album_id]['date'] = max($content->published_on, $updated_album_ids[$content->album_id]['date']); } } else { if (!$content->album_id) { $content_ids[$content->id] = $content->published_on; $aggregate[] = array('type' => 'content', 'id' => $content->id, 'date' => $content->published_on, 'featured' => $content->featured); } } } foreach ($updated_album_ids as $id => $a) { $aggregate[] = array('type' => 'updated_album', 'id' => $id, 'date' => $a['date'], 'featured' => $a['featured']); } $total = count($aggregate); if (!function_exists('_sort')) { function _sort($one, $two) { if ($one['featured'] && !$two['featured']) { return -1; } else { if ($one['featured'] && $two['featured']) { return $one['date'] < $two['date'] ? 1 : -1; } } return $two['featured'] || $one['date'] < $two['date'] || $one['date'] === $two['date'] && $two['id'] > $one['id'] ? 1 : -1; } } usort($aggregate, '_sort'); $stream = array('page' => (int) isset($options['page']) ? (int) $options['page'] : 1, 'pages' => (int) ceil($total / $options['limit']), 'per_page' => (int) min($options['limit'], $total), 'total' => (int) $total); $load = array_slice($aggregate, ($stream['page'] - 1) * $options['limit'], $options['limit']); $counts = array('essays' => count($essay_ids), 'albums' => count($album_ids), 'content' => count($content_ids)); $counts['total'] = $counts['essays'] + $counts['albums'] + $counts['content']; $updated_album_ids_arr = $updated_album_ids; $essay_ids = $album_ids = $content_ids = $updated_album_ids = $final = $index = array(); foreach ($load as $i => $item) { $index[$item['type'] . '-' . $item['id']] = $i; ${$item['type'] . '_ids'}[] = $item['id']; } if (!empty($essay_ids)) { $e = new Text(); $e->where_in('id', $essay_ids)->get_iterated(); foreach ($e as $essay) { $final[$index['essay-' . $essay->id]] = $essay->to_array($shared_params); } } if (!empty($album_ids)) { $a = new Album(); $a->where_in('id', $album_ids)->get_iterated(); foreach ($a as $album) { $final[$index['album-' . $album->id]] = $album->to_array($album_params); } } if (!empty($content_ids)) { $c = new Content(); $c->where_in('id', $content_ids)->get_iterated(); foreach ($c as $content) { $final[$index['content-' . $content->id]] = $content->to_array(array_merge($shared_params, array('order_by' => 'published_on'))); } } if (!empty($updated_album_ids)) { $a = new Album(); $a->where_in('id', $updated_album_ids)->get_iterated(); foreach ($a as $album) { $arr = $album->to_array(); $arr['event_type'] = 'album_update'; $arr['content'] = array(); $info = $updated_album_ids_arr[$album->id]; $c = new Content(); $c->where_in('id', $info['items'])->order_by('published_on DESC')->get_iterated(); foreach ($c as $i => $content) { $carr = $content->to_array(array('order_by' => 'published_on', 'in_album' => $album)); if ($i === 0) { $arr['date'] = $carr['date']; } $arr['content'][] = $carr; } $final[$index['updated_album-' . $album->id]] = $arr; } } ksort($final); $stream['items'] = array_values($final); return array($stream, $counts); }
$fields = $this->db->query("SHOW COLUMNS FROM {$t->table}"); $run = true; if ($fields) { $result = $fields->result(); if ($result && strtolower($result[0]->Type) === 'int(9)') { $run = false; } } if ($run) { $this->db->query("TRUNCATE TABLE {$t->table}"); $this->db->query("ALTER TABLE {$t->table} DROP COLUMN count"); $this->db->query("ALTER TABLE {$c->table} DROP COLUMN count"); $this->db->query("ALTER TABLE {$t->table} DROP COLUMN id"); $this->db->query("ALTER TABLE {$t->table} ADD id INT(9) AUTO_INCREMENT PRIMARY KEY FIRST"); $a = new Album(); $c = new Content(); $t = new Text(); $this->db->query("ALTER TABLE {$a->table} DROP COLUMN tags_migrated"); $this->db->query("ALTER TABLE {$c->table} DROP COLUMN tags_migrated"); $this->db->query("ALTER TABLE {$t->table} DROP COLUMN tags_migrated"); $this->db->query("ALTER TABLE {$a->table} ADD tags_migrated TINYINT(1) DEFAULT 0"); $this->db->query("ALTER TABLE {$c->table} ADD tags_migrated TINYINT(1) DEFAULT 0"); $this->db->query("ALTER TABLE {$t->table} ADD tags_migrated TINYINT(1) DEFAULT 0"); $this->db->query("ALTER TABLE {$a->table} CHANGE tags tags_old TEXT"); $this->db->query("ALTER TABLE {$c->table} CHANGE tags tags_old TEXT"); $this->db->query("ALTER TABLE {$t->table} CHANGE tags tags_old TEXT"); $a->where('tags_old', null)->update(array('tags_migrated' => 1)); $c->where('tags_old', null)->update(array('tags_migrated' => 1)); $t->where('tags_old', null)->update(array('tags_migrated' => 1)); } $done = true;
function _all($params) { $s = new Setting(); $s->where('name', 'site_timezone')->get(); $tz = new DateTimeZone($s->value); $offset = $tz->getOffset(new DateTime('now', new DateTimeZone('UTC'))); if ($offset === 0) { $shift = ''; } else { $shift = ($offset < 0 ? '-' : '+') . abs($offset); } $content_col = $params['content_column']; $select = "COUNT(*) as count, YEAR(FROM_UNIXTIME({$content_col}{$shift})) as event_year"; $group = 'event_year'; $order = 'event_year DESC'; if (!$params['scope'] || $params['scope'] !== 'year') { $select .= ", MONTH(FROM_UNIXTIME({$content_col}{$shift})) as event_month"; $group .= ',event_month'; $order .= ',event_month DESC'; } if (!$params['limit_to'] && !$params['scope']) { $select .= ", DAY(FROM_UNIXTIME({$content_col}{$shift})) as event_day"; $group .= ',event_day'; $order .= ',event_day DESC'; } $a = new Album(); $c = new Content(); $t = new Text(); $c->select(str_replace($content_col, $c->table . '.' . $content_col, $select))->include_related('album', 'id')->where('visibility', 0)->where('deleted', 0); if (!$params['limit_to']) { $c->group_start()->where('album_id', null)->or_where($c->table . '.published_on > `' . $a->table . '`.`published_on`')->group_end(); } $a->select(str_replace($content_col, 'published_on', $select))->where('visibility', 0)->where('deleted', 0)->where('total_count >', 0); $t->select(str_replace($content_col, 'published_on', $select))->where('page_type', 0)->where('published', 1); if ($params['featured']) { $c->where('featured', 1); $a->where('featured', 1); $t->where('featured', 1); } $c->group_by($group)->order_by($order); $t->group_by($group)->order_by($order); $a->group_by($group)->order_by($order); $dates = array(); if (!$params['limit_to'] || $params['limit_to'] === 'content') { foreach ($c->get_iterated() as $content) { if ($params['scope'] === 'year') { $key = "{$content->event_year}"; } else { if ($params['limit_to'] || $params['scope']) { $key = "{$content->event_year}-{$content->event_month}"; } else { $key = "{$content->event_year}-{$content->event_month}-{$content->event_day}"; } } $key = strtotime($key); if (is_numeric($content->event_year)) { $dates[$key] = array('year' => (int) $content->event_year, 'month' => (int) $content->event_month, 'day' => (int) $content->event_day, 'counts' => array('content' => (int) $content->count, 'albums' => 0, 'essays' => 0)); } } } if (!$params['limit_to'] || $params['limit_to'] === 'albums') { foreach ($a->get_iterated() as $album) { if ($params['scope'] === 'year') { $key = "{$album->event_year}"; } else { if ($params['limit_to'] || $params['scope']) { $key = "{$album->event_year}-{$album->event_month}"; } else { $key = "{$album->event_year}-{$album->event_month}-{$album->event_day}"; } } $key = strtotime($key); if (is_numeric($album->event_year)) { if (!isset($dates[$key])) { $dates[$key] = array('year' => (int) $album->event_year, 'month' => (int) $album->event_month, 'day' => (int) $album->event_day, 'counts' => array('content' => 0, 'albums' => (int) $album->count, 'essays' => 0)); } else { $dates[$key]['counts']['albums'] = (int) $album->count; } } } } if (!$params['limit_to'] || $params['limit_to'] === 'essays') { foreach ($t->get_iterated() as $essay) { if ($params['scope'] === 'year') { $key = "{$essay->event_year}"; } else { if ($params['limit_to'] || $params['scope']) { $key = "{$essay->event_year}-{$essay->event_month}"; } else { $key = "{$essay->event_year}-{$essay->event_month}-{$essay->event_day}"; } } $key = strtotime($key); if (is_numeric($essay->event_year)) { if (!isset($dates[$key])) { $dates[$key] = array('year' => (int) $essay->event_year, 'month' => (int) $essay->event_month, 'day' => (int) $essay->event_day, 'counts' => array('content' => 0, 'albums' => 0, 'essays' => (int) $essay->count)); } else { $dates[$key]['counts']['essays'] = (int) $essay->count; } } } } krsort($dates); return $dates; }
function index() { list($params, $id, $slug) = $this->parse_params(func_get_args()); $params['auth'] = $this->auth; // Create or update if ($this->method != 'get') { $t = new Text(); switch ($this->method) { case 'post': case 'put': if ($id) { $t->get_by_id($id); $t->old_published = $t->published; $t->current_slug = $t->slug; if (isset($_POST['unpublish'])) { $_POST['published'] = 0; $_POST['published_on'] = null; } } else { if (isset($_POST['page_type']) && $_POST['page_type'] === 'page') { $_POST['published'] = 1; } } $arr = $_POST; global $raw_input_data; if (isset($raw_input_data['content'])) { $arr['content'] = $raw_input_data['content']; } if (isset($raw_input_data['draft'])) { $arr['draft'] = $raw_input_data['draft']; } // Little hack here to make sure content validation is always run // (newline gets stripped in text->_format_content) if (isset($arr['content'])) { $arr['content'] .= "\n"; } try { $t->from_array($arr, array(), true); } catch (Exception $e) { $this->error('400', $e->getMessage()); return; } if (isset($_POST['tags'])) { $t->_format_tags($_POST['tags']); } else { if ($this->method === 'put' && isset($_POST['published'])) { $t->_update_tag_counts(); } } $arr = $t->to_array(array('expand' => true)); if ($id) { Shutter::hook('text.update', $arr); } else { Shutter::hook('text.create', $arr); } $this->redirect("/text/{$t->id}" . (isset($params['render']) ? '/render:' . $params['render'] : '')); break; case 'delete': if (is_null($id)) { $this->error('403', 'Required parameter "id" not present.'); return; } else { if (is_numeric($id)) { $id = array($id); } else { $id = explode(',', $id); } $tags = array(); foreach ($id as $text_id) { $text = $t->get_by_id($text_id); if ($text->exists()) { $tags = array_merge($tags, $text->tags); $s = new Slug(); $prefix = $text->page_type == 0 ? 'essay' : 'page'; $this->db->query("DELETE FROM {$s->table} WHERE id = '{$prefix}.{$text->slug}'"); Shutter::hook('text.delete', $text->to_array(array('auth' => true))); if (!$text->delete()) { // TODO: More info $this->error('500', 'Delete failed.'); return; } } } } exit; break; } } $p = new Text(); // No id, so we want a list if (is_null($id) && !$slug) { $params['state'] = 'published'; $final = $p->listing($params); } else { if (!is_null($id)) { if (is_numeric($id)) { $page = $p->get_by_id($id); } else { $this->auth = $params['auth'] = true; $page = $p->get_by_internal_id($id); } } else { if ($slug) { $p->group_start()->where('slug', $slug)->or_like('old_slug', ',' . $slug . ',', 'both')->group_end(); if (isset($params['type'])) { $p->where('page_type', $params['type'] === 'essay' ? 0 : 1); } $page = $p->get(); } } $params['expand'] = true; if ($page->exists()) { $final = $page->to_array($params); if (!$this->auth && !$final['published']) { $this->error('404', 'Not found'); return; } } else { $this->error('404', "Text with ID: {$id} not found."); return; } if ($final['page_type'] === 'essay' && $page->published) { $options = array('neighbors' => false, 'context' => false); $options = array_merge($options, $params); if ($options['neighbors']) { // Make sure $neighbors is at least 2 $options['neighbors'] = max($options['neighbors'], 2); // Make sure neighbors is even if ($options['neighbors'] & 1 != 0) { $options['neighbors']++; } $options['neighbors'] = $options['neighbors'] / 2; } else { $options['neighbors'] = 1; } if ($options['neighbors']) { // TODO: Performance check $next = new Text(); $prev = new Text(); $to_arr_options = array('auth' => $this->auth); $next->group_start()->where('page_type', 0)->where('published', 1)->group_start()->where('published_on <', $page->published_on)->or_group_start()->where('published_on =', $page->published_on)->where('id <', $page->id)->group_end()->group_end()->group_end(); $prev->group_start()->where('page_type', 0)->where('published', 1)->group_start()->where('published_on >', $page->published_on)->or_group_start()->where('published_on =', $page->published_on)->where('id >', $page->id)->group_end()->group_end()->group_end(); if (strpos($options['context'], 'tag-') === 0) { $tag = str_replace('tag-', '', urldecode($options['context'])); $t = new Tag(); $t->where('name', $tag)->get(); $to_arr_options['context'] = "tag-{$tag}"; if ($t->exists()) { $next->where_related_tag('id', $t->id); $prev->where_related_tag('id', $t->id); $final['context']['type'] = 'tag'; $final['context']['title'] = $tag; $final['context']['slug'] = $tag; $t->model = 'tag_essays'; $t->slug = $t->name; $url = $t->url(); if ($url) { list($final['context']['__koken_url'], $final['context']['url']) = $url; } } } else { if (strpos($options['context'], 'category-') === 0) { $category = str_replace('category-', '', $options['context']); $cat = new Category(); $cat->where('slug', $category)->get(); if ($cat->exists()) { $next->where_related_category('id', $cat->id); $prev->where_related_category('id', $cat->id); $final['context']['type'] = 'category'; $final['context']['title'] = $cat->title; $final['context']['slug'] = $cat->slug; $to_arr_options['context'] = "category-{$cat->id}"; $cat->model = 'category_essays'; $url = $cat->url(); if ($url) { list($final['context']['__koken_url'], $final['context']['url']) = $url; } } } } $max = $next->get_clone()->count(); $min = $prev->get_clone()->count(); $final['context']['total'] = $max + $min + 1; $final['context']['position'] = $min + 1; $pre_limit = $next_limit = $options['neighbors']; if ($min < $pre_limit) { $next_limit += $pre_limit - $min; $pre_limit = $min; } if ($max < $next_limit) { $pre_limit = min($min, $pre_limit + ($next_limit - $max)); $next_limit = $max; } $final['context']['previous'] = array(); $final['context']['next'] = array(); if ($next_limit > 0) { $next->order_by('published_on DESC, id DESC')->limit($next_limit); $next->get_iterated(); foreach ($next as $c) { $final['context']['next'][] = $c->to_array($to_arr_options); } } if ($pre_limit > 0) { $prev->order_by('published_on ASC, id ASC')->limit($pre_limit); $prev->get_iterated(); foreach ($prev as $c) { $final['context']['previous'][] = $c->to_array($to_arr_options); } $final['context']['previous'] = array_reverse($final['context']['previous']); } } } } $this->set_response_data($final); }
/** * Get texts for rendering all texts. * * @return void */ public function getTexts() { global $data; $controllerAction = Route::currentRouteAction(); list($controller, $action) = explode('@', $controllerAction); $controller = strtolower(str_replace('Controller', '', $controller)); $lang = Route::input('lang'); $data['texts'] = Text::where('controller', '=', $controller)->where('action', '=', $action)->orWhere(function ($query) { $query->where('action', '=', 'global'); })->lists($lang, 'code'); $data['controller'] = $controller; $data['action'] = $action; $data['language'] = $lang; }