Files
pmgr/site/app_model.php
abijah 17b6986758 Fixed problem when nameToID returns no results.
git-svn-id: file:///svn-source/pmgr/branches/invoice_receipt_20090629@282 97e9348a-65ac-dc4b-aefc-98561f571b83
2009-07-09 05:27:13 +00:00

214 lines
6.6 KiB
PHP

<?php
/* SVN FILE: $Id: app_model.php 7945 2008-12-19 02:16:01Z gwoo $ */
/**
* Application model for Cake.
*
* This file is application-wide model file. You can put all
* application-wide model-related methods here.
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package cake
* @subpackage cake.app
* @since CakePHP(tm) v 0.2.9
* @version $Revision: 7945 $
* @modifiedby $LastChangedBy: gwoo $
* @lastmodified $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
* Application model for Cake.
*
* Add your application-wide methods in the class below, your models
* will inherit them.
*
* @package cake
* @subpackage cake.app
*/
class AppModel extends Model {
var $actsAs = array('Containable', 'Linkable');
var $useNullForEmpty = true;
var $formatDateFields = true;
/**
* Get Enum Values
* Snippet v0.1.3
* http://cakeforge.org/snippet/detail.php?type=snippet&id=112
*
* Gets the enum values for MySQL 4 and 5 to use in selectTag()
*/
function getEnumValues($columnName=null, $tableName=null)
{
if ($columnName==null) { return array(); } //no field specified
if (!isset($tableName)) {
//Get the name of the table
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$tableName = $db->fullTableName($this, false);
}
//Get the values for the specified column (database and version specific, needs testing)
$result = $this->query("SHOW COLUMNS FROM {$tableName} LIKE '{$columnName}'");
//figure out where in the result our Types are (this varies between mysql versions)
$types = null;
if ( isset( $result[0]['COLUMNS']['Type'] ) ) { //MySQL 5
$types = $result[0]['COLUMNS']['Type']; $default = $result[0]['COLUMNS']['Default'];
}
elseif ( isset( $result[0][0]['Type'] ) ) { //MySQL 4
$types = $result[0][0]['Type']; $default = $result[0][0]['Default'];
}
else { //types return not accounted for
return array();
}
//Get the values
return array_flip(array_merge(array(''), // MySQL sets 0 to be the empty string
explode("','", strtoupper(preg_replace("/(enum)\('(.+?)'\)/","\\2", $types)))
));
} //end getEnumValues
/**************************************************************************
**************************************************************************
**************************************************************************
* function: nameToID
* - Returns the ID of the named item
*/
function nameToID($name) {
$this->cacheQueries = true;
$item = $this->find('first', array
('recursive' => -1,
'conditions' => compact('name'),
));
$this->cacheQueries = false;
if ($item) {
$item = current($item);
return $item['id'];
}
return null;
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: statMerge
* - Merges summary data from $b into $a
*/
function statsMerge (&$a, $b) {
if (!isset($b))
return;
if (!isset($a)) {
$a = $b;
}
elseif (!is_array($a) && !is_array($b)) {
$a += $b;
}
elseif (is_array($a) && is_array($b)) {
foreach (array_intersect_key($a, $b) AS $k => $v)
{
if (preg_match("/^sp\./", $k))
$a[$k] .= '; ' . $b[$k];
else
$this->statsMerge($a[$k], $b[$k]);
}
$a = array_merge($a, array_diff_key($b, $a));
}
else {
die ("Can't yet merge array and non-array stats");
}
}
function recursive_array_replace($find, $replace, &$data) {
if (!isset($data))
return;
if (is_array($data)) {
foreach ($data as $key => &$value) {
$this->recursive_array_replace($find, $replace, $value);
}
return;
}
if (isset($replace))
$data = preg_replace($find, $replace, $data);
elseif (preg_match($find, $data))
$data = null;
}
function beforeSave() {
/* pr(array('class' => $this->name, */
/* 'alias' => $this->alias, */
/* 'function' => 'AppModel::beforeSave')); */
// Replace all empty strings with NULL.
// If a particular model doesn't like this, they'll have to
// override the behavior, or set useNullForEmpty to false.
if ($this->useNullForEmpty)
$this->recursive_array_replace("/^\s*$/", null, $this->data);
if ($this->formatDateFields) {
$alias = $this->alias;
foreach ($this->_schema AS $field => $info) {
if ($info['type'] == 'date' || $info['type'] == 'timestamp') {
if (isset($this->data[$alias][$field])) {
/* pr("Fix Date for '$alias'.'$field'; current value = " . */
/* "'{$this->data[$alias][$field]}'"); */
if ($this->data[$alias][$field] === 'CURRENT_TIMESTAMP')
// Seems CakePHP is broken for the default timestamp.
// It tries to automagically set the value to CURRENT_TIMESTAMP
// which is wholly rejected by MySQL. Just put it back to NULL
// and let the SQL engine deal with the defaults... it's not
// Cake's place to do this anyway :-/
$this->data[$alias][$field] = null;
else
$this->data[$alias][$field] =
$this->dateFormatBeforeSave($this->data[$alias][$field]);
}
}
}
}
return true;
}
/**************************************************************************
**************************************************************************
**************************************************************************
* function: dateFormatBeforeSave
* - convert dates to database format
*/
function dateFormatBeforeSave($dateString) {
/* $time = ''; */
/* if (preg_match('/(\d+(:\d+))/', $dateString, $match)) */
/* $time = ' '.$match[1]; */
/* $dateString = preg_replace('/(\d+(:\d+))/', '', $dateString); */
/* return date('Y-m-d', strtotime($dateString)) . $time; */
if (preg_match('/:/', $dateString))
return date('Y-m-d H:i:s', strtotime($dateString));
else
return date('Y-m-d', strtotime($dateString));
}
}