<?php
class ArticleItemsController extends AppController {

  var $name = 'ArticleItems';
  var $uses = array('ArticleItem', 'Article', 'Item');
  var $helpers = array('Html', 'Form', 'ApArticleItem', 'Javascript', 'Fck', 'Time', 'Number');

  function admin_index() {
    $this->redirect(array('controller'=>'articles', 'action'=>'admin_index'));
  }

  function admin_view($article_id = null) {
    $this->pageTitle = '管理画面：記事内容の確認';
    $this->layout = 'admin';
    if (!$article_id) {
      $this->flash( '記事がみつかりません。', 'error' );
      $this->redirect(array('controller' => 'articles', 'action' => 'admin_index'));
    } else {
      if (!$article = $this->Article->findById($article_id, null, null, -1)) {
        $this->flash( '記事が登録されていません。', 'error' );
        $this->redirect(array('controller' => 'articles', 'action' => 'admin_add'));
      } else {
        $this->set('article_id', $article_id);
        $this->set('article_title', $article['Article']['title']);
        $this->set('article_status', $article['Article']['status']);
        $this->data = $this->ArticleItem->find('all', array('conditions' => array('article_id' => $article_id, 'Item.status >' => '0'), 'order' => 'Item.sort_order', 'recursive' => 0));
      }
    }
  }

  function admin_add() {
    $this->redirect(array('controller'=>'articles', 'action'=>'admin_add'));
  }

  function admin_edit($article_id = null) {
    $this->pageTitle = '管理画面：記事内容の編集';
    $this->layout = 'admin';
    if (!$article_id) {
      $this->flash( '記事がみつかりません。', 'error' );
      $this->redirect(array('controller' => 'articles', 'action' => 'admin_index'));
    } else {
      if (!$article = $this->Article->findById($article_id, null, null, -1)) {
        $this->flash( '記事が登録されていません。', 'error' );
        $this->redirect(array('controller' => 'articles', 'action' => 'admin_add'));
      } else {
        $this->set('article_title', $article['Article']['title']);
        $this->set('article_status', $article['Article']['status']);
      }
    }
    if (!empty($this->data)) {
      $error_count = 0;
      $saveall_data = array();
      foreach ($this->data['ArticleItem'] as $id => $val) {
        if (!empty($val['date_value'])) {
          if (is_array($val['date_value'])) {
            $date_string = $this->ArticleItem->deconstruct('date_value', $val['date_value']);
            $val['date_value'] = $date_string;
          }
        }
        $article_item = array('ArticleItem' => array_merge(array('id' => $id), $val));
        $saveall_data[] = array_merge(array('id' => $id), $val);
        $this->ArticleItem->create();
        $this->ArticleItem->set($article_item);
        if (!$this->ArticleItem->beforeValidate()) {
          $this->flash($article_item['ArticleItem']['title'] . ' : 必須項目です', 'error');
          $error_count++;
        } else {
          if (!$this->ArticleItem->validates()) {
            $errors = $this->validateErrors($this->ArticleItem);
            foreach ($errors as $err_item => $err_msg) {
              $this->flash($article_item['ArticleItem'][$err_item] . ' : ' . $err_msg, 'error');
              $error_count++;
            }
          }
        }
      }
      if (!$error_count) {
        if ($this->ArticleItem->saveall($saveall_data)) {
          $article = $this->Article->read(null, $article_id);
          $search_text = '';
          foreach ($saveall_data as $id => $val) {
            if (isset($val['search']) && ($val['search'] > 0)) {
              if (isset($val['indexed_value'])) {
                $search_text .= ' ' . strip_tags($val['indexed_value']);
              }
              if (isset($val['text_value'])) {
                $search_text .= ' ' . strip_tags($val['text_value']);
              }
            }
          }
          $article['Article']['search_text'] = $search_text;
          if (!empty($this->params['form']['publish'])) {
            $article['Article']['status'] = 1;
          }
          $this->Article->set($article);
          $this->Article->save();
          $this->flash( '記事内容を更新しました。', 'success' );
          $this->redirect('/admin/article_items/view/' . $article_id);
        }
      } else {
        $this->data = $this->ArticleItem->find('all', array('conditions' => array('article_id' => $article_id, 'Item.status >' => '0'), 'order' => 'Item.sort_order', 'recursive' => 0));
      }
    } else {
      $items = $this->Item->find('all', array('conditions' => array('status' => '1'), 'order' => 'sort_order', 'recursive' => -1));
      $article_item = array();
      foreach ($items as $item) {
        if (!$this->ArticleItem->find('all', array('conditions' => array('article_id' => $article_id, 'item_id' => $item['Item']['id']), 'recursive' => -1))) {
          $article_item[] = array('article_id' => $article_id, 'item_id' => $item['Item']['id']);
        }
      }
      if ($article_item) {
        if (!$this->ArticleItem->saveall($article_item)) {
          $this->flash( '記事作成エラー。', 'error' );
          $this->redirect(array('controller'=>'articles', 'action'=>'admin_add'));
        }
      }
      $this->data = $this->ArticleItem->find('all', array('conditions' => array('article_id' => $article_id, 'Item.status >' => '0'), 'order' => 'Item.sort_order', 'recursive' => 0));
    }
    $this->set('article_id', $article_id);
  }

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

}
?>
