Update version.php to 3.3.0
[isso.git] / Api.php
diff --git a/Api.php b/Api.php
index 89d8dd90b318640fef09c625a641551b5dd8ac34..aa6d6ca79b91b5482c13fc60485e44e66b611da6 100644 (file)
--- a/Api.php
+++ b/Api.php
@@ -2,7 +2,7 @@
 /*=====================================================================*\
 || ###################################################################
 || # Blue Static ISSO Framework
-|| # Copyright (c)2005-2008 Blue Static
+|| # Copyright (c)2005-2009 Blue Static
 || #
 || # 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
@@ -62,7 +62,7 @@ if (!defined('REQ_AUTO'))
  * Abstract API
  *
  * Abstract class that is used as an API base for most common database interaction
- * schemes. Creates a simple structure that holds data and can update, remove, and
+ * schemes. Creates a simple structure that holds data and can update, delete, and
  * insert.
  * 
  * Life-cycle of a new object:
@@ -72,7 +72,7 @@ if (!defined('REQ_AUTO'))
  * 4. try { $o->insert(); <other actions that depend on the saved record> } catch (ApiException $e) {}
  *
  * @author             Blue Static
- * @copyright  Copyright (c)2005 - 2008, Blue Static
+ * @copyright  Copyright (c)2005 - 2009, Blue Static
  * @package            ISSO
  * 
  */
@@ -166,7 +166,7 @@ abstract class BSApi
         * This simply throws the ApiException if it exists, which inside holds
         * all of the individual and specific errors
         */
-       private function _processErrorQueue()
+       protected function _processErrorQueue()
        {
                if ($this->exception)
                {
@@ -193,6 +193,32 @@ abstract class BSApi
                }
        }
        
+       /**
+        * Resets the API object to an initial state. This will NOT clear the primary (REQ_AUTO)
+        * field.
+        */
+       public function reset()
+       {
+               foreach ($this->fields as $field => $settings)
+               {
+                       if ($settings[F_REQ] == REQ_AUTO)
+                       {
+                               $savename       = $field;
+                               $savevalue      = $this->fetchValue($field);
+                               $savevalue      = ($savevalue ? $savevalue : $this->insertid);
+                               break;
+                       }
+               }
+               
+               $this->setfields        = array();
+               $this->values           = array();
+               $this->condition        = '';
+               $this->insertid         = 0;
+               $this->exception        = null;
+               
+               $this->set($savename, $savevalue);
+       }
+       
        /**
         * Sets a value, sanitizes it, and validates it
         *
@@ -374,10 +400,10 @@ abstract class BSApi
        /**
         * Deletes a record
         *
-        * @param       bool    Run pre_remove()?
-        * @param       bool    Run post_remove()?
+        * @param       bool    Run pre_delete()?
+        * @param       bool    Run post_delete()?
         */
-       public function remove($doPre = true, $doPost = true)
+       public function delete($doPre = true, $doPost = true)
        {
                if (!$this->condition)
                {
@@ -386,17 +412,17 @@ abstract class BSApi
                
                $this->fetch();
                
-               $this->_runActionMethod('pre_remove', $doPre);
+               $this->_runActionMethod('pre_delete', $doPre);
                
                BSApp::$db->query("DELETE FROM {$this->prefix}{$this->table} WHERE {$this->condition}");
                
-               $this->_runActionMethod('post_remove', $doPost);
+               $this->_runActionMethod('post_delete', $doPost);
        }
        
        /**
         * Verifies that all required fields are set
         */
-       private function _verifyRequired()
+       protected function _verifyRequired()
        {
                foreach ($this->fields as $name => $options)
                {
@@ -420,7 +446,7 @@ abstract class BSApi
         * @param       string  Action to run
         * @param       bool    Actually run it?
         */
-       private function _runActionMethod($method, $doRun)
+       protected function _runActionMethod($method, $doRun)
        {
                if (!$doRun || !method_exists($this, $method))
                {
@@ -438,7 +464,7 @@ abstract class BSApi
         *
         * @return      string  Prepared value entry
         */
-       private function _prepareFieldForSql($name)
+       protected function _prepareFieldForSql($name)
        {
                $type = $this->fields["$name"][F_TYPE];
                
@@ -460,6 +486,27 @@ abstract class BSApi
                }
        }
        
+       /**
+        * Determines the value of a field from Api->record[], Api->values[] (in that order)
+        *
+        * @param       string  The field ID to determine for
+        *
+        * @return      mixed
+        */
+       public function fetchValue($field)
+       {
+               if ($this->record[$field])
+               {
+                       return $this->record[$field];
+               }
+               else if ($this->values[$field])
+               {
+                       return $this->values[$field];
+               }
+               
+               return null;
+       }
+       
        /**
         * Verify field: not a zero value
         */
@@ -511,7 +558,7 @@ abstract class BSApi
  * of exceptions that can be thrown as one
  *
  * @author             rsesek
- * @copyright  Copyright (c)2005 - 2008, Blue Static
+ * @copyright  Copyright (c)2005 - 2009, Blue Static
  * @package            ISSO
  *
  */
@@ -528,7 +575,7 @@ class ApiException extends Exception
         */
        public function __construct()
        {
-               parent::__construct(_('An error occurred while processing the API data.'));
+               parent::__construct(_('An error occurred while processing the API data. Errors: '));
        }
        
        /**
@@ -539,6 +586,7 @@ class ApiException extends Exception
        public function addException(Exception $e)
        {
                $this->exceptions[] = $e;
+               $this->message .= ' (' . sizeof($this->exceptions) . ') ' . $e->getMessage();
        }
        
        /**
@@ -558,7 +606,7 @@ class ApiException extends Exception
  * This exception represents a problem with an API field
  *
  * @author             rsesek
- * @copyright  Copyright (c)2005 - 2008, Blue Static
+ * @copyright  Copyright (c)2005 - 2009, Blue Static
  * @package            ISSO
  *
  */