DataAccess/mysql/Reddot.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('REDDOT_FILTER_BORROWER', 1);
00033 
00034 // }}}
00035 // {{{ class FF_DataAccess_Reddot_mysql 
00036 
00047 // }}}
00048 class FF_DataAccess_Reddot_mysql extends FF_DataAccess {
00049     // {{{ properties
00050 
00055     var $o_log;
00056 
00061     var $borrowersTable;
00062 
00067     var $filterName;
00068 
00069     // }}}
00070     // {{{ constructor
00071 
00078     function FF_DataAccess_Reddot_mysql()
00079     {
00080         FF_DataAccess::FF_DataAccess();
00081         $this->table = $this->o_registry->getConfigParam('data/reddots_table');
00082         $this->o_log =& new FF_Model_Log();
00083     }
00084 
00085     // }}}
00086     // {{{ update()
00087 
00096     function update($in_data)
00097     {
00098         unset($in_data['create_date']);
00099         unset($in_data['creator_id']);
00100         unset($in_data['borrower_id']);
00101         unset($in_data['cancelor_id']);
00102         unset($in_data['cancel_reason']);
00103         unset($in_data['cancel_date']);
00104         $o_result =& parent::update($in_data);
00105         if ($o_result->isSuccess()) {
00106             $tmp_result =& $this->o_log->newLogEntry('UPDATE', $in_data['id'], $this->table);
00107         }
00108 
00109         return $o_result;
00110     }
00111 
00112     // }}}
00113     // {{{ add()
00114 
00123     function add($in_data)
00124     {
00125         $in_data['create_date'] = $this->timestampToISODate($in_data['create_date']);
00126         unset($in_data['cancelor_id']);
00127         unset($in_data['cancel_reason']);
00128         unset($in_data['cancel_date']);
00129         $o_result =& parent::add($in_data);
00130         if ($o_result->isSuccess()) {
00131             $tmp_result =& $this->o_log->newLogEntry('INSERT', $in_data['id'], $this->table);
00132         }
00133 
00134         return $o_result;
00135     }
00136 
00137     // }}}
00138     // {{{ cancel()
00139 
00148     function cancel($in_data)
00149     {
00150         $o_result = new FF_Result();
00151         $s_where = 'id=' . $this->o_data->quoteSmart($in_data['id']);
00152         $a_data['cancel_reason'] = $in_data['cancel_reason'];
00153         $a_data['cancel_date'] = $this->timestampToISODate($in_data['cancel_date']);
00154         $a_data['cancelor_id'] = $in_data['cancelor_id'];
00155         $result = $this->o_data->autoExecute($this->table, $a_data, DB_AUTOQUERY_UPDATE, $s_where);
00156         if (DB::isError($result)) {
00157             $o_result->addMessage($result->getMessage());
00158             $o_result->setSuccess(false);
00159         }
00160         else {
00161             $tmp_result =& $this->o_log->newLogEntry('DELETE', $in_data['id'], $this->table);
00162         }
00163 
00164         return $o_result;
00165     }
00166 
00167     // }}}
00168     // {{{ getDataByPrimaryKey()
00169 
00179     function getDataByPrimaryKey($in_id, $in_fields = '*')
00180     {
00181         return parent::getDataByPrimaryKey($in_id, $this->_getSelectFields());
00182     }
00183 
00184     // }}}
00185     // {{{ getListData()
00186 
00198     function getListData($in_where, $in_orderByField, $in_orderByDir, $in_fields = '*')
00199     {
00200         if ($this->filterName == REDDOT_FILTER_BORROWER) {
00201             return parent::getListData($in_where, $in_orderByField, $in_orderByDir, $this->_getSelectFields());
00202         }
00203         else {
00204             $s_borrowerTable = $this->o_registry->getConfigParam('data/borrowers_table');
00205             $s_query = "SELECT " . $this->_getSelectFields() . ", COUNT(*) AS num_active,
00206                         CONCAT(first_name, ' ', last_name) AS borrower_name
00207                         FROM $this->table
00208                         LEFT JOIN $s_borrowerTable AS b ON borrower_id = b.id
00209                         WHERE $in_where 
00210                         GROUP BY borrower_id
00211                         ORDER BY $in_orderByField " . $this->_getOrderByDirection($in_orderByDir);
00212             return $this->o_data->query($s_query);
00213         }
00214     }
00215 
00216     // }}}
00217     // {{{ getListFilter()
00218 
00231     function getListFilter($in_searchString, $in_searchFields, $in_filter, $in_filterData)
00232     {
00233         $this->filterName = $in_filter;
00234         // Filter: the reddots for this borrower only
00235         if ($in_filter == REDDOT_FILTER_BORROWER) {
00236             $s_filter = sprintf('borrower_id = %s', $this->o_data->quoteSmart($in_filterData['borrowerId']));
00237         }
00238         // Filter: all borrowers with active reddots
00239         else {
00240             $s_create = $this->timestampToISODate(time() - 
00241                             $this->o_registry->getConfigParam('reddot/active_cutoff') * 86400);
00242             $s_filter = "cancel_date = 0 AND create_date > " . $this->o_data->quoteSmart($s_create);
00243         }
00244 
00245         $s_filter .= ' AND (' . parent::getListFilter($in_searchString, $in_searchFields, $in_filter, $in_filterData) . ')';
00246         $s_filter = preg_replace('/`borrower_name` LIKE \'%(.*?)%\'/', 
00247                 'b.first_name LIKE \'\\1%\' OR b.last_name LIKE \'\\1%\'', $s_filter);
00248 
00249         return $s_filter;
00250     }
00251 
00252     // }}}
00253     // {{{ getNextId()
00254 
00261     function getNextId()
00262     {
00263         // change sequence name since we give it the full table name
00264         $this->o_data->setOption('seqname_format', '%s');
00265         return $this->o_data->nextId($this->o_registry->getConfigParam('data/sequence_table'));
00266     }
00267 
00268     // }}}
00269     // {{{ getProblemReddotsForBorrower()
00270     
00279     function getProblemReddotsForBorrower($in_borrowerId)
00280     {
00281         $s_query = "SELECT UNIX_TIMESTAMP(create_date) AS create_date, is_block
00282                     FROM $this->table 
00283                     WHERE borrower_id = ? AND cancel_date = 0 AND (is_block = 1 OR create_date > ?)
00284                     ORDER BY is_block DESC, create_date DESC";
00285         $s_create = $this->timestampToISODate(time() - 
00286                         $this->o_registry->getConfigParam('reddot/active_duration') * 86400);
00287 
00288         return $this->o_data->getAll($s_query, array($in_borrowerId, $s_create));
00289     }
00290 
00291     // }}}
00292     // {{{ getBlockingReddotCountForBorrower()
00293     
00302     function getBlockingReddotCountForBorrower($in_borrowerId)
00303     {
00304         $s_query = "SELECT COUNT(*) FROM $this->table 
00305                     WHERE borrower_id = ? AND cancel_date = 0 AND is_block = 1";
00306 
00307         return $this->o_data->getOne($s_query, array($in_borrowerId));
00308     }
00309 
00310     // }}}
00311     // {{{ getProblemReddotCountForBorrower()
00312     
00321     function getProblemReddotCountForBorrower($in_borrowerId)
00322     {
00323         $s_query = "SELECT COUNT(*) FROM $this->table 
00324                     WHERE borrower_id = ? AND cancel_date = 0 AND (is_block = 1 OR create_date > ?)";
00325         $s_create = $this->timestampToISODate(time() -
00326                         $this->o_registry->getConfigParam('reddot/active_duration') * 86400);
00327 
00328         return $this->o_data->getOne($s_query, array($in_borrowerId, $s_create));
00329     }
00330 
00331     // }}}
00332     // {{{ getReddotCountForBorrower()
00333     
00342     function getReddotCountForBorrower($in_borrowerId)
00343     {
00344         $s_query = "SELECT COUNT(*) FROM $this->table 
00345                     WHERE borrower_id = ? AND cancel_date = 0 and create_date > ?";
00346         $s_create = $this->timestampToISODate(time() -
00347                         $this->o_registry->getConfigParam('reddot/active_cutoff') * 86400);
00348 
00349         return $this->o_data->getOne($s_query, array($in_borrowerId, $s_create));
00350     }
00351 
00352     // }}}
00353     // {{{ _getSelectFields()
00354 
00362     function _getSelectFields()
00363     {
00364         return $this->table . '.id, description, borrower_id, creator_id, create_reason, UNIX_TIMESTAMP(create_date) AS create_date, cancelor_id, cancel_reason, UNIX_TIMESTAMP(cancel_date) AS cancel_date, is_block';
00365     }
00366 
00367     // }}}
00368 }
00369 ?>

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