File indexing completed on 2024-12-22 05:36:23

0001 <?php
0002 
0003 /**
0004  *  ocs-webserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *    This file is part of ocs-webserver.
0009  *
0010  *    This program is free software: you can redistribute it and/or modify
0011  *    it under the terms of the GNU Affero General Public License as
0012  *    published by the Free Software Foundation, either version 3 of the
0013  *    License, or (at your option) any later version.
0014  *
0015  *    This program is distributed in the hope that it will be useful,
0016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  *    GNU Affero General Public License for more details.
0019  *
0020  *    You should have received a copy of the GNU Affero General Public License
0021  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022  **/
0023 class Local_Model_Table extends Zend_Db_Table
0024 {
0025 
0026     protected $_key = null;
0027     protected $_keyColumnsForRow = array();
0028 
0029 
0030     /**
0031      * @param $data
0032      *
0033      * @throws Exception
0034      * @return Zend_Db_Table_Row_Abstract
0035      */
0036     public function save($data)
0037     {
0038         if (empty($this->_keyColumnsForRow)) {
0039             throw new Exception(__METHOD__ . ' - no _keyColumnsForRow were set');
0040         }
0041         $rowSet = $this->findForKeyColumns($data, $this->_keyColumnsForRow);
0042 
0043         if (null === $rowSet) {
0044             $rowSet = $this->createRow($data);
0045         } else {
0046             $rowSet->setFromArray($data);
0047         }
0048 
0049         $rowSet->save();
0050 
0051         return $rowSet;
0052     }
0053 
0054     /**
0055      * @param array        $data
0056      * @param string|array $keyColumns
0057      *
0058      * @return null|Zend_Db_Table_Row_Abstract
0059      */
0060     public function findForKeyColumns($data, $keyColumns)
0061     {
0062         if (false === is_array($keyColumns)) {
0063             $keyColumns = array($keyColumns);
0064         }
0065         if (0 < count(array_diff($keyColumns, array_keys($data)))) {
0066             // if data doesn't contain any key column we can stop here
0067             return null;
0068         }
0069         $statement = $this->select()->setIntegrityCheck(false)->from($this->_name);
0070         foreach ($keyColumns as $identifier) {
0071             if (null === $data[$identifier]) {
0072                 $statement->where($this->_db->quoteIdentifier($identifier) . ' IS NULL');
0073             } else {
0074                 $statement->where($this->_db->quoteIdentifier($identifier) . ' = ?', $data[$identifier]);
0075             }
0076         }
0077 
0078         return $this->fetchRow($statement);
0079     }
0080 
0081     /**
0082      * @param $data
0083      *
0084      * @throws Exception
0085      * @return mixed
0086      */
0087     public function saveByKey($data)
0088     {
0089         if (empty($this->_key)) {
0090             throw new Exception('no _key was set');
0091         }
0092         $rowSet = $this->find($data[$this->_key])->current();
0093 
0094         if (null === $rowSet) {
0095             $rowSet = $this->createRow($data);
0096         } else {
0097             $rowSet->setFromArray($data);
0098         }
0099 
0100         return $rowSet->save();
0101     }
0102 
0103     public function findSingleRow()
0104     {
0105         $rowset = $this->find($args = func_get_args());
0106         if (0 < $rowset->count()) {
0107             return $rowset->current();
0108         } else {
0109             return $this->createRow(array(), self::DEFAULT_CLASS);
0110         }
0111     }
0112 
0113     /**
0114      * @param $data
0115      *
0116      * @return Zend_Db_Table_Row_Abstract
0117      * @throws Zend_Exception
0118      */
0119     public function getRow($data)
0120     {
0121         if (false == is_array($data)) {
0122             throw new Zend_Exception('given data must be an array');
0123         }
0124 
0125         return $this->generateRowClass($data);
0126     }
0127 
0128     /**
0129      * @param array $data
0130      *
0131      * @return Zend_Db_Table_Row_Abstract
0132      */
0133     protected function generateRowClass($data)
0134     {
0135         if (empty($data)) {
0136             return $this->createRow();
0137         }
0138 
0139         /** @var Zend_Db_Table_Row $classRow */
0140         $classRow = $this->getRowClass();
0141 
0142         return new $classRow(array('table' => $this, 'stored' => true, 'data' => $data));
0143     }
0144 
0145     /**
0146      * Convert an array, string, or Zend_Db_Expr object
0147      * into a string to put in a WHERE clause.
0148      *
0149      * @param mixed $where
0150      * @return string
0151      */
0152     protected function _whereExpr($where)
0153     {
0154         if (empty($where)) {
0155             return $where;
0156         }
0157         if (!is_array($where)) {
0158             $where = array($where);
0159         }
0160         foreach ($where as $cond => &$term) {
0161             // is $cond an int? (i.e. Not a condition)
0162             if (is_int($cond)) {
0163                 // $term is the full condition
0164                 if ($term instanceof Zend_Db_Expr) {
0165                     $term = $term->__toString();
0166                 }
0167             } else {
0168                 // $cond is the condition with placeholder,
0169                 // and $term is quoted into the condition
0170                 $term = $this->getAdapter()->quoteInto($cond, $term);
0171             }
0172             $term = '(' . $term . ')';
0173         }
0174 
0175         $where = implode(' AND ', $where);
0176         return $where;
0177     }
0178 
0179 }