Added support for fetching categories list
authorArtem Daniliants <artem@daniliants.com>
Wed, 17 Mar 2010 12:53:00 +0000 (14:53 +0200)
committerArtem Daniliants <artem@daniliants.com>
Wed, 17 Mar 2010 13:04:59 +0000 (15:04 +0200)
Server/application/controllers/api.php
Server/application/models/category.php [new file with mode: 0644]
Server/application/models/user.php
Server/application/views/api/categories.php [new file with mode: 0644]

index 6b05e45..e40b263 100644 (file)
@@ -54,21 +54,53 @@ class Api_Controller extends Controller{
      *
      */
     public function login(){
-       if (isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])){
-               $user = new User_Model();
-               if ($user->login($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']))
+       if ($this->is_authorized()){
                  print "OK";
-               else {
-                 header('HTTP/1.0 401 Unauthorized');
-                 print "Invalid credentials";
                  die;
-               } 
        }
-       else {
-               header('HTTP/1.0 401 Unauthorized');
-            print "No credentials supplied";
-            die;
+               else
+                 $this->not_authorized();
+    }
+
+    /*
+     * Validate supplied credentials
+     */
+    public function is_authorized(){
+       if (isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])){
+            $user = new User_Model();
+            if ($user->login($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']))
+                return true;
+            else
+                return false;
+       }
+        else
+            return false;
+
+    }
+
+    /*
+     * Display "You're not authorized error to client
+     *
+     * @todo Need to create function for generally displaying errors
+     */
+    public function not_authorized(){
+       header('HTTP/1.0 401 Unauthorized');
+        print "Invalid credentials or not registered";
+        die;
+    }
+
+    /*
+     * Get categories list and output it as XML
+     *
+     */
+    public function categories(){
+       if ($this->is_authorized()){
+               $xml = new View('api/categories');
+               $cat = new Category_Model();
+               $xml->categories=$cat->get_all();
+               $xml->render(true);
        }
-          
+       else
+          $this->not_authorized();
     }
 }
\ No newline at end of file
diff --git a/Server/application/models/category.php b/Server/application/models/category.php
new file mode 100644 (file)
index 0000000..f47f0e4
--- /dev/null
@@ -0,0 +1,31 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+/*
+ * Categories model for creating and manipulating categories
+ *
+ * @author      Artem Daniliants <artem@daniliants.com>
+ * @copyright   (c) 2010 Speed Freak team
+ * @license     http://opensource.org/licenses/gpl-license.php GNU Public License
+ */
+
+class Category_Model extends Model {
+
+    public function __construct(){
+
+        // load database library into $this->db
+        parent::__construct();
+    }
+
+    /*
+     * Fetch all categories
+     *
+     * @return object|bool Returns object containing results if everything is ok and false otherwise
+     */
+    public function get_all(){
+        $results = $this->db->query("SELECT slug,description,unit FROM categories");
+       if ($results->count()>0)
+            return $results;
+        else
+            return false;
+    }
+
+}
\ No newline at end of file
index 4440cdf..855ffaf 100644 (file)
@@ -1,6 +1,6 @@
 <?php defined('SYSPATH') or die('No direct script access.');
 /*
- * API controller for communicating with mobile clients
+ * Users model for creating and manipulating user accounts
  * 
  * @author      Artem Daniliants <artem@daniliants.com>
  * @copyright   (c) 2010 Speed Freak team
diff --git a/Server/application/views/api/categories.php b/Server/application/views/api/categories.php
new file mode 100644 (file)
index 0000000..287fa97
--- /dev/null
@@ -0,0 +1,5 @@
+<?php echo "<?"; ?>xml version="1.0" encoding="utf-8" <?php echo "?>"; ?>
+
+<categories>
+    <?php foreach ($categories as $k){ ?>   <category description="<?php echo $k->description; ?>" unit="<?php echo $k->unit; ?>"><?php echo $k->slug; ?></category>
+<?php } ?></catgories>
\ No newline at end of file