/** * 名簿の削除 * * @param array $queries * @param array $options * * @return resource */ function delete_members($queries, $options = array()) { $queries = db_placeholder($queries); $options = array('softdelete' => isset($options['softdelete']) ? $options['softdelete'] : true, 'category' => isset($options['category']) ? $options['category'] : false, 'file' => isset($options['file']) ? $options['file'] : false); // 削除するデータのIDを取得 $members = db_select(array('select' => 'id', 'from' => DATABASE_PREFIX . 'members AS members', 'where' => isset($queries['where']) ? $queries['where'] : '', 'limit' => isset($queries['limit']) ? $queries['limit'] : '')); $deletes = array(); foreach ($members as $member) { $deletes[] = intval($member['id']); } if ($options['softdelete'] === true) { // データを編集 $resource = db_update(array('update' => DATABASE_PREFIX . 'members AS members', 'set' => array('deleted' => localdate('Y-m-d H:i:s')), 'where' => isset($queries['where']) ? $queries['where'] : '', 'limit' => isset($queries['limit']) ? $queries['limit'] : '')); if (!$resource) { return $resource; } } else { // データを削除 $resource = db_delete(array('delete_from' => DATABASE_PREFIX . 'members AS members', 'where' => isset($queries['where']) ? $queries['where'] : '', 'limit' => isset($queries['limit']) ? $queries['limit'] : '')); if (!$resource) { return $resource; } } if ($options['category'] === true) { // 関連する分類を削除 $resource = delete_category_sets(array('where' => 'member_id IN(' . implode(',', array_map('db_escape', $deletes)) . ')')); if (!$resource) { return $resource; } } if ($options['file'] === true) { // 関連するファイルを削除 foreach ($deletes as $delete) { directory_rmdir($GLOBALS['config']['file_targets']['member'] . $delete . '/'); } } return $resource; }
/** * Removes directory * * @param string $path * @param bool $recursive * * @return bool */ function directory_rmdir($path, $recursive = true) { if (!is_dir($path)) { return true; } $flag = false; if ($dh = opendir($path)) { while (($entry = readdir($dh)) !== false) { if ($entry === '.' || $entry === '..') { continue; } if (is_dir($path . $entry)) { if ($recursive && !directory_rmdir($path . $entry . '/')) { return false; } $flag = true; } elseif (is_file($path . $entry)) { if (!unlink($path . $entry)) { return false; } } } } else { return false; } if (!$recursive && $flag) { return true; } if (rmdir($path)) { return true; } else { return false; } }
/** * 教室の削除 * * @param array $queries * @param array $options * * @return resource */ function delete_classes($queries, $options = array()) { $queries = db_placeholder($queries); $options = array('softdelete' => isset($options['softdelete']) ? $options['softdelete'] : true, 'associate' => isset($options['associate']) ? $options['associate'] : false, 'file' => isset($options['file']) ? $options['file'] : false); // 削除するデータのIDを取得 $classes = db_select(array('select' => 'id', 'from' => DATABASE_PREFIX . 'classes AS classes', 'where' => isset($queries['where']) ? $queries['where'] : '', 'limit' => isset($queries['limit']) ? $queries['limit'] : '')); $deletes = array(); foreach ($classes as $class) { $deletes[] = intval($class['id']); } if ($options['associate'] === true) { // 関連するデータを削除 $resource = delete_members(array('where' => 'class_id IN(' . implode($deletes) . ')')); if (!$resource) { return $resource; } } if ($options['softdelete'] === true) { // データを編集 $resource = db_update(array('update' => DATABASE_PREFIX . 'classes AS classes', 'set' => array('deleted' => localdate('Y-m-d H:i:s'), 'code' => array('CONCAT(\'DELETED ' . localdate('YmdHis') . ' \', code)')), 'where' => isset($queries['where']) ? $queries['where'] : '', 'limit' => isset($queries['limit']) ? $queries['limit'] : '')); if (!$resource) { return $resource; } } else { // データを削除 $resource = db_delete(array('delete_from' => DATABASE_PREFIX . 'classes AS classes', 'where' => isset($queries['where']) ? $queries['where'] : '', 'limit' => isset($queries['limit']) ? $queries['limit'] : '')); if (!$resource) { return $resource; } } if ($options['file'] === true) { // 関連するファイルを削除 foreach ($deletes as $delete) { directory_rmdir($GLOBALS['config']['file_targets']['class'] . $delete . '/'); } } return $resource; }