From: <pan...@us...> - 2008-09-29 13:40:02
|
Revision: 395 http://acmcontester.svn.sourceforge.net/acmcontester/?rev=395&view=rev Author: panzaboi Date: 2008-09-29 13:39:57 +0000 (Mon, 29 Sep 2008) Log Message: ----------- update Modified Paths: -------------- website/library/Ostacium/Db/CrudTable.php Modified: website/library/Ostacium/Db/CrudTable.php =================================================================== --- website/library/Ostacium/Db/CrudTable.php 2008-09-29 13:39:49 UTC (rev 394) +++ website/library/Ostacium/Db/CrudTable.php 2008-09-29 13:39:57 UTC (rev 395) @@ -31,25 +31,47 @@ return self::$perpage; } - public function getSelect($table, $columns) + public function getSelect($table, $columns, $where = null, $notIn = null) { - $index = md5(serialize($table) . serialize($columns)); + $index = md5(serialize($table) . serialize($columns) . serialize($where) . serialize($notIn)); if (isset($_select[$index])) { return $_select[$index]; } $select = $this->select()->setIntegrityCheck(false)->from($table, $columns); + + if ($where) + $select = $select->where($where); + + if ($notIn) + $select = $select->where($this->notIn($notIn['table'], $notIn['column1'], $notIn['column2'])); + $_select[$index] = $this->fetchAll($select)->toArray(); return $_select[$index]; } + + public function notIn($table, $column1, $column2) + { + return ($column1 . ' NOT IN (SELECT ' . $column2 . ' FROM ' . $table . ')'); + } public function createData($values) { - $values = $this->_cleanInput($values, array_keys($this->_options['createFields'])); + try + { + $values = $this->_cleanInput($values, array_keys($this->_options['createFields'])); - return $this->insert($values); + return $this->insert($values); + } + catch(Exception $e) + { + if (stripos($e->getMessage(), 'Duplicate entry')) + return false; + else + throw $e; + } } public function getData($id) @@ -62,8 +84,13 @@ public function updateData($values, $id) { $db_values = $this->_cleanInput($this->get($id), array_keys($this->_options['updateFields'])); + $values = $this->_cleanInput($values, array_keys($db_values)); + $values = array_diff_assoc($values, $db_values); + + if (count($values) < 1) return true; + try { return (bool)$this->update($values, $this->getAdapter()->quoteInto(current($this->_primary) . ' = ?', $id)); @@ -92,6 +119,11 @@ throw $e; } } + + public function markDelete($id) + { + return (bool)$this->update(array('deleted' => 1), $this->getAdapter()->quoteInto(current($this->_primary) . ' = ?', $id)); + } public function retrieve($page, $sort) { @@ -108,9 +140,20 @@ $data = $this->fetchAll($select, key($sort).' '.current($sort), self::$perpage, self::$perpage * $page); - return $data->toArray(); + return $data->toArray(true); } + public function getCountries() + { + $locale = new Zend_Locale(Zend_Registry::get('Zend_Translate')->getAdapter()->getLocale()); + + $list = $locale->getTranslationList('Territory', null, 2); + + unset($list['ZZ']); + + return $list; + } + public function up($id) { $position = $this->select()->from($this->_name, 'position')->where(current($this->_primary) . ' = ?', $id)->query()->fetchColumn(); @@ -146,15 +189,16 @@ } } - protected function _cleanInput($data, $fields) + protected function _cleanInput($data, $fields, $flip = true) { if (!$fields || !is_array($fields)) return $data; if (!$data || !is_array($data) ) return $data; - $data = array_intersect_key($data, array_flip($fields)); + if ($flip) + $data = array_intersect_key($data, array_flip($fields)); + else + $data = array_intersect_key($data, $fields); return $data; } -} - -?> \ No newline at end of file +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |