File indexing completed on 2024-04-28 05:53:54

0001 <?php
0002 
0003 /**
0004  *  ocs-apiserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *    This file is part of ocs-apiserver.
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 Application_Model_DbTable_Project extends Local_Model_Table
0024 {
0025 
0026     const PROJECT_TYPE_PERSONAL = 0;
0027     const PROJECT_TYPE_STANDARD = 1;
0028     const PROJECT_TYPE_UPDATE = 2;
0029     const PROJECT_FAULTY = 0;       // project data contains errors
0030     const PROJECT_INCOMPLETE = 10;  // process for adding the product was not successfully completed
0031     const PROJECT_ILLEGAL = 20;     // project data is complete, but the project doesn't accord to our rules
0032     const PROJECT_DELETED = 30;     // owner or staff deleted the product
0033     const PROJECT_INACTIVE = 40;    // project is not visible to the world, but for the owner and staff
0034     const PROJECT_ACTIVE = 100;     // project is active and visible to the world
0035     const PROJECT_CLAIMED = 1;
0036     const PROJECT_CLAIMABLE = 1;
0037     const PROJECT_DEFAULT = null;
0038     const MYSQL_DATE_FORMAT = "Y-m-d H:i:s";
0039     const PROJECT_SPAM_CHECKED = 1;
0040     const PROJECT_SPAM_UNCHECKED = 0;
0041 
0042     protected $_keyColumnsForRow = array('project_id');
0043     protected $_key = 'project_id';
0044     protected $_name = "project";
0045     protected $_rowClass = 'Application_Model_DbRow_Project';
0046 
0047     protected $_referenceMap = array(
0048         'Owner'       => array(
0049             'columns'       => 'member_id',
0050             'refTableClass' => 'Application_Model_DbTable_Member',
0051             'refColumns'    => 'member_id'
0052         ),
0053         'Category'    => array(
0054             'columns'       => 'project_category_id',
0055             'refTableClass' => 'Application_Model_DbTable_ProjectCategory',
0056             'refColumns'    => 'project_category_id'
0057         ),
0058         'MainProject' => array(
0059             'columns'       => 'project_id',
0060             'refTableClass' => 'Application_Model_Member',
0061             'refColumns'    => 'main_project_id'
0062         )
0063     );
0064 
0065     protected $_types = array(
0066         'person'     => self::PROJECT_TYPE_PERSONAL,
0067         'collection' => self::PROJECT_TYPE_STANDARD,
0068         'item'       => self::PROJECT_TYPE_UPDATE
0069     );
0070 
0071     protected $_allowedStatusTypes = array(
0072         self::PROJECT_FAULTY,
0073         self::PROJECT_INCOMPLETE,
0074         self::PROJECT_ILLEGAL,
0075         self::PROJECT_INACTIVE,
0076         self::PROJECT_ACTIVE,
0077         self::PROJECT_DELETED
0078     );
0079 
0080     /**
0081      * Override the insert method.
0082      *
0083      * @see Zend_Db_Table_Abstract::insert()
0084      *
0085      * @param array $data
0086      *
0087      * @return mixed
0088      */
0089     public function insert(array $data)
0090     {
0091         //Insert
0092         if (!isset($data['description'])) {
0093             $data['description'] = null;
0094         }
0095 
0096         if (!isset($data['title'])) {
0097             $data['title'] = null;
0098         }
0099 
0100         if (!isset($data['image_small'])) {
0101             $data['image_small'] = null;
0102         }
0103 
0104         if (!isset($data['project_category_id'])) {
0105             if ($data['type_id'] == 2) {
0106                 // Find parent...
0107                 $parent = $this->getParent($data['pid']);
0108                 $data['project_category_id'] = $parent['project_category_id'];
0109             }
0110         }
0111 
0112         return parent::insert($data);
0113     }
0114 
0115     public function getParent($pid)
0116     {
0117         $parent = $this->select()->where('project_id = ?', $pid)->query()->fetchAll();
0118         if (!empty($parent)) {
0119             return $parent[0];
0120         } else {
0121             return false;
0122         }
0123     }
0124 
0125     public function setSpamChecked($projectId, $spamChecked = 1)
0126     {
0127         $sql = "update {$this->_name} set spam_checked = :spam_checked where project_id = :project_id";
0128         $this->_db->query($sql, array('spam_checked' => $spamChecked, 'project_id' => $projectId))->execute();
0129     }
0130 
0131     /**
0132      * @param array $affectedRows
0133      *
0134      * @return bool
0135      */
0136     public function deleteLikes($affectedRows)
0137     {
0138         if (false === is_array($affectedRows) OR (count($affectedRows) == 0)) {
0139             return false;
0140         }
0141 
0142         foreach ($affectedRows as $affected_row) {
0143             $projectData = $this->fetchRow(array('project_id = ?' => $affected_row['project_id']));
0144             if ($affected_row['user_like']) {
0145                 $projectData->count_likes -= 1;
0146             }
0147             if ($affected_row['user_dislike']) {
0148                 $projectData->count_dislikes -= 1;
0149             }
0150             $projectData->save();
0151         }
0152 
0153         return true;
0154     }
0155 
0156 }