<?php

class ClearAdmin extends Buyers
{
    public function fetch()
    {
        if($this->request->method('post'))
        {
            if($this->request->post('resizes', 'integer'))
            {
                $this->delTree($this->images->resized_images_dir);
                mkdir($this->images->resized_images_dir, 0755);
            }

            if($this->request->post('templates', 'integer'))
            {
                $dir = $this->design->get_compile_dir();
                foreach (glob($dir."/*") as $filename)
                    unlink($filename);
            }

            $this->request->redirect(array('module' => 'ClearAdmin', 'message_success' => 'cleared'));
        }
        return $this->design->fetch('clear.tpl');
    }

    public function delTree($dir)
    {
        $files = array_diff(scandir($dir), array('.','..'));
        foreach ($files as $file)
            is_dir($dir."/".$file) ? $this->delTree($dir."/".$file) : unlink($dir."/".$file);
        return rmdir($dir);
    }
}
