File indexing completed on 2024-05-12 05:58:46

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  *    Created: 22.09.2016
0024  **/
0025 class Default_Model_MemberDeactivationLog extends Default_Model_DbTable_MemberDeactivationLog
0026 {
0027     const OBJ_TYPE_OPENDESKTOP_MEMBER = 1;
0028     const OBJ_TYPE_OPENDESKTOP_MEMBER_EMAIL = 2;
0029     const OBJ_TYPE_OPENDESKTOP_PROJECT = 3;
0030     const OBJ_TYPE_OPENDESKTOP_COMMENT = 4;
0031 
0032     const OBJ_TYPE_GITLAB_USER = 20;
0033     const OBJ_TYPE_GITLAB_PROJECT = 21;
0034 
0035     const OBJ_TYPE_DISCOURSE_USER = 30;
0036     const OBJ_TYPE_DISCOURSE_POST = 31;
0037     const OBJ_TYPE_DISCOURSE_TOPIC = 32;
0038 
0039 
0040     /**
0041      * @param int $identifer
0042      *
0043      * @return void
0044      * @throws Zend_Exception
0045      */
0046     public function logMemberAsDeleted($identifer)
0047     {
0048         return $this->addLog($identifer, Default_Model_MemberDeactivationLog::OBJ_TYPE_OPENDESKTOP_MEMBER, $identifer);
0049     }
0050 
0051     /**
0052      * @param int $member_id
0053      * @param int $object_type
0054      * @param int $identifier object id
0055      *
0056      * @return void
0057      * @throws Zend_Exception
0058      */
0059     public function addLog($member_id, $object_type, $identifier)
0060     {
0061         $identity = Zend_Auth::getInstance()->getIdentity()->member_id;
0062 
0063         $sql =
0064             "INSERT INTO `member_deactivation_log` (`deactivation_id`,`object_type_id`,`object_id`,`member_id`) VALUES (:deactivation_id,:object_type_id,:object_id,:member_id)";
0065 
0066         try {
0067             Zend_Db_Table::getDefaultAdapter()->query($sql, array(
0068                 'deactivation_id' => $member_id,
0069                 'object_type_id'  => $object_type,
0070                 'object_id'       => $identifier,
0071                 'member_id'       => $identity
0072             ))
0073             ;
0074         } catch (Exception $e) {
0075             Zend_Registry::get('logger')->err(__METHOD__ . ' - ERROR write member deactivation log - ' . print_r($e, true));
0076         }
0077     }
0078 
0079     /**
0080      * @param int $member_id
0081      * @param int $identifer
0082      *
0083      * @return void
0084      * @throws Zend_Exception
0085      */
0086     public function logMemberEmailAsDeleted($member_id, $identifer)
0087     {
0088         return $this->addLog($member_id, Default_Model_MemberDeactivationLog::OBJ_TYPE_OPENDESKTOP_MEMBER_EMAIL, $identifer);
0089     }
0090 
0091     /**
0092      * @param int $member_id
0093      * @param int $identifer
0094      *
0095      * @return void
0096      * @throws Zend_Exception
0097      */
0098     public function logProjectAsDeleted($member_id, $identifer)
0099     {
0100         return $this->addLog($member_id, Default_Model_MemberDeactivationLog::OBJ_TYPE_OPENDESKTOP_PROJECT, $identifer);
0101     }
0102 
0103     /**
0104      * @param     $member_id
0105      * @param int $identifer
0106      *
0107      * @return void
0108      * @throws Zend_Exception
0109      */
0110     public function logCommentAsDeleted($member_id, $identifer)
0111     {
0112         return $this->addLog($member_id, Default_Model_MemberDeactivationLog::OBJ_TYPE_OPENDESKTOP_COMMENT, $identifer);
0113     }
0114 
0115     /**
0116      * @param int $member_id
0117      * @param int $object_type
0118      * @param int $identifier
0119      * @param string $data
0120      *
0121      * @throws Zend_Exception
0122      */
0123     public function addLogData($member_id, $object_type, $identifier, $data)
0124     {
0125         $identity = Zend_Auth::getInstance()->getIdentity()->member_id;
0126 
0127         $sql =
0128             "INSERT INTO `member_deactivation_log` (`deactivation_id`,`object_type_id`,`object_id`,`member_id`, `object_data`) VALUES (:deactivation_id,:object_type_id,:object_id,:member_id,:object_data)";
0129 
0130         try {
0131             Zend_Db_Table::getDefaultAdapter()->query($sql, array(
0132                 'deactivation_id' => $member_id,
0133                 'object_type_id'  => $object_type,
0134                 'object_id'       => $identifier,
0135                 'member_id'       => $identity,
0136                 'object_data'     => $data
0137             ))
0138             ;
0139         } catch (Exception $e) {
0140             Zend_Registry::get('logger')->err(__METHOD__ . ' - ERROR write member deactivation log - ' . print_r($e, true));
0141         }
0142     }
0143 
0144     /**
0145      * @param int $identifer
0146      *
0147      * @return void
0148      * @throws Zend_Exception
0149      */
0150     public function removeLogMemberAsDeleted($identifer)
0151     {
0152         return $this->deleteLog($identifer, Default_Model_MemberDeactivationLog::OBJ_TYPE_OPENDESKTOP_MEMBER, $identifer);
0153     }
0154 
0155     /**
0156      * @param int $member_id
0157      * @param int $object_type
0158      * @param int $identifer object id
0159      *
0160      * @return void
0161      * @throws Zend_Exception
0162      */
0163     public function deleteLog($member_id, $object_type, $identifer)
0164     {
0165         $identity = Zend_Auth::getInstance()->getIdentity()->member_id;
0166 
0167         $sql =
0168             "UPDATE `member_deactivation_log` SET is_deleted = 1, deleted_at = NOW() WHERE  deactivation_id = :deactivation_id AND object_type_id = :object_type_id AND object_id = :object_id";
0169 
0170         try {
0171             Zend_Db_Table::getDefaultAdapter()->query($sql,
0172                 array('deactivation_id' => $member_id, 'object_type_id' => $object_type, 'object_id' => $identifer))
0173             ;
0174         } catch (Exception $e) {
0175             Zend_Registry::get('logger')->err(__METHOD__ . ' - ERROR write member deactivation log - ' . print_r($e, true));
0176         }
0177     }
0178 
0179     /**
0180      * @param int $member_id
0181      * @param int $identifer
0182      *
0183      * @return void
0184      * @throws Zend_Exception
0185      */
0186     public function removeLogMemberEmailAsDeleted($member_id, $identifer)
0187     {
0188         return $this->deleteLog($member_id, Default_Model_MemberDeactivationLog::OBJ_TYPE_OPENDESKTOP_MEMBER_EMAIL, $identifer);
0189     }
0190 
0191     /**
0192      * @param int $member_id
0193      * @param int $identifer
0194      *
0195      * @return void
0196      * @throws Zend_Exception
0197      */
0198     public function removeLogProjectAsDeleted($member_id, $identifer)
0199     {
0200         return $this->deleteLog($member_id, Default_Model_MemberDeactivationLog::OBJ_TYPE_OPENDESKTOP_PROJECT, $identifer);
0201     }
0202 
0203     /**
0204      * @param int $member_id
0205      * @param int $identifer
0206      *
0207      * @return void
0208      * @throws Zend_Exception
0209      */
0210     public function removeLogCommentAsDeleted($member_id, $identifer)
0211     {
0212         return $this->deleteLog($member_id, Default_Model_MemberDeactivationLog::OBJ_TYPE_OPENDESKTOP_COMMENT, $identifer);
0213     }
0214 
0215     public function getLogEntries($member_id, $obj_type, $id)
0216     {
0217         $sql = "SELECT * FROM member_deactivation_log WHERE deactivation_id = :memberid AND object_type_id = :objecttype AND object_id = :objectid AND is_deleted = 0";
0218         $result = array();
0219 
0220         try {
0221             $result = Zend_Db_Table::getDefaultAdapter()->fetchRow($sql, array('memberid' => $member_id, 'objecttype' => $obj_type, 'objectid' => $id));
0222         } catch (Exception $e) {
0223             Zend_Registry::get('logger')->err(__METHOD__ . ' - ERROR READ member deactivation log - ' . print_r($e, true));
0224         }
0225 
0226         return $result;
0227     }
0228 
0229     /**
0230      * @param int $member_id
0231      *
0232      * @return array
0233      * @throws Zend_Exception
0234      */
0235     public function getLogForumPosts($member_id)
0236     {
0237         $sql = "SELECT * FROM member_deactivation_log WHERE deactivation_id = :memberid AND (object_type_id = ".self::OBJ_TYPE_DISCOURSE_TOPIC." OR object_type_id = ".self::OBJ_TYPE_DISCOURSE_POST.") AND is_deleted = 0";
0238         $result = array();
0239 
0240         try {
0241             $result = Zend_Db_Table::getDefaultAdapter()->fetchAll($sql, array('memberid' => $member_id));
0242         } catch (Exception $e) {
0243             Zend_Registry::get('logger')->err(__METHOD__ . ' - ERROR READ member deactivation log - ' . print_r($e, true));
0244         }
0245 
0246         $posts = array();
0247 
0248         foreach ($result as $item) {
0249             if (self::OBJ_TYPE_DISCOURSE_TOPIC == $item['object_type_id']) {
0250                 $posts['topics'][$item['object_id']] = $item;
0251             }
0252             if (self::OBJ_TYPE_DISCOURSE_POST == $item['object_type_id']) {
0253                 $posts['posts'][$item['object_id']] = $item;
0254             }
0255         }
0256 
0257         return $posts;
0258     }
0259 
0260 }