Advertisement
Tyler_Elric

adopt.php

Oct 1st, 2012
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.71 KB | None | 0 0
  1. <?php
  2. require 'dbConnect.php';
  3. require 'Slim/Slim.php';
  4. \Slim\Slim::registerAutoloader();
  5.  
  6. $con = Connect('Adoptables');
  7.  
  8. class JSONTemplate extends \Slim\View
  9. {
  10.     public function render($template)
  11.     {
  12.         if($template=='json')
  13.             return json_encode($this->data['output']);
  14.     }
  15. }
  16.  
  17. function getUser(){return 0;}//Do this later.
  18.  
  19. $app = new \Slim\Slim(array(
  20.     'view' => new JSONTemplate()
  21. ));
  22.  
  23. $app->get('/adoptable(/:id)', function ($adoptID=null) use($app,$con){
  24.     $lang = isset($_GET['lang'])?$_GET['lang']:'0';
  25.     $query = 'Select
  26.         adopt.id as "ID",
  27.         adopt.userId as "userID",
  28.         (select species_names.name from species_names where species_names.speciesId=adopt.speciesId AND species_names.languageId='.$lang.') as "species",
  29.         coalesce(
  30.          adopt.name,
  31.          (select species_names.name from species_names where species_names.speciesId=adopt.speciesId AND species_names.languageId='.$lang.' )
  32.         ) as "name",
  33.         adopt.gender as "gender",
  34.         adopt.level as "level",
  35.         adopt.clicks as "clicks",
  36.         adopt.adopted as "creation"
  37.      from adoptables as adopt';
  38.     if($adoptID!==null){
  39.         $query = $query . "\n where adopt.id=$adoptID limit 0,1";
  40.     }
  41.     $num_results = 0;
  42.     $query_result = mysql_query($query,$con) or die(mysql_error());
  43.     $return = array();
  44.     while($row = mysql_fetch_array($query_result))
  45.     {
  46.         $num_results+=1;
  47.         array_push($return,array(
  48.             'id' => $row['ID'],
  49.             'user' => $row['userID'],
  50.             'species' => $row['species'],
  51.             'name' => $row['name'],
  52.             'gender' => $row['gender'],
  53.             'exp' => $row['clicks'],
  54.             'created' => $row['creation']
  55.         ));
  56.     }
  57.     if($num_results>0)
  58.     {
  59.         $app->response()['Content-Type'] = 'application/json';
  60.         $app->render('json',array('output'=>$return));
  61.     }
  62.     else
  63.         throw new Exception("Couldn't find any adoptables".((isset($adoptID))?('by the ID of '.$adoptID):"").".");
  64. });
  65.  
  66. $app->post('/adoptable',function() use($app,$con){
  67.     $user=getUser();
  68.     $adopt_name=isset($_POST['name'])?mysql_encode_string($_POST['name']):'null';
  69.     $adopt_species=$_POST['species'];
  70.     if($adopt_species=="")
  71.         throw new Exception("A species is required.");
  72.     $query = "Insert into adoptables(userId,name,speciesId) values($user,$adopt_name,$adopt_species)";
  73.     $result = mysql_query($query,$con);
  74.     if($result==null)
  75.         throw new Exception(mysql_error());
  76.     $app->response()['Content-Type'] = 'application/json';
  77.     $app->render('json',array('output'=>array("id"=>mysql_insert_id())));
  78. });
  79.  
  80. $app->post('/click/:id',function($adoptID) use($app,$con){
  81.     $user=getUser();
  82.     $query="INSERT INTO clicks(userID,adoptableID) VALUES($user,$adoptID) ON DUPLICATE KEY UPDATE lastClick=CURRENT_TIMESTAMP";
  83.     $result = mysql_query($query,$con);
  84.     $app->response()['Content-Type'] = 'application/json';
  85.     $app->render('json',array('output'=>array('Response'=>($result?'Good.':mysql_error()))));
  86. });
  87.  
  88. $app->run();
  89. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement