/** * Delete those users from the database which corresponds to the given condition or to the given ids array * Note: the delete cascade arrays are handled! * * @param string the name of this class * Note: This is required until min phpversion will be 5.3. Since PHP 5.3 we can use static::function_name to achieve late static bindings * @param string where condition * @param array object ids * @return mixed # of rows affected or false if error */ static function db_delete_where($class_name, $sql_where, $object_ids = NULL, $params = NULL) { global $DB; $DB->begin(); if (!empty($sql_where)) { $object_ids = $DB->get_col('SELECT user_ID FROM T_users WHERE ' . $sql_where); } if (!$object_ids) { // There is no user to delete $DB->commit(); return; } // Delete orphan threads per users $result = delete_orphan_threads($object_ids); // Remove deleted user(s) from posts where it was as last edit user $user_ids = implode(',', $object_ids); $result = $result && $DB->query('UPDATE T_items__item SET post_lastedit_user_ID = NULL WHERE post_lastedit_user_ID IN ( ' . $user_ids . ' )') !== false; $result = $result && $DB->query('UPDATE T_items__version SET iver_edit_user_ID = NULL WHERE iver_edit_user_ID IN ( ' . $user_ids . ' )') !== false; // Remove this user from links where it was as last edit user $result = $result && $DB->query('UPDATE T_links SET link_lastedit_user_ID = NULL WHERE link_lastedit_user_ID IN ( ' . $user_ids . ' )') !== false; if ($result) { // Delete the user(s) with all of the cascade objects $params['use_transaction'] = false; // no need to start a new transaction $result = parent::db_delete_where($class_name, $sql_where, $object_ids, $params); } if ($result !== false) { // delete the users' media folders recursively, for all deleted users $FileRootCache =& get_FileRootCache(); foreach ($object_ids as $user_ID) { if ($root_directory = $FileRootCache->get_root_dir('user', $user_ID)) { // Delete the folder only when it is detected: rmdir_r($root_directory); } } } $result !== false ? $DB->commit() : $DB->rollback(); return $result; }
/** * Delete those comments from the database which corresponds to the given condition or to the given ids array * Note: the delete cascade arrays are handled! * * @param string the name of this class * Note: This is required until min phpversion will be 5.3. Since PHP 5.3 we can use static::function_name to achieve late static bindings * @param string where condition * @param array object ids * @return mixed # of rows affected or false if error */ static function db_delete_where($class_name, $sql_where, $object_ids = NULL, $params = NULL) { global $DB; $use_transaction = isset($params['use_transaction']) ? $params['use_transaction'] : true; if ($use_transaction) { $DB->begin(); $params['use_transaction'] = false; } if (!empty($sql_where)) { $object_ids = $DB->get_col('SELECT comment_ID FROM T_comments WHERE ' . $sql_where); } if (!$object_ids) { // There is no comment to delete if ($use_transaction) { // Commit transaction if it was started $DB->commit(); } return; } $query_get_attached_file_ids = 'SELECT link_file_ID FROM T_links WHERE link_cmt_ID IN ( ' . implode(', ', $object_ids) . ' )'; $attached_file_ids = $DB->get_col($query_get_attached_file_ids); $result = parent::db_delete_where($class_name, $sql_where, $object_ids); if ($result !== false && !empty($attached_file_ids)) { // Delete orphan attachments and empty comment attachment folders load_funcs('files/model/_file.funcs.php'); remove_orphan_files($attached_file_ids, NULL, true); } if ($use_transaction) { // Commit or rollback the transaction $result !== false ? $DB->commit() : $DB->rollback(); } return $result; }
/** * Delete those messages from the database which corresponds to the given condition or to the given ids array * Note: the delete cascade arrays are handled! * * @param string the name of this class * Note: This is required until min phpversion will be 5.3. Since PHP 5.3 we can use static::function_name to achieve late static bindings * @param string where condition * @param array object ids * @return mixed # of rows affected or false if error */ static function db_delete_where($class_name, $sql_where, $object_ids = NULL, $params = NULL) { global $DB; $DB->begin(); if (!empty($sql_where)) { $messages_to_delete = $DB->get_assoc('SELECT msg_ID, msg_thread_ID FROM T_messaging__message WHERE ' . $sql_where); $object_ids = array_keys($messages_to_delete); $thread_ids_to_delete = array_unique($messages_to_delete); } if (!$object_ids) { // There is no comment to delete $DB->commit(); return; } $message_ids_to_delete = implode(', ', $object_ids); if (empty($thread_ids_to_delete)) { // Make sure thread ids of the messages are collected $thread_ids_to_delete = $DB->get_col('SELECT msg_thread_ID FROM T_messaging__message WHERE msg_ID IN ( ' . $message_ids_to_delete . ' )'); } // Update thread statuses first unread message IDs $result = $DB->query('UPDATE T_messaging__threadstatus SET tsta_first_unread_msg_ID = ( SELECT message1.msg_ID FROM T_messaging__message as message1 WHERE message1.msg_thread_ID = tsta_thread_ID AND message1.msg_datetime > ( SELECT MAX( message2.msg_datetime) FROM T_messaging__message as message2 WHERE message2.msg_ID IN ( ' . $message_ids_to_delete . ' ) AND message2.msg_thread_ID = tsta_thread_ID ) ORDER BY message1.msg_datetime ASC LIMIT 1 ) WHERE tsta_first_unread_msg_ID IN ( ' . $message_ids_to_delete . ')') !== false; if ($result) { // Remove messages with all of its delete cascade relations $result = parent::db_delete_where($class_name, $sql_where, $object_ids); } if ($result !== false) { // Delete those threads where all of the messages were deleted load_class('messaging/model/_thread.class.php', 'Thread'); $orphan_thread_ids = $DB->get_col(' SELECT msg_thread_ID FROM T_messaging__message WHERE msg_thread_ID IN ( ' . implode(', ', $thread_ids_to_delete) . ' ) GROUP BY msg_thread_ID HAVING COUNT(*) < 1'); // Delete orphan threads if there are any if (!empty($orphan_thread_ids) && Thread::db_delete_where('Thread', NULL, $orphan_thread_ids) === false) { // Deleting threads was unsuccessful $result = false; } } // Commit or rollback the transaction $result !== false ? $DB->commit() : $DB->rollback(); return $result; }