Advertisement
kotvalera83

API codeigniter controllers

Jan 13th, 2014
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 17.33 KB | None | 0 0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. require(APPPATH."/libraries/REST_Controller.php");
  4.  
  5. class Blog extends REST_Controller {
  6.  
  7.     function __construct()
  8.     {
  9.         parent::__construct();
  10.         $this->load->model('api_model', 'model');
  11.     }
  12.    
  13.     function grades_get()  
  14.     {  
  15.         $grade_id = (int)$this->uri->segment(4, 0);
  16.         if ($grade_id === 0) {
  17.             $data = array();
  18.         } else {
  19.             $data = $this->model->get_grade($grade_id);
  20.         }  
  21.        
  22.         $this->response($data);    
  23.     }
  24.  
  25.     function classes_get()  
  26.     {  
  27.         $router = $this->_routers();
  28.         $data = $this->_get_info($router);
  29.         $this->response($data);    
  30.     }  
  31.  
  32.       function classes_put()  
  33.     {  
  34.         $router = $this->_routers();
  35.         $allow = $this->_allow_put($router);
  36.         if (!$allow) {
  37.             $data = array();
  38.         } else {
  39.             $data = $this->_put($router);
  40.         }  
  41.        
  42.         $this->response($data);
  43.     }  
  44.  
  45.     function classes_post()  
  46.     {  
  47.         $router = $this->_routers();
  48.         $allow = $this->_allow_post($router);
  49.         if (!$allow) {
  50.             $data = array();
  51.         } else {
  52.             $data = $this->_post($router);
  53.         }
  54.                
  55.         $this->response($data);
  56.     }  
  57.  
  58.     function classes_delete()  
  59.     {  
  60.         $router = $this->_routers();
  61.         $allow = $this->_allow_delete($router);
  62.         if (!$allow) {
  63.             $data = array();
  64.         } else {
  65.             $data = $this->_delete($router);
  66.         }
  67.  
  68.         $this->response($data);
  69.     }
  70.  
  71.     function _allow_post($router)
  72.     {
  73.         $allow = array(4, 5, 0);
  74.         return in_array($router, $allow);
  75.     }
  76.  
  77.     function _allow_put($router)
  78.     {
  79.         $allow = array(6, 10);
  80.         return in_array($router, $allow);
  81.     }
  82.  
  83.     function _allow_delete($router)
  84.     {
  85.         $allow = array(5, 6, 10);
  86.         return in_array($router, $allow);
  87.     }
  88.  
  89.     function _routers()
  90.     {
  91.         $class_id = (int)$this->uri->segment(4, 0);
  92.         $search_segment = $this->uri->segment(5);
  93.         $array = array(0 => 'grade', 1 => 'blogPosts', 2 => 'blogArchive', 3 => 'teachers');
  94.         $key = array_search( $search_segment, $array);
  95.         if ($key === FALSE) {
  96.             if ($class_id === 0) {
  97.                 # classes/
  98.                 return $routers = 1;
  99.             }else{
  100.                 # classes/<class-id>
  101.                 return $routers = 2;
  102.             }
  103.  
  104.         }elseif ($key == 0) {
  105.             # classes/<class-id>/grade
  106.             return $routers = 3;
  107.  
  108.         }elseif ($key == 1) {
  109.             $post_id = (int)$this->uri->segment(6, 0);
  110.             $media_string = $this->uri->segment(7, 0);
  111.             $media = $this->uri->segment(8, 0);
  112.             if ($post_id === 0) {
  113.                 # classes/<class-id>/blogPosts
  114.                 return $routers = 4;
  115.             }
  116.             elseif (is_string($media)) {
  117.                 # classes/<class-id>/blogPosts/<post-id>/media/<media-file-name>
  118.                 return $routers = 5;
  119.             }elseif ($media_string === 'media') {
  120.                 # classes/<class-id>/blogPosts/<post-id>/media/
  121.                 return $routers = 0;
  122.             }elseif ($media_string === 0) {
  123.                 # classes/<class-id>/blogPosts/<post-id>
  124.                 return $routers = 6;
  125.             }
  126.  
  127.         }elseif ($key == 2) {
  128.             $yyyymm = $this->uri->segment(6, 0);
  129.             $posts = $this->uri->segment(7);
  130.             $post_id = (int)$this->uri->segment(8, 0);
  131.             if ($yyyymm === 0) {
  132.                 # classes/<class-id>/blogArchive
  133.                 return $routers = 7;
  134.             }
  135.             elseif ($yyyymm === 'all') {
  136.                 # classes/<class-id>/blogArchive/all
  137.                 return $routers = 13;
  138.             }
  139.             elseif (!$posts) {
  140.                 # classes/<class-id>/blogArchive/<yyyymm>
  141.                 return $routers = 8;
  142.             }
  143.             elseif ($post_id === 0) {
  144.                 # classes/<class-id>/blogArchive/<yyyymm>/posts
  145.                 return $routers = 9;
  146.             }
  147.             else{
  148.                 # classes/<class-id>/blogArchive/<yyyymm>/posts/<post-id>
  149.                 return $routers = 10;
  150.             }
  151.         }elseif ($key == 3) {
  152.             $teachers_id = (int)$this->uri->segment(6, 0);
  153.             if ($teachers_id == 0) {
  154.                 # classes/345/teachers/
  155.                 return $routers = 11;
  156.             } else {
  157.                 # classes/345/teachers/<teachers-id>
  158.                 return $routers = 12;
  159.             }
  160.            
  161.         }
  162.     }
  163.  
  164.     function _get_info($router)
  165.     {
  166.         switch ($router) {
  167.             case '1':
  168.                 # classes/
  169.                 $classes_all = $this->model->get_classes_all();
  170.                 if (empty($classes_all)) {
  171.                     $data = 'No classes';
  172.                 } else {   
  173.                     $i = 0;
  174.                     foreach ($classes_all as $value) {
  175.                         $grade = $this->model->get_grade($value['grade_id']);
  176.                         $classes = array(
  177.                             'id' => $value['id'],
  178.                             'name' => $value['name'],
  179.                             'year' => $value['year'],
  180.                             'note' => $value['note'],
  181.                             'type' => $value['type'],
  182.                             'grade' => $grade);
  183.                         $data[$i] = $classes;
  184.                         $i++;
  185.                     }
  186.                 }
  187.                 break;
  188.  
  189.             case '2':
  190.                 # classes/<class-id>
  191.                 $id = $this->uri->segment(4);
  192.                 $classes = $this->model->get_classes($id);
  193.                 if (empty($classes)) {
  194.                     $data = 'No classes';
  195.                 } else {
  196.                     $grade = $this->model->get_grade($classes->grade_id);
  197.                     $data = array(
  198.                         'id' => $classes->id,
  199.                         'name' => $classes->name,
  200.                         'year' => $classes->year,
  201.                         'note' => $classes->note,
  202.                         'type' => $classes->type,
  203.                         'grade' => $grade);
  204.                 }
  205.                 break;
  206.  
  207.             case '3':
  208.                 # classes/<class-id>/grade
  209.                $class_id = $this->uri->segment(4);
  210.                 $grade_class = $this->model->get_grade_class($class_id);
  211.                 if (empty($grade_class)) {
  212.                     $data = 'No grade';
  213.                 } else {
  214.                     $data = $grade_class;
  215.                 }
  216.                 break;
  217.  
  218.             case '4':
  219.                 # classes/<class-id>/blogPosts
  220.                 $class_id = $this->uri->segment(4);
  221.                 $blogPosts = $this->model->get_posts($class_id);
  222.                 if (empty($blogPosts)) {
  223.                     $data = 'No posts';
  224.                 } else {
  225.                     $i = 0;
  226.                     foreach ($blogPosts as $value) {
  227.                         $publisher = $this->_publisher($value['publisher_id']);
  228.                         if ($value['corrector_id'] != NULL) {
  229.                             $corrector = $this->_publisher($value['corrector_id']);
  230.                         } else {
  231.                             $corrector = NULL;
  232.                         }
  233.                         $blogPost = array(
  234.                             'id' => $value['id'],
  235.                             'title' => $value['title'],
  236.                             'contents' => $value['contents'],
  237.                             'isPublished' => $value['is_published'],
  238.                             'publicationTS' => $value['publication_ts'],
  239.                             'publisher' => $publisher,
  240.                             'correctionTS' => $value['correction_ts'],
  241.                             'corrector' => $corrector);
  242.                         $data[$i] = $blogPost;
  243.                         $i++;
  244.                     }
  245.                 }
  246.                
  247.                 break;
  248.  
  249.             case '5':
  250.                 # classes/<class-id>/blogPosts/<post-id>/media/<media-file-name>
  251.                 $media = $this->uri->segment(8);
  252.                 $get_media = $this->model->get_media($media);
  253.                 if (empty($get_media)) {
  254.                     $data = 'No media-file';
  255.                 } else {
  256.                     $get_media->url = site_url().'media/blogPosts/'.$get_media->id;
  257.                     $data = $get_media;
  258.                 }
  259.                                                
  260.                 break;
  261.  
  262.             case '0':
  263.                 # classes/<class-id>/blogPosts/<post-id>/media/
  264.                 $post_id = $this->uri->segment(6);
  265.                 $get_media = $this->model->get_media_all($post_id);
  266.                 if (empty($get_media)) {
  267.                     $data = 'No media-file';
  268.                 } else {
  269.                     $i = 0;
  270.                     foreach ($get_media as $value) {
  271.                         $url = site_url().'media/blogPosts/'.$value['id'];
  272.                         $media = array(
  273.                             'id' => $value['id'],
  274.                             'size' => $value['size'],
  275.                             'mime_type' => $value['mime_type'],
  276.                             'file_name' => $value['file_name'],
  277.                             'url' => $url);
  278.                         $data[$i] = $media;
  279.                         $i++;
  280.                     }
  281.                 }
  282.                                                
  283.                 break;
  284.  
  285.             case '6':
  286.                 # classes/<class-id>/blogPosts/<post-id>
  287.                 $post_id = $this->uri->segment(6);
  288.                 $class_id = $this->uri->segment(4);
  289.                 $data = $this->_blogPost($class_id, $post_id);
  290.                 break;
  291.  
  292.             case '7':
  293.                 # classes/<class-id>/blogArchive               
  294.                 $class_id = $this->uri->segment(4);
  295.                 $year = $this->get('year');
  296.                 $this->load->helper('date');
  297.                 $i = 0;
  298.                 $month = 1;
  299.                 while ( $month <= 12) {
  300.                     $min_date = $year.'-'.$month.'-01';
  301.                     $max_date = $year.'-'.$month.'-'.days_in_month($month, $year);;
  302.                     $count = $this->model->get_blogArchive($class_id, $min_date, $max_date);
  303.                     if (strlen($month) == 1) {
  304.                         $yyyymm = $year.'0'.$month;
  305.                     }else{
  306.                         $yyyymm = $year.$month;
  307.                     }
  308.                    
  309.                     if ($count != 0) {
  310.                         $data[$i] = array(
  311.                         'id' => date("F", strtotime($min_date)),
  312.                         'count' => $count,
  313.                         'yyyymm' => $yyyymm);
  314.                         $i++;
  315.                     }
  316.                     $month++;  
  317.                    
  318.                 }
  319.                
  320.                 break;
  321.  
  322.             case '13':
  323.                 # classes/<class-id>/blogArchive/all               
  324.                 $class_id = $this->uri->segment(4);
  325.                 $max_min_year = $this->model->get_max_min($class_id);
  326.                 if (is_null($max_min_year->min_year) && is_null($max_min_year->max_year) ) {
  327.                     $data = 'No blog Archive';
  328.                 } else {
  329.                     $max_year = substr((int)$this->_convert_year($max_min_year->max_year), 0, 4);
  330.                     $min_year = substr((int)$this->_convert_year($max_min_year->min_year), 0, 4);
  331.                     $i = 0;
  332.                     while ( $min_year <= $max_year) {
  333.                         $min_date = $min_year.'-01-01';
  334.                         $max_date = $min_year.'-12-31';
  335.                         $count = $this->model->get_blogArchive($class_id, $min_date, $max_date);
  336.                         if ($count != 0) {
  337.                             $data[$i] = array(
  338.                             'id' => $min_year,
  339.                             'count' => $count);
  340.                             $i++;
  341.                         }                          
  342.                        
  343.                         $min_year++;                       
  344.                     }
  345.                     arsort($data);
  346.                 }
  347.                
  348.                 break;
  349.  
  350.             case '8':
  351.                 # classes/<class-id>/blogArchive/<yyyymm>
  352.                 $this->load->helper('date');
  353.                 $class_id = $this->uri->segment(4);
  354.                 $yyyymm = $this->uri->segment(6);
  355.                 $date_arr = $this->_date_start_end($yyyymm);
  356.                 $date_start = $date_arr[0];
  357.                 $date_end = $date_arr[1];
  358.                 $blogArchive = $this->model->get_blogArchive($class_id, $date_start, $date_end);
  359.                 if (empty($blogArchive)) {
  360.                     $data = 'No posts';
  361.                 } else {
  362.                     $data = array(
  363.                             'id' => $yyyymm,
  364.                             'count' => $blogArchive);
  365.                 }
  366.                 break;
  367.  
  368.             case '9':
  369.                 # classes/<class-id>/blogArchive/<yyyymm>/posts
  370.                 $this->load->helper('date');
  371.                 $class_id = $this->uri->segment(4);
  372.                 $yyyymm = $this->uri->segment(6);
  373.                 $date_arr = $this->_date_start_end($yyyymm);
  374.                 $date_start = $date_arr[0];
  375.                 $date_end = $date_arr[1];
  376.  
  377.                 $posts = $this->model->get_blogrchive_posts($class_id, $date_start, $date_end);
  378.                 if (empty($posts)) {
  379.                     $data = 'No posts';
  380.                 } else {               
  381.                     $i = 0;
  382.                     foreach ($posts as $value) {
  383.                         $data[$i] = $this->_blogPost($class_id, $value['id']);
  384.                         $i++;
  385.                     }
  386.                 }
  387.                 break;
  388.  
  389.             case '10':
  390.                 # classes/<class-id>/blogArchive/<yyyymm>/posts/<post-id>
  391.                 $post_id = $this->uri->segment(8);
  392.                 $this->load->helper('date');
  393.                 $class_id = $this->uri->segment(4);
  394.                 $yyyymm = $this->uri->segment(6);
  395.                 $date_arr = $this->_date_start_end($yyyymm);
  396.                 $date_start = $date_arr[0];
  397.                 $date_end = $date_arr[1];
  398.  
  399.                 $posts = $this->model->get_blogrchive_post($post_id, $class_id, $date_start, $date_end);
  400.                 if (empty($posts)) {
  401.                     $data = 'No posts';
  402.                 } else {               
  403.                     $i = 0;
  404.                     foreach ($posts as $value) {
  405.                         $data[$i] = $this->_blogPost($class_id, $value['id']);
  406.                         $i++;
  407.                     }
  408.                 }
  409.                 break;
  410.             case '11':
  411.                 # classes/<class-id>/teachers/
  412.                 $data = array();
  413.                 break;
  414.             case '12':
  415.                 # classes/<class-id>/teachers/<teachers-id>
  416.                 $teachers_id = $this->uri->segment(6);
  417.                 $data = $this->_publisher($teachers_id);
  418.                 break;
  419.         }
  420.         return $data;
  421.     }
  422.  
  423.     function _blogPost($class_id, $post_id)
  424.     {
  425.         $blogPost = $this->model->get_post_id($class_id, $post_id);
  426.         if (empty($blogPost)) {
  427.             $data = 'No post';
  428.         } else {
  429.             $publisher = $this->_publisher($blogPost->publisher_id);
  430.             if ($blogPost->corrector_id != NULL) {
  431.                 $corrector = $this->_publisher($blogPost->corrector_id);
  432.             } else {
  433.                 $corrector = NULL;
  434.             }
  435.             $data = array(
  436.                 'id' => $blogPost->id,
  437.                 'title' => $blogPost->title,
  438.                 'contents' => $blogPost->contents,
  439.                 'isPublished' => $blogPost->is_published,
  440.                 'publicationTS' => $blogPost->publication_ts,
  441.                 'publisher' => $publisher,
  442.                 'correctionTS' => $blogPost->correction_ts,
  443.                 'corrector' => $corrector);
  444.         }
  445.        
  446.        
  447.         return $data;
  448.     }
  449.  
  450.     function _publisher($publisher_id)
  451.     {
  452.         $get_publisher = $this->model->get_publisher($publisher_id);
  453.         if (empty($get_publisher)) {
  454.             $publisher = 'Not found';
  455.         } else {
  456.             $publisher = array(
  457.             'id' => $get_publisher->id,
  458.             'firstName' => $get_publisher->first_name,
  459.             'lastName' => $get_publisher->last_name,
  460.             'date_of_birth' => $get_publisher->date_of_birth,
  461.             'gender' => $get_publisher->gender,
  462.             'type' =>$get_publisher->type);
  463.         }
  464.        
  465.        
  466.         return $publisher;
  467.     }
  468.  
  469.     function _delete($router)
  470.     {
  471.         switch ($router) {
  472.             case '5':
  473.                 # classes/<class-id>/blogPosts/<post-id>/media/<media-file-name>
  474.                 $media = $this->uri->segment(8);
  475.                 if ($this->model->delete_blog_media($media)) {
  476.                     $data = "Delete";
  477.                 } else {
  478.                     $data = "Error";
  479.                 }
  480.                
  481.                 break;
  482.  
  483.             case '6':
  484.                 # classes/<class-id>/blogPosts/<post-id>
  485.                 $post_id = $this->uri->segment(6);
  486.                 $data = $this->_delete_post($post_id);
  487.                 break;
  488.  
  489.             case '10':
  490.                 # classes/<class-id>/blogArchive/<yyyymm>/posts/<post-id>
  491.                 $post_id = $this->uri->segment(8);
  492.                 $data = $this->_delete_post($post_id);
  493.                 break;
  494.         }
  495.         return $data;
  496.     }
  497.  
  498.     function _delete_post($id)
  499.     {
  500.         if ($this->model->delete_blog_post($id)) {
  501.             $data = "Delete";
  502.         } else {
  503.             $data = "Error";
  504.         }
  505.         return $data;
  506.     }
  507.  
  508.     function _post($router)
  509.     {
  510.         switch ($router) {
  511.             case '4':
  512.                 # classes/<class-id>/blogPosts/
  513.                 $class_id = (int)$this->uri->segment(4);
  514.                 $title = $this->post('title');
  515.                 $contents = $this->post('contents')? $this->post('contents') : NULL;
  516.                 $publisher_id = $this->post('publisher_id');
  517.                 $corrector_id = $this->post('corrector_id')? $this->post('corrector_id') : NULL;
  518.                 $is_published = $this->post('isPublished');
  519.                 $publication_ts = date('Y-m-d H:i:s');
  520.                 $correction_ts = $this->post('correction_ts')? $this->post('correction_ts') : NULL;
  521.                 if ($this->model->save_blog_post($class_id, $title, $contents, $publisher_id, $corrector_id, $is_published, $publication_ts, $correction_ts)) {
  522.                     $data = 'Saved';
  523.                 } else {
  524.                     $data = 'Error';
  525.                 }
  526.                 break;
  527.  
  528.             case '0':
  529.                 # classes/<class-id>/blogPosts/<post-id>/media/
  530.                 $post_id = $this->uri->segment(6);
  531.                 $data = $this->_upload($this->post('full_path'), $this->post('mime_type'), $this->post('size'), $this->post('file_name'), $post_id);
  532.                
  533.                 break;
  534.             case '5':
  535.                 # classes/<class-id>/blogPosts/<post-id>/media/<media-file-name>               
  536.                 $post_id = $this->uri->segment(6);
  537.                 $media = $this->uri->segment(8);
  538.                 $data = $this->_upload($this->post('full_path'), $this->post('mime_type'), $this->post('size'), $this->post('file_name'), $post_id, $media);
  539.                 break;
  540.  
  541.         }
  542.         return $data;
  543.     }
  544.  
  545.     function _put($router)
  546.     {
  547.         switch ($router) {
  548.             case '6':
  549.                 # classes/<class-id>/blogPosts/<post-id>
  550.                 $post_id = $this->uri->segment(6);
  551.                 $class_id = $this->uri->segment(4);
  552.                 $data = $this->_update_blog_post($post_id, $class_id);
  553.                 break;
  554.  
  555.             case '10':
  556.                 # classes/<class-id>/blogArchive/<yyyymm>/posts/<post-id>
  557.                 $post_id = $this->uri->segment(8);
  558.                 $class_id = $this->uri->segment(4);
  559.                 $data = $this->_update_blog_post($post_id, $class_id);
  560.                 break;
  561.         }
  562.         return $data;
  563.     }
  564.  
  565.     function _update_blog_post($post_id, $class_id)
  566.     {
  567.         $title = $this->put('title');
  568.         $contents = $this->put('contents');
  569.         $publisher_id = $this->put('publisher_id');
  570.         $corrector_id = $this->put('corrector_id');
  571.         $is_published = $this->put('isPublished');
  572.         $publication_ts = date('Y-m-d H:i:s');
  573.         $correction_ts = $this->put('correction_ts');
  574.         $object = array('class_id' => $class_id, 'publication_ts' => $publication_ts);
  575.         if ($title != FALSE) {
  576.             $object['title'] = $title;
  577.         }
  578.         if ($publisher_id != FALSE) {
  579.             $object['publisher_id'] = $publisher_id;
  580.         }
  581.         if ($corrector_id != FALSE) {
  582.             $object['corrector_id'] = $corrector_id;
  583.         }
  584.         if ($is_published != FALSE) {
  585.             $object['is_published'] = $is_published;
  586.         }
  587.         if ($correction_ts != FALSE) {
  588.             $object['correction_ts'] = $correction_ts;
  589.         }
  590.         if ($contents != FALSE) {
  591.             $object['contents'] = $contents;
  592.         }
  593.         if ($this->model->update_blog_post($post_id, $object)) {
  594.             $data = 'Updated';
  595.         } else {
  596.             $data = 'Error';
  597.         }
  598.         return $data;
  599.     }
  600.  
  601.     function _upload($full_path, $mime_type, $size, $file_name, $post_id, $media=null)
  602.     {
  603.             $contents = file_get_contents($full_path);
  604.             $insert_data = array(
  605.                     'post_id' => $post_id,
  606.                     'size' => $size,
  607.                     'mime_type' => $mime_type,                 
  608.                     'contents' => $contents
  609.                 );
  610.                        
  611.             if ($media == NULL) {
  612.                 $insert_data['file_name'] = $file_name;
  613.                 if ($this->model->save_media_file($insert_data)) {
  614.                     $data = 'Saved';
  615.                 } else {
  616.                     $data = 'Error';
  617.                 }
  618.             } else {
  619.                 $insert_data['file_name'] = $media;
  620.                 if ($this->model->update_media_file($insert_data, $media)) {
  621.                     $data = 'Updated';
  622.                 } else {
  623.                     $data = 'Error';
  624.                 }
  625.             }
  626.             @unlink($full_path);
  627.            
  628.         return $data;
  629.     }
  630.  
  631.     function _convert_year($value)
  632.     {
  633.         $data = str_split(str_replace("-", "", $value), 6);
  634.         return $data[0];
  635.     }
  636.  
  637.     function _date_start_end($value)
  638.     {
  639.         $this->load->helper('date');
  640.         $date_arr = str_split($value, 4);
  641.         $last_day = days_in_month($date_arr[1], $date_arr[0]);
  642.         $date_start = $date_arr[0] . '-' . $date_arr[1] . '-01';
  643.         $date_end = $date_arr[0] . '-' . $date_arr[1] .'-'. $last_day;
  644.         return $data = array($date_start, $date_end);
  645.     }
  646.  
  647. }
  648.  
  649. /* End of file api.php */
  650. /* Location: ./application/controllers/api.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement