DataAccess/mysql/Demo.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 define('DEMOS_FILTER_BORROWER_DEMO', 1);
00033 
00034 // }}}
00035 // {{{ class FF_DataAccess_Demo_mysql 
00036 
00047 // }}}
00048 class FF_DataAccess_Demo_mysql extends FF_DataAccess {
00049     // {{{ properties
00050 
00055     var $o_log;
00056 
00061     var $demoItemsTable;
00062 
00067     var $borrowerDemosTable;
00068 
00073     var $itemsTable;
00074 
00079     var $groupsTable;
00080 
00085     var $filterName;
00086 
00087     // }}}
00088     // {{{ constructor
00089 
00096     function FF_DataAccess_Demo_mysql()
00097     {
00098         FF_DataAccess::FF_DataAccess();
00099         $this->table = $this->o_registry->getConfigParam('data/demos_table');
00100         $this->demoItemsTable = $this->o_registry->getConfigParam('data/demo_items_table');
00101         $this->groupsTable = $this->o_registry->getConfigParam('data/groups_table');
00102         $this->itemsTable = $this->o_registry->getConfigParam('data/items_table');
00103         $this->borrowerDemosTable = $this->o_registry->getConfigParam('data/borrower_demos_table');
00104         $this->o_log =& new FF_Model_Log();
00105     }
00106 
00107     // }}}
00108     // {{{ update()
00109 
00118     function update($in_data)
00119     {
00120         $a_items = $in_data['items'];
00121         unset($in_data['items']);
00122         unset($in_data['create_date']);
00123         unset($in_data['itemsToRemove']);
00124         unset($in_data['borrowersToAdd']);
00125         $o_result =& parent::update($in_data);
00126         if ($o_result->isSuccess()) {
00127             $tmp_result =& $this->o_log->newLogEntry('UPDATE', $in_data['id'], $this->table);
00128             $this->addNewItems($in_data['id'], $a_items, $o_result);
00129         }
00130 
00131         return $o_result;
00132     }
00133 
00134     // }}}
00135     // {{{ add()
00136 
00145     function add($in_data)
00146     {
00147         $a_items = $in_data['items'];
00148         unset($in_data['items']);
00149         unset($in_data['create_date']);
00150         unset($in_data['itemsToRemove']);
00151         unset($in_data['borrowersToAdd']);
00152         $in_data['deleted'] = false;
00153         $o_result =& parent::add($in_data);
00154         if ($o_result->isSuccess()) {
00155             $tmp_result =& $this->o_log->newLogEntry('INSERT', $in_data['id'], $this->table);
00156             $this->addNewItems($in_data['id'], $a_items, $o_result);
00157         }
00158 
00159         return $o_result;
00160     }
00161 
00162     // }}}
00163     // {{{ getListData()
00164 
00176     function getListData($in_where, $in_orderByField, $in_orderByDir, $in_fields = '*')
00177     {
00178         if ($this->filterName == DEMOS_FILTER_BORROWER_DEMO) {
00179             $s_query = "SELECT t2.id, name, description, UNIX_TIMESTAMP(t1.create_date) AS create_date
00180                         FROM $this->borrowerDemosTable AS t1 
00181                         INNER JOIN $this->table as t2 ON t1.demo_id = t2.id
00182                         WHERE $in_where GROUP BY t2.id ORDER BY $in_orderByField " . 
00183                         $this->_getOrderByDirection($in_orderByDir);
00184             return $this->o_data->query($s_query);
00185         }
00186         else {
00187             return parent::getListData($in_where, $in_orderByField, $in_orderByDir);
00188         }
00189     }
00190 
00191     // }}}
00192     // {{{ getListFilter()
00193 
00206     function getListFilter($in_searchString, $in_searchFields, $in_filter, $in_filterData)
00207     {
00208         $this->filterName = $in_filter;
00209         if ($this->filterName == DEMOS_FILTER_BORROWER_DEMO) {
00210             $s_filter = sprintf('borrower_id = %s AND t1.deleted != 1',
00211                     $this->o_data->quoteSmart($in_filterData['borrowerId']));
00212         } 
00213         else {
00214             $s_filter = 'deleted != 1 AND ';
00215             $s_filter .= '(' . parent::getListFilter($in_searchString, $in_searchFields, $in_filter, $in_filterData) . ')';
00216         }
00217 
00218         return $s_filter;
00219     }
00220 
00221     // }}}
00222     // {{{ remove()
00223 
00232     function remove($in_demoId)
00233     {
00234         $a_data['deleted'] = true;
00235         $a_data['id'] = $in_demoId;
00236         return parent::update($a_data);
00237     }
00238 
00239     // }}}
00240     // {{{ getNextId()
00241 
00248     function getNextId()
00249     {
00250         // change sequence name since we give it the full table name
00251         $this->o_data->setOption('seqname_format', '%s');
00252         return $this->o_data->nextId($this->o_registry->getConfigParam('data/sequence_table'));
00253     }
00254 
00255     // }}}
00256     //{{{ getDemosByBorrowerId()
00257 
00266     function getDemosByBorrowerId($in_borrowerId)
00267     {
00268         $s_query = "SELECT demo_id FROM $this->borrowerDemosTable WHERE borrower_id = %s AND deleted = 0";
00269         return $this->o_data->getCol($s_query,0, $in_borrowerId);
00270     }
00271     
00272     // }}}
00273     // {{{ getDemoItems()
00274 
00283     function getDemoItems($in_id)
00284     {
00285         $s_query = "SELECT item_id, barcode,
00286                     IF(i.name IS NULL, g.name, i.name) AS name, 
00287                     IF(i.name IS NULL, g.is_kit, 0) AS is_kit, 
00288                     ISNULL(i.name) AS is_group 
00289                     FROM $this->demoItemsTable AS d
00290                     LEFT JOIN $this->itemsTable AS i ON d.item_id=i.id
00291                     LEFT JOIN $this->groupsTable AS g ON d.item_id=g.id
00292                     WHERE demo_id=?"; 
00293         return $this->o_data->getAll($s_query, array($in_id));
00294     }
00295 
00296     // }}}
00297     // {{{ addNewItems()
00298 
00309     function addNewItems($in_demoId, $in_items, &$in_resultObj)
00310     {
00311         $s_query = "SELECT item_id FROM $this->demoItemsTable WHERE demo_id=?"; 
00312         $a_existingItems = $this->o_data->getCol($s_query, 0, $in_demoId);
00313         $s_stmt = $this->o_data->autoPrepare($this->demoItemsTable, array('demo_id', 'item_id'));
00314         foreach ($in_items as $a_data) {
00315             if (!is_null($a_data['itemId']) &&
00316                 !in_array($a_data['itemId'], $a_existingItems)) {
00317                 if (PEAR::isError($o_result = $this->o_data->execute($s_stmt, array($in_demoId, $a_data['itemId'])))) {
00318                     $in_resultObj->addMessage(sprintf(_('Error adding item with id: %s'), $a_data['itemId']));
00319                     $in_resultObj->setSuccess(false);
00320                 }
00321                 else {
00322                     // Add the item to the list of existing items
00323                     $a_existingItems[] = $a_data['itemId'];
00324                 }
00325             }
00326         }
00327     }
00328 
00329     // }}}
00330     // {{{ removeItem()
00331 
00342     function removeItem($in_demoId, $in_itemId, &$in_resultObj)
00343     {
00344         $s_stmt = $this->o_data->prepare("DELETE FROM $this->demoItemsTable WHERE demo_id=? AND item_id=?"); 
00345         if (PEAR::isError($tmp_result = $this->o_data->execute($s_stmt, array($in_demoId, $in_itemId)))) {
00346             $in_resultObj->addMessage($tmp_result->getMessage());
00347             $in_resultObj->setSuccess(false);
00348         }
00349     }
00350 
00351     // }}}
00352     // {{{ addDemoToBorrower()
00353 
00363     function &addDemoToBorrower($in_demoId, $in_borrowerId)
00364     {
00365         $o_result = new FF_Result();
00366         $a_data['borrower_id'] = $in_borrowerId;
00367         $a_data['demo_id'] = $in_demoId;
00368         $a_data['create_date'] = $this->timestampToISODate(time());
00369         $a_data['deleted'] = false;
00370         $result = $this->o_data->autoExecute($this->borrowerDemosTable, $a_data);
00371         if (DB::isError($result)) {
00372             $o_result->addMessage($result->getMessage());
00373             $o_result->setSuccess(false);
00374         }
00375 
00376         return $o_result;
00377     }
00378 
00379     // }}}
00380     // {{{ removeDemoFromBorrower()
00381 
00390     function removeDemoFromBorrower($in_demoId, $in_borrowerId)
00391     {
00392         $o_result = new FF_Result();
00393         $s_stmt = $this->o_data->prepare("UPDATE $this->borrowerDemosTable SET deleted = 1 WHERE demo_id = ? AND borrower_id = ?");
00394         if (DB::isError($result = $this->o_data->execute($s_stmt, array($in_demoId, $in_borrowerId)))) {
00395             $o_result->addMessage($result->getMessage());
00396             $o_result->setSuccess(false);
00397         }
00398 
00399         return $o_result;
00400     }
00401 
00402     // }}}
00403     // {{{ getMissingDemos()
00404 
00418     function getMissingDemos($in_items, $in_borrowerId)
00419     {
00420         $s_itemGroupWhere = '';
00421         $a_items = array();
00422         foreach ($in_items as $s_key => $a_item) {
00423             $s_itemGroupWhere .= sprintf(' OR di.item_id = %s', $a_item['item_id']);
00424             if (!$a_item['is_group']) {
00425                 $a_items[] = $a_item['item_id'];
00426             }
00427 
00428             // Key it for fast searching later on
00429             unset($in_items[$s_key]);
00430             $in_items[$a_item['item_id']] = $a_item;
00431         }
00432 
00433         if (count($a_items) > 0) {
00434             $o_groupsDataAccess =& FF_DataAccess::factory('Group');
00435             $a_groups = $o_groupsDataAccess->getGroupsForItems($a_items);
00436             foreach ($a_groups as $s_groupId => $s_itemId) {
00437                 $s_itemGroupWhere .= sprintf(' OR di.item_id = %s', $s_groupId);
00438                 // Copy over the info about the item that found this group
00439                 $in_items[$s_groupId] = $in_items[$s_itemId];
00440             }
00441         }
00442 
00443         $s_query = "SELECT DISTINCT(d.id), d.name, di.item_id
00444                     FROM $this->table AS d
00445                     INNER JOIN $this->demoItemsTable AS di ON d.id = di.demo_id
00446                     LEFT JOIN $this->borrowerDemosTable AS b ON b.demo_id = d.id  AND b.deleted = 0 AND b.borrower_id = ?
00447                     WHERE (b.borrower_id IS NULL) AND d.deleted=0 AND ( 0 $s_itemGroupWhere)";
00448         $a_demos = $this->o_data->getAll($s_query, array($in_borrowerId));
00449         // Add additional information about the item
00450         foreach ($a_demos as $s_key => $a_demo) {
00451             if (isset($in_items[$a_demo['item_id']])) {
00452                 $a_demos[$s_key]['is_group'] = $in_items[$a_demo['item_id']]['is_group'];
00453                 $a_demos[$s_key]['item_name'] = $in_items[$a_demo['item_id']]['name'];
00454             }
00455         }
00456 
00457         return $a_demos;
00458     }
00459 
00460     // }}}
00461 }
00462 ?>

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