DataAccess/mysql/Item.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 // | Authors: Greg Gilbert <ggilbert@codejanitor.com>                     |
00021 // +----------------------------------------------------------------------+
00022 
00023 // }}}
00024 // {{{ requires
00025 
00026 require_once FASTFRAME_ROOT . 'lib/FastFrame/DataAccess.php';
00027 require_once dirname(__FILE__) . '/../../Model/Log.php';
00028 
00029 // }}}
00030 // {{{ constants
00031 
00032 // The filters for the list page
00033 define('ITEMS_FILTER_CATEGORY', 1);
00034 define('ITEMS_FILTER_UNFILED', 2);
00035 define('ITEMS_FILTER_ALL', 3);
00036 define('ITEMS_GROUPS_FILTER_CATEGORY', 4);
00037 define('ITEMS_GROUPS_FILTER_UNFILED', 5);
00038 define('ITEMS_GROUPS_FILTER_ALL', 6);
00039 define('GROUPS_FILTER_CATEGORY', 7);
00040 define('GROUPS_FILTER_UNFILED', 8);
00041 define('GROUPS_FILTER_ALL', 9);
00042 define('ITEMS_FILTER_NO_RESULTS', 10);
00043 define('ITEMS_FILTER_MISSING', 11);
00044 define('ITEMS_FILTER_VENDOR', 12);
00045 define('ITEMS_FILTER_RULE', 13);
00046 
00047 // }}}
00048 // {{{ class FF_DataAccess_Item 
00049 
00060 // }}}
00061 class FF_DataAccess_Item_mysql extends FF_DataAccess {
00062     // {{{ properties
00063 
00068     var $attributesTable;
00069 
00074     var $categoriesTable;
00075 
00080     var $ruleItemsTable;
00081 
00086     var $userId;
00087 
00092     var $filterName;
00093 
00094     // }}}
00095     // {{{ constructor
00096 
00103     function FF_DataAccess_Item_mysql()
00104     {
00105         FF_DataAccess::FF_DataAccess();
00106         $this->table = $this->o_registry->getConfigParam('data/items_table');
00107         $this->attributesTable = $this->o_registry->getConfigParam('data/item_attributes_table');
00108         $this->categoriesTable = $this->o_registry->getConfigParam('data/categories_table');
00109         $this->ruleItemsTable = $this->o_registry->getConfigParam('data/rule_items_table');
00110         $this->userId = FF_Auth::getCredential('userId'); 
00111         $this->o_log =& new FF_Model_Log();
00112     }
00113 
00114     // }}}
00115     // {{{ update()
00116 
00125     function update($in_data)
00126     {
00127         $a_attrib = $in_data['attributes'];
00128         unset($in_data['attributes']);
00129         unset($in_data['isGroup']);
00130         unset($in_data['newComment']);
00131         unset($in_data['commentsToRemove']);
00132         unset($in_data['rules']);
00133         $o_result =& parent::update($in_data);
00134         if ($o_result->isSuccess()) {
00135             $tmp_result =& $this->o_log->newLogEntry('UPDATE', $in_data['id'], $this->table);
00136         }
00137 
00138         $tmp_result =& $this->updateAttributes($in_data['id'], $a_attrib);
00139         if (!$tmp_result->isSuccess()) {
00140             $o_result->addMessage(_('Unable to add item attributes.'));
00141             $o_result->setSuccess(false);
00142         }
00143 
00144         return $o_result;
00145     }
00146 
00147     // }}}
00148     // {{{ add()
00149 
00158     function add($in_data)
00159     {
00160         $a_attrib = $in_data['attributes'];
00161         unset($in_data['attributes']);
00162         unset($in_data['isGroup']);
00163         unset($in_data['newComment']);
00164         unset($in_data['commentsToRemove']);
00165         unset($in_data['rules']);
00166         $in_data['deleted'] = false;
00167         $o_result =& parent::add($in_data);
00168         if ($o_result->isSuccess()) {
00169             $tmp_result =& $this->o_log->newLogEntry('INSERT', $in_data['id'], $this->table);
00170         }
00171 
00172         $tmp_result =& $this->updateAttributes($in_data['id'], $a_attrib);
00173         if (!$tmp_result->isSuccess()) {
00174             $o_result->addMessage(_('Unable to add item attributes.'));
00175             $o_result->setSuccess(false);
00176         }
00177 
00178         return $o_result;
00179     }
00180 
00181     // }}}
00182     // {{{ remove()
00183 
00192     function remove($in_itemId)
00193     {
00194         $o_result = new FF_Result();
00195         // Return the item from any loan it is in first
00196         $o_dao =& FF_DataAccess::factory('Transaction');
00197         // This has the loan type constants
00198         require_once dirname(__FILE__) . '/Loan.php';
00199         $o_dao->returnItemsInTransaction(array(array('itemId' => $in_itemId)));
00200         $s_where = 'id=' . $this->o_data->quoteSmart($in_itemId);
00201         $result = $this->o_data->autoExecute($this->table, array('deleted' => true), DB_AUTOQUERY_UPDATE, $s_where);
00202         if (DB::isError($result)) {
00203             $o_result->addMessage($result->getMessage());
00204             $o_result->setSuccess(false);
00205         }
00206         else {
00207             // Remove associated image
00208             $o_fileCache =& FF_FileCache::singleton();
00209             $o_fileCache->remove(array('subdir' => 'item_images', 'name' => $in_itemId), true);
00210             // Remove it from any groups
00211             $o_dao =& FF_DataAccess::factory('Group');
00212             $o_dao->removeItemFromAllGroups($in_itemId);
00213             $tmp_result =& $this->o_log->newLogEntry('DELETE', $in_itemId, $this->table);
00214         }
00215 
00216         return $o_result;
00217     }
00218 
00219     // }}}
00220     // {{{ getDataByPrimaryKey()
00221 
00232     function getDataByPrimaryKey($in_id, $in_fields = '*')
00233     {
00234         $a_data = parent::getDataByPrimaryKey($in_id, $in_fields);
00235         if (count($a_data) != 0) {
00236             $s_query = "SELECT k, v FROM $this->attributesTable WHERE item_id=?"; 
00237             $a_data['attributes'] = $this->o_data->getAssoc($s_query, false, $in_id);
00238             $o_rulesDataAccess =& FF_DataAccess::factory('Rule');
00239             $a_data['rules'] = $o_rulesDataAccess->getRulesForItem($in_id);
00240         }
00241 
00242         return $a_data;
00243     }
00244 
00245     // }}}
00246     // {{{ getListData()
00247 
00259     function getListData($in_where, $in_orderByField, $in_orderByDir, $in_fields = '*')
00260     {
00261         if (!empty($this->filterName)) {
00262             $s_query = $this->_getFilterQuery($in_where);
00263             $s_query .= " ORDER BY $in_orderByField " . $this->_getOrderByDirection($in_orderByDir);
00264             return $this->o_data->query($s_query);
00265         }
00266         else {
00267             return parent::getListData($in_where, $in_orderByField, $in_orderByDir, $in_fields); 
00268         }
00269     }
00270 
00271     // }}}
00272     // {{{ getListFilter()
00273 
00286     function getListFilter($in_searchString, $in_searchFields, $in_filter, $in_filterData)
00287     {
00288         // Filter is needed in getListData as well
00289         $this->filterName = $in_filter;
00290         $s_filter = 'deleted != 1 AND ';
00291         if ($this->filterName == ITEMS_FILTER_NO_RESULTS) {
00292             return '0=1';
00293         }
00294         elseif ($this->filterName == ITEMS_FILTER_MISSING) {
00295             $s_filter .= sprintf('status = %s AND ', 
00296                     $this->o_data->quoteSmart(ITEM_STATUS_MISSING));
00297         }
00298         elseif ($this->filterName == ITEMS_FILTER_VENDOR) {
00299             $s_filter .= sprintf('vendor_id = %s AND ', 
00300                     $this->o_data->quoteSmart($in_filterData['vendor_id']));
00301         }
00302         elseif ($this->filterName == ITEMS_FILTER_RULE) {
00303             $s_filter .= sprintf('rule_id = %s AND ', 
00304                     $this->o_data->quoteSmart($in_filterData['rule_id']));
00305         }
00306         elseif ($this->filterName == ITEMS_FILTER_CATEGORY ||
00307                 $this->filterName == ITEMS_GROUPS_FILTER_CATEGORY ||
00308                 $this->filterName == GROUPS_FILTER_CATEGORY) {
00309             $s_filter .= sprintf('category_id = %s AND ', 
00310                     $this->o_data->quoteSmart($in_filterData['catId']));
00311         }
00312         elseif ($this->filterName == ITEMS_FILTER_UNFILED ||
00313                 $this->filterName == ITEMS_GROUPS_FILTER_UNFILED ||
00314                 $this->filterName == GROUPS_FILTER_UNFILED) {
00315             $in_filterData['tableName'] = 't1';
00316             $s_filter .= 't2.id IS NULL AND ';
00317         }
00318 
00319         // See if we should only show those items that are shopper viewable
00320         if ($in_filterData['shopperViewable']) {
00321             $s_filter .= 't1.shopper_viewable = 1 AND ';
00322         }
00323 
00324         // See if we need to restrict by store id.
00325         if (!empty($in_filterData['storeId'])) {
00326             $s_filter .= sprintf('(t1.store_id = %s', $this->o_data->quoteSmart($in_filterData['storeId']));
00327             // See if we want to include items that are marked as being in all stores
00328             if (!empty($in_filterData['includeAllStore'])) {
00329                 $s_filter .= ' OR t1.store_id = 0';
00330             }
00331 
00332             $s_filter .= ') AND ';
00333         }
00334 
00335 
00336         $s_filter .= '(' . parent::getListFilter($in_searchString, $in_searchFields, $in_filter, $in_filterData) . ')';
00337         return $s_filter;
00338     }
00339 
00340     // }}}
00341     // {{{ updateAttributes()
00342 
00352     function &updateAttributes($in_itemId, $in_attributes)
00353     {
00354         $o_result = new FF_Result();
00355         if (count($in_attributes)) {
00356             // First remove all attributes for the item 
00357             $s_stmt = $this->o_data->prepare("DELETE FROM $this->attributesTable WHERE item_id=?");
00358             if (DB::isError($result = $this->o_data->execute($s_stmt, $in_itemId))) {
00359                 $o_result->addMessage($result->getMessage());
00360                 $o_result->setSuccess(false);
00361                 return $o_result;
00362             }
00363 
00364             // Add all attributes for the user
00365             $s_stmt = $this->o_data->autoPrepare($this->attributesTable, array('item_id', 'k', 'v'));
00366             $a_data = array();
00367             foreach ($in_attributes as $s_key => $s_val) {
00368                 $a_data[] = array($in_itemId, $s_key, $s_val);
00369             }
00370 
00371             if (DB::isError($result = $this->o_data->executeMultiple($s_stmt, $a_data))) {
00372                 $o_result->addMessage($result->getMessage());
00373                 $o_result->setSuccess(false);
00374             } 
00375         }
00376 
00377         return $o_result;
00378     }
00379 
00380     // }}}
00381     // {{{ updateStatusOfItems()
00382 
00392     function &updateStatusOfItems($in_items, $in_status)
00393     {
00394         $o_result = new FF_Result();
00395         $s_itemList = '';
00396         foreach ($in_items as $s_item) {
00397             $s_itemList .= 'id = ' . $this->o_data->quoteSmart($s_item) . " OR \n";
00398         }
00399 
00400         $s_itemList .= '0=1';
00401         $result = $this->o_data->autoExecute($this->table, array('status' => $in_status), DB_AUTOQUERY_UPDATE, $s_itemList);
00402         if (DB::isError($result)) {
00403             $o_result->addMessage($result->getMessage());
00404             $o_result->setSuccess(false);
00405         }
00406 
00407         return $o_result;
00408     }
00409 
00410     // }}}
00411     // {{{ getItemIdByBarcode()
00412 
00421     function getItemIdByBarcode($in_barcode)
00422     {
00423         $s_query = "SELECT id FROM $this->table WHERE barcode=? AND deleted=0";
00424         return $this->o_data->getOne($s_query, $in_barcode);
00425     }
00426 
00427     // }}}
00428     // {{{ isBarcodeUnique()
00429 
00440     function isBarcodeUnique($in_barcode, $in_id, $in_isUpdate)
00441     {
00442         $s_where = 'AND deleted != 1';
00443         return parent::isDataUnique('barcode', $in_barcode, $in_id, $in_isUpdate, $s_where);
00444     }
00445 
00446     // }}}
00447     // {{{ getNextId()
00448 
00455     function getNextId()
00456     {
00457         // change sequence name since we give it the full table name
00458         $this->o_data->setOption('seqname_format', '%s');
00459         return $this->o_data->nextId($this->o_registry->getConfigParam('data/sequence_table'));
00460     }
00461 
00462     // }}}
00463     // {{{ _getFilterQuery()
00464 
00473     function _getFilterQuery($in_where)
00474     {
00475         if ($this->filterName == GROUPS_FILTER_CATEGORY ||
00476             $this->filterName == GROUPS_FILTER_UNFILED ||
00477             $this->filterName == GROUPS_FILTER_ALL) {
00478             return $this->_getGroupListQuery($in_where);
00479         }
00480         // Filter: gettting both items and groups
00481         elseif ($this->filterName == ITEMS_GROUPS_FILTER_CATEGORY ||
00482                 $this->filterName == ITEMS_GROUPS_FILTER_UNFILED ||
00483                 $this->filterName == ITEMS_GROUPS_FILTER_ALL ||
00484                 $this->filterName == ITEMS_FILTER_RULE) {
00485             return $this->_getItemGroupListQuery($in_where);
00486         }
00487         // Filter: gettting only items
00488         else {
00489             return $this->_getItemListQuery($in_where);
00490         }
00491     }
00492 
00493     // }}}
00494     // {{{ _getGroupListQuery()
00495 
00504     function _getGroupListQuery($in_where)
00505     {
00506         $s_join = '';
00507         $s_field = '';
00508         if ($this->filterName == GROUPS_FILTER_UNFILED ||
00509             $this->filterName == ITEMS_GROUPS_FILTER_UNFILED) {
00510             // Have to alias the field so it doesn't overwrite 'id'
00511             $s_field = ', t2.id AS catId';
00512             $s_join = "LEFT JOIN $this->categoriesTable AS t2 ON t1.category_id = t2.id";
00513         }
00514         elseif ($this->filterName == ITEMS_FILTER_RULE) {
00515             $s_join = "LEFT JOIN $this->ruleItemsTable AS t2 ON t1.id = t2.item_id";
00516         }
00517 
00518         $s_groupsTable = $this->o_registry->getConfigParam('data/groups_table');
00519         // Make it always match deleted clause (F != T)
00520         $in_where = str_replace('deleted !=', '0 !=', $in_where);
00521         // Remove status, barcode, and serial num clause since it doesn't have those fields
00522         $in_where = preg_replace('/(t1\.)?`status` LIKE \'%.*?%\'/s', '0=1', $in_where);
00523         $in_where = preg_replace('/(t1\.)?`barcode` LIKE \'%.*?%\'/s', '0=1', $in_where);
00524         $in_where = preg_replace('/(t1\.)?`serial_num` LIKE \'%.*?%\'/s', '0=1', $in_where);
00525         $s_na = $this->o_data->quoteSmart(_('N/A'));
00526         return "SELECT t1.id, t1.name, t1.description, $s_na AS barcode, -1 AS status, 
00527                 $s_na AS serial_num, 1 AS isGroup $s_field
00528                 FROM $s_groupsTable AS t1 $s_join WHERE $in_where";
00529     }
00530 
00531     // }}}
00532     // {{{ _getItemListQuery()
00533 
00542     function _getItemListQuery($in_where)
00543     {
00544         $s_join = '';
00545         $s_field = '';
00546         if ($this->filterName == ITEMS_FILTER_UNFILED ||
00547             $this->filterName == ITEMS_GROUPS_FILTER_UNFILED) {
00548             // Have to alias the field so it doesn't overwrite 'id'
00549             $s_field = ', t2.id AS catId';
00550             $s_join = "LEFT JOIN $this->categoriesTable AS t2 ON t1.category_id= t2.id";
00551         }
00552         elseif ($this->filterName == ITEMS_FILTER_RULE) {
00553             $s_join = "LEFT JOIN $this->ruleItemsTable AS t2 ON t1.id = t2.item_id";
00554         }
00555 
00556         return "SELECT t1.id, t1.name, t1.description, t1.barcode, t1.status, 
00557                 t1.serial_num, 0 AS isGroup $s_field 
00558                 FROM $this->table AS t1 $s_join WHERE $in_where";
00559     }
00560 
00561     // }}}
00562     // {{{ _getItemGroupListQuery()
00563 
00572     function _getItemGroupListQuery($in_where)
00573     {
00574         return sprintf('(%s) UNION ALL (%s)', 
00575                 $this->_getItemListQuery($in_where),
00576                 $this->_getGroupListQuery($in_where));
00577     }
00578 
00579     // }}}
00580 }
00581 ?>

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