Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- require(APPPATH."/libraries/REST_Controller.php");
- class Blog extends REST_Controller {
- function __construct()
- {
- parent::__construct();
- $this->load->model('api_model', 'model');
- }
- function grades_get()
- {
- $grade_id = (int)$this->uri->segment(4, 0);
- if ($grade_id === 0) {
- $data = array();
- } else {
- $data = $this->model->get_grade($grade_id);
- }
- $this->response($data);
- }
- function classes_get()
- {
- $router = $this->_routers();
- $data = $this->_get_info($router);
- $this->response($data);
- }
- function classes_put()
- {
- $router = $this->_routers();
- $allow = $this->_allow_put($router);
- if (!$allow) {
- $data = array();
- } else {
- $data = $this->_put($router);
- }
- $this->response($data);
- }
- function classes_post()
- {
- $router = $this->_routers();
- $allow = $this->_allow_post($router);
- if (!$allow) {
- $data = array();
- } else {
- $data = $this->_post($router);
- }
- $this->response($data);
- }
- function classes_delete()
- {
- $router = $this->_routers();
- $allow = $this->_allow_delete($router);
- if (!$allow) {
- $data = array();
- } else {
- $data = $this->_delete($router);
- }
- $this->response($data);
- }
- function _allow_post($router)
- {
- $allow = array(4, 5, 0);
- return in_array($router, $allow);
- }
- function _allow_put($router)
- {
- $allow = array(6, 10);
- return in_array($router, $allow);
- }
- function _allow_delete($router)
- {
- $allow = array(5, 6, 10);
- return in_array($router, $allow);
- }
- function _routers()
- {
- $class_id = (int)$this->uri->segment(4, 0);
- $search_segment = $this->uri->segment(5);
- $array = array(0 => 'grade', 1 => 'blogPosts', 2 => 'blogArchive', 3 => 'teachers');
- $key = array_search( $search_segment, $array);
- if ($key === FALSE) {
- if ($class_id === 0) {
- # classes/
- return $routers = 1;
- }else{
- # classes/<class-id>
- return $routers = 2;
- }
- }elseif ($key == 0) {
- # classes/<class-id>/grade
- return $routers = 3;
- }elseif ($key == 1) {
- $post_id = (int)$this->uri->segment(6, 0);
- $media_string = $this->uri->segment(7, 0);
- $media = $this->uri->segment(8, 0);
- if ($post_id === 0) {
- # classes/<class-id>/blogPosts
- return $routers = 4;
- }
- elseif (is_string($media)) {
- # classes/<class-id>/blogPosts/<post-id>/media/<media-file-name>
- return $routers = 5;
- }elseif ($media_string === 'media') {
- # classes/<class-id>/blogPosts/<post-id>/media/
- return $routers = 0;
- }elseif ($media_string === 0) {
- # classes/<class-id>/blogPosts/<post-id>
- return $routers = 6;
- }
- }elseif ($key == 2) {
- $yyyymm = $this->uri->segment(6, 0);
- $posts = $this->uri->segment(7);
- $post_id = (int)$this->uri->segment(8, 0);
- if ($yyyymm === 0) {
- # classes/<class-id>/blogArchive
- return $routers = 7;
- }
- elseif ($yyyymm === 'all') {
- # classes/<class-id>/blogArchive/all
- return $routers = 13;
- }
- elseif (!$posts) {
- # classes/<class-id>/blogArchive/<yyyymm>
- return $routers = 8;
- }
- elseif ($post_id === 0) {
- # classes/<class-id>/blogArchive/<yyyymm>/posts
- return $routers = 9;
- }
- else{
- # classes/<class-id>/blogArchive/<yyyymm>/posts/<post-id>
- return $routers = 10;
- }
- }elseif ($key == 3) {
- $teachers_id = (int)$this->uri->segment(6, 0);
- if ($teachers_id == 0) {
- # classes/345/teachers/
- return $routers = 11;
- } else {
- # classes/345/teachers/<teachers-id>
- return $routers = 12;
- }
- }
- }
- function _get_info($router)
- {
- switch ($router) {
- case '1':
- # classes/
- $classes_all = $this->model->get_classes_all();
- if (empty($classes_all)) {
- $data = 'No classes';
- } else {
- $i = 0;
- foreach ($classes_all as $value) {
- $grade = $this->model->get_grade($value['grade_id']);
- $classes = array(
- 'id' => $value['id'],
- 'name' => $value['name'],
- 'year' => $value['year'],
- 'note' => $value['note'],
- 'type' => $value['type'],
- 'grade' => $grade);
- $data[$i] = $classes;
- $i++;
- }
- }
- break;
- case '2':
- # classes/<class-id>
- $id = $this->uri->segment(4);
- $classes = $this->model->get_classes($id);
- if (empty($classes)) {
- $data = 'No classes';
- } else {
- $grade = $this->model->get_grade($classes->grade_id);
- $data = array(
- 'id' => $classes->id,
- 'name' => $classes->name,
- 'year' => $classes->year,
- 'note' => $classes->note,
- 'type' => $classes->type,
- 'grade' => $grade);
- }
- break;
- case '3':
- # classes/<class-id>/grade
- $class_id = $this->uri->segment(4);
- $grade_class = $this->model->get_grade_class($class_id);
- if (empty($grade_class)) {
- $data = 'No grade';
- } else {
- $data = $grade_class;
- }
- break;
- case '4':
- # classes/<class-id>/blogPosts
- $class_id = $this->uri->segment(4);
- $blogPosts = $this->model->get_posts($class_id);
- if (empty($blogPosts)) {
- $data = 'No posts';
- } else {
- $i = 0;
- foreach ($blogPosts as $value) {
- $publisher = $this->_publisher($value['publisher_id']);
- if ($value['corrector_id'] != NULL) {
- $corrector = $this->_publisher($value['corrector_id']);
- } else {
- $corrector = NULL;
- }
- $blogPost = array(
- 'id' => $value['id'],
- 'title' => $value['title'],
- 'contents' => $value['contents'],
- 'isPublished' => $value['is_published'],
- 'publicationTS' => $value['publication_ts'],
- 'publisher' => $publisher,
- 'correctionTS' => $value['correction_ts'],
- 'corrector' => $corrector);
- $data[$i] = $blogPost;
- $i++;
- }
- }
- break;
- case '5':
- # classes/<class-id>/blogPosts/<post-id>/media/<media-file-name>
- $media = $this->uri->segment(8);
- $get_media = $this->model->get_media($media);
- if (empty($get_media)) {
- $data = 'No media-file';
- } else {
- $get_media->url = site_url().'media/blogPosts/'.$get_media->id;
- $data = $get_media;
- }
- break;
- case '0':
- # classes/<class-id>/blogPosts/<post-id>/media/
- $post_id = $this->uri->segment(6);
- $get_media = $this->model->get_media_all($post_id);
- if (empty($get_media)) {
- $data = 'No media-file';
- } else {
- $i = 0;
- foreach ($get_media as $value) {
- $url = site_url().'media/blogPosts/'.$value['id'];
- $media = array(
- 'id' => $value['id'],
- 'size' => $value['size'],
- 'mime_type' => $value['mime_type'],
- 'file_name' => $value['file_name'],
- 'url' => $url);
- $data[$i] = $media;
- $i++;
- }
- }
- break;
- case '6':
- # classes/<class-id>/blogPosts/<post-id>
- $post_id = $this->uri->segment(6);
- $class_id = $this->uri->segment(4);
- $data = $this->_blogPost($class_id, $post_id);
- break;
- case '7':
- # classes/<class-id>/blogArchive
- $class_id = $this->uri->segment(4);
- $year = $this->get('year');
- $this->load->helper('date');
- $i = 0;
- $month = 1;
- while ( $month <= 12) {
- $min_date = $year.'-'.$month.'-01';
- $max_date = $year.'-'.$month.'-'.days_in_month($month, $year);;
- $count = $this->model->get_blogArchive($class_id, $min_date, $max_date);
- if (strlen($month) == 1) {
- $yyyymm = $year.'0'.$month;
- }else{
- $yyyymm = $year.$month;
- }
- if ($count != 0) {
- $data[$i] = array(
- 'id' => date("F", strtotime($min_date)),
- 'count' => $count,
- 'yyyymm' => $yyyymm);
- $i++;
- }
- $month++;
- }
- break;
- case '13':
- # classes/<class-id>/blogArchive/all
- $class_id = $this->uri->segment(4);
- $max_min_year = $this->model->get_max_min($class_id);
- if (is_null($max_min_year->min_year) && is_null($max_min_year->max_year) ) {
- $data = 'No blog Archive';
- } else {
- $max_year = substr((int)$this->_convert_year($max_min_year->max_year), 0, 4);
- $min_year = substr((int)$this->_convert_year($max_min_year->min_year), 0, 4);
- $i = 0;
- while ( $min_year <= $max_year) {
- $min_date = $min_year.'-01-01';
- $max_date = $min_year.'-12-31';
- $count = $this->model->get_blogArchive($class_id, $min_date, $max_date);
- if ($count != 0) {
- $data[$i] = array(
- 'id' => $min_year,
- 'count' => $count);
- $i++;
- }
- $min_year++;
- }
- arsort($data);
- }
- break;
- case '8':
- # classes/<class-id>/blogArchive/<yyyymm>
- $this->load->helper('date');
- $class_id = $this->uri->segment(4);
- $yyyymm = $this->uri->segment(6);
- $date_arr = $this->_date_start_end($yyyymm);
- $date_start = $date_arr[0];
- $date_end = $date_arr[1];
- $blogArchive = $this->model->get_blogArchive($class_id, $date_start, $date_end);
- if (empty($blogArchive)) {
- $data = 'No posts';
- } else {
- $data = array(
- 'id' => $yyyymm,
- 'count' => $blogArchive);
- }
- break;
- case '9':
- # classes/<class-id>/blogArchive/<yyyymm>/posts
- $this->load->helper('date');
- $class_id = $this->uri->segment(4);
- $yyyymm = $this->uri->segment(6);
- $date_arr = $this->_date_start_end($yyyymm);
- $date_start = $date_arr[0];
- $date_end = $date_arr[1];
- $posts = $this->model->get_blogrchive_posts($class_id, $date_start, $date_end);
- if (empty($posts)) {
- $data = 'No posts';
- } else {
- $i = 0;
- foreach ($posts as $value) {
- $data[$i] = $this->_blogPost($class_id, $value['id']);
- $i++;
- }
- }
- break;
- case '10':
- # classes/<class-id>/blogArchive/<yyyymm>/posts/<post-id>
- $post_id = $this->uri->segment(8);
- $this->load->helper('date');
- $class_id = $this->uri->segment(4);
- $yyyymm = $this->uri->segment(6);
- $date_arr = $this->_date_start_end($yyyymm);
- $date_start = $date_arr[0];
- $date_end = $date_arr[1];
- $posts = $this->model->get_blogrchive_post($post_id, $class_id, $date_start, $date_end);
- if (empty($posts)) {
- $data = 'No posts';
- } else {
- $i = 0;
- foreach ($posts as $value) {
- $data[$i] = $this->_blogPost($class_id, $value['id']);
- $i++;
- }
- }
- break;
- case '11':
- # classes/<class-id>/teachers/
- $data = array();
- break;
- case '12':
- # classes/<class-id>/teachers/<teachers-id>
- $teachers_id = $this->uri->segment(6);
- $data = $this->_publisher($teachers_id);
- break;
- }
- return $data;
- }
- function _blogPost($class_id, $post_id)
- {
- $blogPost = $this->model->get_post_id($class_id, $post_id);
- if (empty($blogPost)) {
- $data = 'No post';
- } else {
- $publisher = $this->_publisher($blogPost->publisher_id);
- if ($blogPost->corrector_id != NULL) {
- $corrector = $this->_publisher($blogPost->corrector_id);
- } else {
- $corrector = NULL;
- }
- $data = array(
- 'id' => $blogPost->id,
- 'title' => $blogPost->title,
- 'contents' => $blogPost->contents,
- 'isPublished' => $blogPost->is_published,
- 'publicationTS' => $blogPost->publication_ts,
- 'publisher' => $publisher,
- 'correctionTS' => $blogPost->correction_ts,
- 'corrector' => $corrector);
- }
- return $data;
- }
- function _publisher($publisher_id)
- {
- $get_publisher = $this->model->get_publisher($publisher_id);
- if (empty($get_publisher)) {
- $publisher = 'Not found';
- } else {
- $publisher = array(
- 'id' => $get_publisher->id,
- 'firstName' => $get_publisher->first_name,
- 'lastName' => $get_publisher->last_name,
- 'date_of_birth' => $get_publisher->date_of_birth,
- 'gender' => $get_publisher->gender,
- 'type' =>$get_publisher->type);
- }
- return $publisher;
- }
- function _delete($router)
- {
- switch ($router) {
- case '5':
- # classes/<class-id>/blogPosts/<post-id>/media/<media-file-name>
- $media = $this->uri->segment(8);
- if ($this->model->delete_blog_media($media)) {
- $data = "Delete";
- } else {
- $data = "Error";
- }
- break;
- case '6':
- # classes/<class-id>/blogPosts/<post-id>
- $post_id = $this->uri->segment(6);
- $data = $this->_delete_post($post_id);
- break;
- case '10':
- # classes/<class-id>/blogArchive/<yyyymm>/posts/<post-id>
- $post_id = $this->uri->segment(8);
- $data = $this->_delete_post($post_id);
- break;
- }
- return $data;
- }
- function _delete_post($id)
- {
- if ($this->model->delete_blog_post($id)) {
- $data = "Delete";
- } else {
- $data = "Error";
- }
- return $data;
- }
- function _post($router)
- {
- switch ($router) {
- case '4':
- # classes/<class-id>/blogPosts/
- $class_id = (int)$this->uri->segment(4);
- $title = $this->post('title');
- $contents = $this->post('contents')? $this->post('contents') : NULL;
- $publisher_id = $this->post('publisher_id');
- $corrector_id = $this->post('corrector_id')? $this->post('corrector_id') : NULL;
- $is_published = $this->post('isPublished');
- $publication_ts = date('Y-m-d H:i:s');
- $correction_ts = $this->post('correction_ts')? $this->post('correction_ts') : NULL;
- if ($this->model->save_blog_post($class_id, $title, $contents, $publisher_id, $corrector_id, $is_published, $publication_ts, $correction_ts)) {
- $data = 'Saved';
- } else {
- $data = 'Error';
- }
- break;
- case '0':
- # classes/<class-id>/blogPosts/<post-id>/media/
- $post_id = $this->uri->segment(6);
- $data = $this->_upload($this->post('full_path'), $this->post('mime_type'), $this->post('size'), $this->post('file_name'), $post_id);
- break;
- case '5':
- # classes/<class-id>/blogPosts/<post-id>/media/<media-file-name>
- $post_id = $this->uri->segment(6);
- $media = $this->uri->segment(8);
- $data = $this->_upload($this->post('full_path'), $this->post('mime_type'), $this->post('size'), $this->post('file_name'), $post_id, $media);
- break;
- }
- return $data;
- }
- function _put($router)
- {
- switch ($router) {
- case '6':
- # classes/<class-id>/blogPosts/<post-id>
- $post_id = $this->uri->segment(6);
- $class_id = $this->uri->segment(4);
- $data = $this->_update_blog_post($post_id, $class_id);
- break;
- case '10':
- # classes/<class-id>/blogArchive/<yyyymm>/posts/<post-id>
- $post_id = $this->uri->segment(8);
- $class_id = $this->uri->segment(4);
- $data = $this->_update_blog_post($post_id, $class_id);
- break;
- }
- return $data;
- }
- function _update_blog_post($post_id, $class_id)
- {
- $title = $this->put('title');
- $contents = $this->put('contents');
- $publisher_id = $this->put('publisher_id');
- $corrector_id = $this->put('corrector_id');
- $is_published = $this->put('isPublished');
- $publication_ts = date('Y-m-d H:i:s');
- $correction_ts = $this->put('correction_ts');
- $object = array('class_id' => $class_id, 'publication_ts' => $publication_ts);
- if ($title != FALSE) {
- $object['title'] = $title;
- }
- if ($publisher_id != FALSE) {
- $object['publisher_id'] = $publisher_id;
- }
- if ($corrector_id != FALSE) {
- $object['corrector_id'] = $corrector_id;
- }
- if ($is_published != FALSE) {
- $object['is_published'] = $is_published;
- }
- if ($correction_ts != FALSE) {
- $object['correction_ts'] = $correction_ts;
- }
- if ($contents != FALSE) {
- $object['contents'] = $contents;
- }
- if ($this->model->update_blog_post($post_id, $object)) {
- $data = 'Updated';
- } else {
- $data = 'Error';
- }
- return $data;
- }
- function _upload($full_path, $mime_type, $size, $file_name, $post_id, $media=null)
- {
- $contents = file_get_contents($full_path);
- $insert_data = array(
- 'post_id' => $post_id,
- 'size' => $size,
- 'mime_type' => $mime_type,
- 'contents' => $contents
- );
- if ($media == NULL) {
- $insert_data['file_name'] = $file_name;
- if ($this->model->save_media_file($insert_data)) {
- $data = 'Saved';
- } else {
- $data = 'Error';
- }
- } else {
- $insert_data['file_name'] = $media;
- if ($this->model->update_media_file($insert_data, $media)) {
- $data = 'Updated';
- } else {
- $data = 'Error';
- }
- }
- @unlink($full_path);
- return $data;
- }
- function _convert_year($value)
- {
- $data = str_split(str_replace("-", "", $value), 6);
- return $data[0];
- }
- function _date_start_end($value)
- {
- $this->load->helper('date');
- $date_arr = str_split($value, 4);
- $last_day = days_in_month($date_arr[1], $date_arr[0]);
- $date_start = $date_arr[0] . '-' . $date_arr[1] . '-01';
- $date_end = $date_arr[0] . '-' . $date_arr[1] .'-'. $last_day;
- return $data = array($date_start, $date_end);
- }
- }
- /* End of file api.php */
- /* Location: ./application/controllers/api.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement