00001 <?php
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 require_once FASTFRAME_ROOT . 'lib/FastFrame/DataAccess.php';
00027 require_once dirname(__FILE__) . '/../../Model/Log.php';
00028
00029
00030
00031
00032 define('REDDOT_FILTER_BORROWER', 1);
00033
00034
00035
00036
00047
00048 class FF_DataAccess_Reddot_mysql extends FF_DataAccess {
00049
00050
00055 var $o_log;
00056
00061 var $borrowersTable;
00062
00067 var $filterName;
00068
00069
00070
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
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
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
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
00169
00179 function getDataByPrimaryKey($in_id, $in_fields = '*')
00180 {
00181 return parent::getDataByPrimaryKey($in_id, $this->_getSelectFields());
00182 }
00183
00184
00185
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
00218
00231 function getListFilter($in_searchString, $in_searchFields, $in_filter, $in_filterData)
00232 {
00233 $this->filterName = $in_filter;
00234
00235 if ($in_filter == REDDOT_FILTER_BORROWER) {
00236 $s_filter = sprintf('borrower_id = %s', $this->o_data->quoteSmart($in_filterData['borrowerId']));
00237 }
00238
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
00254
00261 function getNextId()
00262 {
00263
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
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
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
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
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
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 ?>