File indexing completed on 2025-02-09 07:14:34

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