<?php
class ArticleItem extends AppModel {

  var $name = 'ArticleItem';

  function beforeValidate() {
    if (!empty($this->data['ArticleItem']['required'])) {
      if (empty($this->data['ArticleItem']['indexed_value']) && empty($this->data['ArticleItem']['number_value']) && empty($this->data['ArticleItem']['date_value']) && empty($this->data['ArticleItem']['text_value'])) {
        return false;
      }
    }
    return true;
  }

  function beforeSave() {
    if (!empty($this->data[$this->name]['optional_value'])) {
      $var = serialize($this->data[$this->name]['optional_value']);
      $this->data[$this->name]['optional_value'] = base64_encode($var);
    }
    return true;
  }

  function afterFind($results) {
    $mod_results = array();
    foreach ($results as $result) {
      if (!empty($result[$this->name]['optional_value'])) {
        $var = base64_decode($result[$this->name]['optional_value']);
        $result[$this->name]['optional_value'] = unserialize($var);
      }
      $mod_results[] = $result;
    }
    return $mod_results;
  }

  var $validate = array(
    'number_value' => array(
      'numeric' => array(
        'rule' => array('numeric'),
        'allowEmpty' => true,
        'required' => false,
        'message' => '半角数字のみ有効です。'
      )
    )
  );

  //The Associations below have been created with all possible keys, those that are not needed can be removed
  var $belongsTo = array(
      'Item' => array('className' => 'Item',
                'foreignKey' => 'item_id',
                'conditions' => '',
                'fields' => '',
                'order' => ''
      )
  );

}
?>
