/** * Deletes a user from the database. * @param Int $userid id of the user to be deleted. * return boolean true if user was successfully deleted, false otherwise. */ public function deleteUser($userid) { // safety checks if (empty($userid)) { return false; } // start transaction because we will perform several atomic operations. $this->dbConnector->startTransaction(); // first check if we need to remove the avatar. $data = $this->getDataForUser($userid); if (isset($data["avatar"])) { $ih = new \creamy\ImageHandler(); if (!$ih->removeUserAvatar($data["avatar"])) { $this->dbConnector->rollback(); return false; } } // delete the user notifications $this->dbConnector->where("target_user", $userid); if (!$this->dbConnector->delete(CRM_NOTIFICATIONS_TABLE_NAME)) { $this->dbConnector->rollback(); return false; } // delete the user events. $this->dbConnector->where("user_id", $userid); if (!$this->dbConnector->delete(CRM_EVENTS_TABLE_NAME)) { $this->dbConnector->rollback(); return false; } // deletes the user tasks $this->dbConnector->where("user_id", $userid); if (!$this->dbConnector->delete(CRM_TASKS_TABLE_NAME)) { $this->dbConnector->rollback(); return false; } // delete the user messages. // inbox $this->dbConnector->where("user_to", $userid); if (!$this->dbConnector->delete(CRM_MESSAGES_INBOX_TABLE_NAME)) { $this->dbConnector->rollback(); return false; } // outbox $this->dbConnector->where("user_from", $userid); if (!$this->dbConnector->delete(CRM_MESSAGES_OUTBOX_TABLE_NAME)) { $this->dbConnector->rollback(); return false; } // junk $this->dbConnector->where("user_to", $userid)->where("origin_folder", CRM_MESSAGES_INBOX_TABLE_NAME); if (!$this->dbConnector->delete(CRM_MESSAGES_JUNK_TABLE_NAME)) { $this->dbConnector->rollback(); return false; } $this->dbConnector->where("user_from", $userid)->where("origin_folder", CRM_MESSAGES_OUTBOX_TABLE_NAME); if (!$this->dbConnector->delete(CRM_MESSAGES_JUNK_TABLE_NAME)) { $this->dbConnector->rollback(); return false; } // last remove the user entry at the database $this->dbConnector->where("id", $userid); $result = $this->dbConnector->delete(CRM_USERS_TABLE_NAME); if ($result === true) { $this->dbConnector->commit(); return true; } else { $this->dbConnector->rollback(); return false; } }