<?php
class CategoriesController extends AppController {

  var $name = 'Categories';
  var $uses = array('Category', 'Article');
  var $helpers = array('Html', 'Form', 'Tree', 'Javascript', 'Fck', 'ApCategoryList');

  function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('view');
  }

  function view($id = null) {
    $this->Category->recursive = -1;
    $root_category = $this->Category->findByParent_Id('0');
    $this->set('heading_title', $root_category['Category']['title']);
    if (!$id) {
      $this->pageTitle = $root_category['Category']['title'];
      $this->set('meta_description', '');
      $this->set('meta_keywords', '');
      $cid = $root_category['Category']['id'];
      $error404 = true;
    } else {
      if (!$current_category = $this->Category->findById($id)) {
        $this->pageTitle = $root_category['Category']['title'];
        $this->set('meta_description', '');
        $this->set('meta_keywords', '');
        $cid = $root_category['Category']['id'];
        $error404 = true;
      } else {
        $this->set('current', $current_category);
        $this->pageTitle = $root_category['Category']['title'] . ' : ' . $current_category['Category']['title'];
        $this->set('meta_description', $current_category['Category']['meta_description']);
        $this->set('meta_keywords', $current_category['Category']['meta_keywords']);
        $cid = $id;
        $error404 = false;
      }
    }
    $parent_category = $this->Category->getpath($cid);
    $conditions_array = array();
    if (!empty($parent_category)) {
      foreach ($parent_category as $val) {
        $conditions_array[] = 'parent_id = ' . $val['Category']['id'];
      }
    }
    $conditions = implode(' or ', $conditions_array);
    $cTree = $this->Category->find('all', array('conditions' => $conditions, 'order' => 'lft ASC', 'recursive' => -1));
    if (!empty($cTree)) {
      foreach ($cTree as $key => $val) {
        if ($val['Category']['id'] == $id) {
          $cTree[$key]['Category']['current'] = true;
        }
      }
    }
    $this->set('cTree', $cTree);
    if ($error404) {
      header("HTTP/1.1 404 Not Found");
      $this->render('view404');
    } else {
      $category = $this->Category->find('all', array('order' => 'lft ASC', 'recursive' => -1));
      $this->set('category', $category);
      $this->Article->recursive = -1;
      $this->paginate = array('conditions' => 'Article.status > 0 and Article.category_id = ' . $id, 'sort' => 'Article.created desc', 'limit' => 10);
      $this->set('articles', $this->paginate('Article'));
    }
  }

  function admin_index() {
    $this->pageTitle = '管理画面：カテゴリ一覧';
    $this->layout = 'admin';
    $this->Category->recursive = -1;
    if (!$this->Category->findByParent_Id('0')) {
      $this->redirect(array('action' => 'admin_init'));
    }
    $cTree = $this->Category->find('all', array('order' => 'lft ASC', 'recursive' => -1));
    $this->set('cTree', $cTree);
  }

  function admin_view($id = null) {
    $this->pageTitle = '管理画面：カテゴリ情報';
    $this->layout = 'admin';
    if (!$id) {
      $this->flash( 'カテゴリが選択されていません。', 'error' );
      $this->redirect(array('action' => 'admin_index'));
    }
    $this->Category->recursive = -1;
    $category = $this->Category->read(null, $id);
    $this->set('category', $category);

    if (!empty($category['Category']['parent_id'])) {
      $parent_category = $this->Category->read(null, $category['Category']['parent_id']);
      $this->set('parent_title', $parent_category['Category']['title']);
    } else {
      $this->set('parent_title', '');
    }
  }

  function admin_add($parent_id = null) {
    $this->pageTitle = '管理画面：カテゴリの追加';
    $this->layout = 'admin';
    if (!empty($this->data)) {
      $this->Category->create();
      $this->Category->set($this->data);
      if ($this->Category->save()) {
        $this->flash( 'カテゴリを追加しました。', 'success' );
        $this->redirect(array('action' => 'admin_index'));
      } else {
        if (!empty($this->data['Category']['parent_id'])) {
          $parent_id = $this->data['Category']['parent_id'];
        }
        $errors = $this->validateErrors($this->Category);
        foreach ($errors as $id => $val) {
          $this->flash($val, 'error');
        }
      }
    }
    if (!$parent_id) {
      $this->flash( '親カテゴリが選択されていません。', 'error' );
      $this->redirect(array('action' => 'admin_index'));
    }
    if ($parent_category = $this->Category->read(null, $parent_id)) {
      $this->set('parent_title', $parent_category['Category']['title']);
      $this->set('parent_id', $parent_id);
    } else {
      $this->flash( '親カテゴリが存在しません。', 'error' );
      $this->redirect(array('action' => 'admin_index'));
    }
  }

  function admin_delete($id = null) {
    if (!$id) {
      $this->flash( '削除するカテゴリを指定してください。', 'error' );
      $this->redirect(array('action' => 'admin_index'));
    }
    if ($this->Category->del($id, true)) {
      $this->flash( 'カテゴリを削除しました。', 'success' );
      $this->redirect(array('action' => 'admin_index'));
    }
  }

  function admin_edit($id = null) {
    $this->pageTitle = '管理画面：カテゴリ情報の編集';
    $this->layout = 'admin';
    if (!$id && empty($this->data)) {
      $this->flash( '編集するカテゴリを指定してください。', 'error' );
      $this->redirect(array('action' => 'admin_index'));
    }
    if (!empty($this->data)) {
      $this->Category->set($this->data);
      if ($this->Category->save()) {
        $this->flash( 'カテゴリ情報を更新いたしました。', 'success' );
        $this->redirect(array('action' => 'admin_index'));
      }
    }
    if (empty($this->data)) {
      $this->Category->recursive = -1;
      $this->data = $this->Category->read(null, $id);
    }
    if ($parent_category = $this->Category->read(null, $this->data['Category']['parent_id'])) {
      $this->set('parent_title', $parent_category['Category']['title']);
    } else {
      $this->set('parent_title', '');
    }
  }

  function admin_init() {
    $this->pageTitle = '管理画面：サイト名の登録';
    $this->layout = 'admin';
    if ($this->Category->findByParent_Id('0')) {
      $this->redirect(array('action' => 'admin_index'));
    }
    $rootCategory = array('Category' => array('name' => 'sample_site', 'title' => 'サンプルサイト', 'parent_id' => '0'));
    if (!empty($this->data)) {
      $this->Category->create();
      $this->data['Category']['parent_id'] = $rootCategory['Category']['parent_id'];
      unset($this->Category->validate['parent_id']['existCategory']);
      $this->Category->set($this->data);
      if ($this->Category->save()) {
        $this->flash( 'サイト名を登録いたしました。', 'success' );
        $this->redirect(array('action' => 'admin_index'));
      } else {
        $errors = $this->validateErrors($this->Category);
        foreach ($errors as $id => $val) {
          $this->flash($val, 'error');
        }
      }
    } else {
      $this->data = $rootCategory;
    }
  }

  function admin_upload() {
    if (!$this->data['Category']['upload_file']) {
      $this->set('error', 'アップロードするファイルを選択してください');
      $this->render('admin_upload','ajax');
    } else {
      $filename = '';
      if ($this->data['Category']['upload_file']['type'] == 'image/jpeg') {
        $filename = intval(rand()) . '.jpg';
      } elseif ($this->data['Category']['upload_file']['type'] == 'image/pjpeg') {
        $filename = intval(rand()) . '.jpg';
      } elseif ($this->data['Category']['upload_file']['type'] == 'image/gif') {
        $filename = intval(rand()) . '.gif';
      } elseif ($this->data['Category']['upload_file']['type'] == 'image/png') {
        $filename = intval(rand()) . '.png';
      } elseif ($this->data['Category']['upload_file']['type'] == 'image/x-png') {
        $filename = intval(rand()) . '.png';
      }
      if ($filename) {
        if (is_writeable(IMAGES)) {
          rename($this->data['Category']['upload_file']['tmp_name'], IMAGES . $filename);
          $this->set('filename', $filename);
          $this->render('admin_upload','ajax');
        } else {
          $this->set('error', '画像フォルダ(' . IMAGES . ')に書き込みできません。');
          $this->render('admin_upload','ajax');
        }
      } else {
        $this->set('error', '画像ファイルではありません。');
        $this->render('admin_upload','ajax');
      }
    }
  }


}
?>
