Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require 'dbConnect.php';
- require 'Slim/Slim.php';
- \Slim\Slim::registerAutoloader();
- $con = Connect('Adoptables');
- class JSONTemplate extends \Slim\View
- {
- public function render($template)
- {
- if($template=='json')
- return json_encode($this->data['output']);
- }
- }
- function getUser(){return 0;}//Do this later.
- $app = new \Slim\Slim(array(
- 'view' => new JSONTemplate()
- ));
- $app->get('/adoptable(/:id)', function ($adoptID=null) use($app,$con){
- $lang = isset($_GET['lang'])?$_GET['lang']:'0';
- $query = 'Select
- adopt.id as "ID",
- adopt.userId as "userID",
- (select species_names.name from species_names where species_names.speciesId=adopt.speciesId AND species_names.languageId='.$lang.') as "species",
- coalesce(
- adopt.name,
- (select species_names.name from species_names where species_names.speciesId=adopt.speciesId AND species_names.languageId='.$lang.' )
- ) as "name",
- adopt.gender as "gender",
- adopt.level as "level",
- adopt.clicks as "clicks",
- adopt.adopted as "creation"
- from adoptables as adopt';
- if($adoptID!==null){
- $query = $query . "\n where adopt.id=$adoptID limit 0,1";
- }
- $num_results = 0;
- $query_result = mysql_query($query,$con) or die(mysql_error());
- $return = array();
- while($row = mysql_fetch_array($query_result))
- {
- $num_results+=1;
- array_push($return,array(
- 'id' => $row['ID'],
- 'user' => $row['userID'],
- 'species' => $row['species'],
- 'name' => $row['name'],
- 'gender' => $row['gender'],
- 'exp' => $row['clicks'],
- 'created' => $row['creation']
- ));
- }
- if($num_results>0)
- {
- $app->response()['Content-Type'] = 'application/json';
- $app->render('json',array('output'=>$return));
- }
- else
- throw new Exception("Couldn't find any adoptables".((isset($adoptID))?('by the ID of '.$adoptID):"").".");
- });
- $app->post('/adoptable',function() use($app,$con){
- $user=getUser();
- $adopt_name=isset($_POST['name'])?mysql_encode_string($_POST['name']):'null';
- $adopt_species=$_POST['species'];
- if($adopt_species=="")
- throw new Exception("A species is required.");
- $query = "Insert into adoptables(userId,name,speciesId) values($user,$adopt_name,$adopt_species)";
- $result = mysql_query($query,$con);
- if($result==null)
- throw new Exception(mysql_error());
- $app->response()['Content-Type'] = 'application/json';
- $app->render('json',array('output'=>array("id"=>mysql_insert_id())));
- });
- $app->post('/click/:id',function($adoptID) use($app,$con){
- $user=getUser();
- $query="INSERT INTO clicks(userID,adoptableID) VALUES($user,$adoptID) ON DUPLICATE KEY UPDATE lastClick=CURRENT_TIMESTAMP";
- $result = mysql_query($query,$con);
- $app->response()['Content-Type'] = 'application/json';
- $app->render('json',array('output'=>array('Response'=>($result?'Good.':mysql_error()))));
- });
- $app->get('/click/:id',function($adoptID) use($app,$con){
- $query="Select userID,lastClick from clicks where adoptableId=$adoptID";
- $result = mysql_query($query,$con);
- $app->response()['Content-Type'] = 'application/json';
- $ret=array();
- while($row=mysql_fetch_array($result))
- {
- array_push($ret,array(
- 'visitor' => $row['userID'],
- 'time' => $row['lastClick']
- ));
- }
- $app->render('json',array('output'=>$result?$ret:mysql_error()));
- });
- $app->run();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement