DataAccess/mysql/Inventory.php

Go to the documentation of this file.
00001 <?php
00003 // {{{ license
00004 
00005 // +----------------------------------------------------------------------+
00006 // | FastFrame Application Framework                                      |
00007 // +----------------------------------------------------------------------+
00008 // | Copyright (c) 2002-2006 The Codejanitor Group                        |
00009 // +----------------------------------------------------------------------+
00010 // | This source file is subject to the GNU Lesser Public License (LGPL), |
00011 // | that is bundled with this package in the file LICENSE, and is        |
00012 // | available at through the world-wide-web at                           |
00013 // | http://www.fsf.org/copyleft/lesser.html                              |
00014 // | If you did not receive a copy of the LGPL and are unable to          |
00015 // | obtain it through the world-wide-web, you can get it by writing the  |
00016 // | Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
00017 // | MA 02111-1307, USA.                                                  |
00018 // +----------------------------------------------------------------------+
00019 // | Authors: Jason Rust <jrust@codejanitor.com>                          |
00020 // +----------------------------------------------------------------------+
00021 
00022 // }}}
00023 // {{{ requires
00024 
00025 require_once FASTFRAME_ROOT . 'lib/FastFrame/DataAccess.php';
00026 
00027 // }}}
00028 // {{{ class FF_DataAccess_Inventory 
00029 
00040 // }}}
00041 class FF_DataAccess_Inventory_mysql extends FF_DataAccess {
00042     // {{{ properties
00043 
00048     var $itemsTable;
00049 
00054     var $userId;
00055 
00056     // }}}
00057     // {{{ constructor
00058 
00065     function FF_DataAccess_Inventory_mysql()
00066     {
00067         FF_DataAccess::FF_DataAccess();
00068         $this->table = $this->o_registry->getConfigParam('data/item_inventory_table');
00069         $this->itemsTable = $this->o_registry->getConfigParam('data/items_table');
00070         $this->userId = FF_Auth::getCredential('userId'); 
00071     }
00072 
00073     // }}}
00074     // {{{ add()
00075 
00084     function add($in_data)
00085     {
00086         $o_result = new FF_Result();
00087         $s_stmt = $this->o_data->autoPrepare($this->table, array('item_id', 'entered_date', 'user_id'));
00088         $tmp_data[0] = null;
00089         $tmp_data[1] = $this->timestampToISODate(time());
00090         $tmp_data[2] = $this->userId;
00091         foreach ($in_data['items'] as $a_data) {
00092             if (!is_null($a_data['item_id'])) {
00093                 $tmp_data[0] = $a_data['item_id'];
00094                 if (DB::isError($result = $this->o_data->execute($s_stmt, $tmp_data))) {
00095                     $o_result->addMessage($result->getMessage());
00096                     $o_result->setSuccess(false);
00097                 }
00098             }
00099         }
00100 
00101         return $o_result;
00102     }
00103 
00104     // }}}
00105     // {{{ process()
00106 
00113     function &process()
00114     {
00115         require_once dirname(__FILE__) . '/../../Model/Item.php';
00116         $o_result = new FF_Result();
00117         $s_query = "SELECT item_id FROM $this->table";
00118         if (DB::isError($a_items = $this->o_data->getCol($s_query, 0))) {
00119             $o_result->addMessage($a_items->getMessage());
00120             $o_result->setSuccess(false);
00121             return $o_result;
00122         }
00123 
00124         // First mark everything as missing that is available
00125         $result = $this->o_data->autoExecute($this->itemsTable, 
00126                 array('status' => ITEM_STATUS_MISSING), DB_AUTOQUERY_UPDATE, 
00127                 'status=' . $this->o_data->quoteSmart(ITEM_STATUS_AVAILABLE));
00128 
00129         if (DB::isError($result)) {
00130             $o_result->addMessage($result->getMessage());
00131             $o_result->setSuccess(false);
00132             return $o_result;
00133         }
00134 
00135         // Generate where statement
00136         $s_where = '';
00137         foreach ($a_items as $s_item) {
00138             $s_where .= 'id = ' . $s_item . " OR \n";
00139         }
00140 
00141         $s_where .= '0=1';
00142         $tmp_where = 'status=' . $this->o_data->quoteSmart(ITEM_STATUS_MISSING) . " AND ($s_where)";
00143         // Set the status to available if the item is currently marked as missing, 
00144         // and is in the inventory list.
00145         $result = $this->o_data->autoExecute($this->itemsTable, 
00146                 array('status' => ITEM_STATUS_AVAILABLE), DB_AUTOQUERY_UPDATE, $tmp_where);
00147 
00148         if (DB::isError($result)) {
00149             $o_result->addMessage($result->getMessage());
00150             $o_result->setSuccess(false);
00151             return $o_result;
00152         }
00153 
00154         // Now return all the items that are marked onloan, but in inventory
00155         $s_query = "SELECT id AS itemId FROM $this->itemsTable WHERE status=? AND ($s_where)";
00156         $a_items = $this->o_data->getAll($s_query, array(ITEM_STATUS_ONLOAN));
00157         $o_dao =& FF_DataAccess::factory('Transaction');
00158         $tmp_result =& $o_dao->returnItemsInTransaction($a_items);
00159         if (!$tmp_result->isSuccess()) {
00160             $o_result->setSuccess(false);
00161             $o_result->addMessage($tmp_result->getMessages());
00162         }
00163 
00164         return $o_result;
00165     }
00166 
00167     // }}}
00168     // {{{ getListData()
00169 
00181     function getListData($in_where, $in_orderByField, $in_orderByDir, $in_fields = '*')
00182     {
00183         $s_query = "SELECT t1.item_id, UNIX_TIMESTAMP(t1.entered_date) AS entered_date, t1.user_id, 
00184                     t2.name, t2.barcode
00185                     FROM $this->table AS t1
00186                     INNER JOIN $this->itemsTable AS t2 ON t1.item_id=t2.id
00187                     WHERE $in_where ORDER BY $in_orderByField " .
00188                     $this->_getOrderByDirection($in_orderByDir);
00189 
00190         return $this->o_data->query($s_query);
00191     }
00192 
00193     // }}}
00194 }
00195 ?>

Generated on Fri Jun 23 11:38:16 2006 for FastFrame by  doxygen 1.4.4