Wednesday, September 18, 2013

Creating A Simple PHP MVC Framework Part 6B, First Application- Youtube API (smarty version)

Written by Lorenzo D. Alipio (open source PHP developer) 

This is the part 6B of our tutorial series. In this segment, we will be creating these controller files. These are the files we were not able to create in Part 6A.

1.MostviewedController.php
2.PlayController.php
3.TopRatedController.php

The Views we need to create
1.ViewClass.php

Filename: controller/MostviewedController.php
<?php

/*
* 
* Copyright Lorenzo D. Alipio 
* 
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
* 
* MostviewedController.php
*/

class MostviewedController extends Controller {

 public $q = "funnies";
 public $index = '1';
 public $orderby = "published";
 public $i = 48;
 public $count ='';
 function __construct() {
  
  parent::__construct();
  Use_Class::autoload(YOUTUBE);
  
 }

public function index(){

 
 if(isset($_GET['q'])){
 $q = $_GET['q'];
    $q = preg_replace('/[[:space:]]+/', '/', trim($q));
    $q = str_replace('/', '+', $q);
 $index = $_GET['start-index'];
 $orderby = $_GET['orderby'];
 $i = 25;
}
else{
  $q = "Metallica";
  $index = "1";
  $orderby = "published";
  $i = 25;
}
$object = new YoutubeVideo($q,$index,$orderby,$i);
 $video = array(
     'count'=> $object->getCount(),
     'video'=> $object->getVideo(),
     'sitepath'=>SITEPATH,
     'cat' => $q,
     'pagination'=>$object->pagenate('mostviewed.php')
  );
 
 return $this->view->set_content($video,'latest.tpl');
 
}


}  





Filename : controller/PlayController.php
<?php

/*
* 
* Copyright Lorenzo D. Alipio 
* 
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
* 
* PlayController.php
*/
class PlayController extends Controller {
 
 public $v_id;
 public $title;
 public $description ;
 public $q = "funnies";
 public $index = '1';
 public $orderby = "viewCount";
 public $i = 10;
 public $count ='';

 public function __construct() {

  parent::__construct();
  Use_Class::autoload(YOUTUBE);
  

  if(isset($_GET['vid'])){
   $this->v_id = filter_var($_GET['vid'], FILTER_SANITIZE_STRIPPED);
   $this->title = filter_var($_GET['title'], FILTER_SANITIZE_STRIPPED);
   $this->category = filter_var($_GET['cat'], FILTER_SANITIZE_STRIPPED);
   $this->q = $this->category;
   // set video data feed URL
         

        
    
   
  }

 
 }

 public function index(){
  
  $object = new YoutubeVideo($this->q,$this->index,$this->orderby,$this->i);
  $relatedObject = new YoutubeVideo($this->q, 2 ,$this->orderby,15);
  
  $video = array(

      'v_id' => $this->v_id,
      'title' => $this->title,
      'video'=> $object->getVideo(),
      'related'=>$relatedObject->getVideo(),
      
      'sitepath' => SITEPATH,
      'cat' =>$this->category
  );

  return $this->view->set_content($video,'play.tpl');

 }


}




filename: TopratedController.php
<?php
/*
 * Copyright Lorenzo D. Alipio 
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * File: application/controller/TopratedController.php
 */

class TopratedController extends Controller {

 public $q = "toprated";
 public $index = '1';
 public $orderby = "published";
 public $i = 48;
 public $count ='';
 function __construct() {
  
  parent::__construct();
  Use_Class::autoload(YOUTUBE);
  
 }

public function index(){

 //$object = Ini_object::instance('YoutubeVideo');
 if(isset($_GET['q'])){
 $q = $_GET['q'];
    $q = preg_replace('/[[:space:]]+/', '/', trim($q));
    $q = str_replace('/', '+', $q);
 $index = $_GET['start-index'];
 $orderby = $_GET['orderby'];
 $i = 25;
}
else{
  $q = "Aerosmith";
  $index = "1";
  $orderby = "published";
  $i = 25;
}

$object = new YoutubeVideo($q,$index,$orderby,$i);
 
 $video = array(
     'count'=> $object->getCount(),
     'video'=> $object->getVideo(),
     'sitepath'=>SITEPATH,
     'cat' => $q,
     'pagination'=>$object->pagenate('toprated.php')
  );
 
 return $this->view->set_content($video,'latest.tpl');
 
}


}  





filename: view/ViewClass.php
<?php
/*
 * Copyright Lorenzo D. Alipio 
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * File: application/view/ViewClass.php
 */
Class View{

public function  __construct(){
 
 if(TPL === 'Smarty'){
 Ini_object::instance('Smarty');
 $this->tpl = Ini_object::instance('Smarty');
 $this->tpl->left_delimiter = '{% ';
  $this->tpl->right_delimiter = ' %}';
 $this->tpl->template_dir = ( THEME_DIR );
  $this->tpl->compile_dir  = ( THEME_COMPILE );
  $this->tpl->caching = 0;
  $this->tpl->setCompileCheck(true);
  $this->tpl->cache_dir    = ( THEME_CACHE );
  $this->tpl->config_dir   = ( THEME_CONFIG ); 
}

else if(TPL ==='Twig'){
  Use_Class::useFile(TWIG_LOADER);
  Twig_Autoloader::register();
   ## define our template directory location
  $loader = new Twig_Loader_Filesystem('templates');

  ##  initialize Twig environment with cache enabled
  $twig = new Twig_Environment($loader, array(
    'cache'       => TWIG_CACHE,
    'auto_reload' => true
  ));
}

}
/*
* TO DO integrate TWIG 
*
*/

public function set_content($content=array(),$templateName = null){
 
 

  $this->tpl->assign('content', $content);
  return $this->tpl->display(THEME .$templateName);

 
 
 }
 
 
} 



Notice, how the ViewClass.php was coded, we are setting our own delimiters for our smarty template engine. We will be using {%  and  %} because I am trying to build this framework to be able to use twig template engine. This conclude the Part 6B. Next time we will be writing the parent template file, the child template files. Hopefully, we can test our simple MVC implementing youtube API v2.

No comments:

Post a Comment