<?php

/**
 * $Id$
 *
 * @package    Mediboard
 * @subpackage SalleOp
 * @author     SARL OpenXtrem <*****@*****.**>
 * @license    GNU General Public License, see http://www.gnu.org/licenses/gpl.html
 * @version    $Revision$
 */
CCanDo::checkAdmin();
$item_type_id = CValue::get('item_type_id');
$item_category_id = CValue::get('item_category_id');
$group_id = CGroups::loadCurrent()->_id;
$item_type = new CDailyCheckItemType();
if ($item_type->load($item_type_id)) {
    $item_type->loadRefsNotes();
} else {
    $item_type->index = 1;
    $item_type->category_id = $item_category_id;
    $item_type->active = "1";
}
$item_type->loadRefCategory()->loadRefListType();
$item_category = new CDailyCheckItemCategory();
foreach (CDailyCheckList::$_HAS_classes as $_class) {
    unset($item_category->_specs["target_class"]->_locales[$_class]);
}
$category = new CDailyCheckItemCategory();
$category->load($item_category_id);
$op = $category->target_class == "COperation" ? 1 : 0;
 /**
  * Get item types
  *
  * @return CDailyCheckItemType[]
  */
 function loadItemTypes()
 {
     $ds = $this->getDS();
     $where = array("active" => "= '1'");
     if ($this->type) {
         $where['daily_check_item_category.type'] = $ds->prepare("= %", $this->type);
     }
     $ljoin = array('daily_check_item_category' => 'daily_check_item_category.daily_check_item_category_id = daily_check_item_type.category_id');
     if ($this->list_type_id) {
         $where["daily_check_item_category.list_type_id"] = $ds->prepare("= %", $this->list_type_id);
     } else {
         $where["daily_check_item_category.target_class"] = $ds->prepare("= %", $this->object_class);
         $where[] = $ds->prepare("daily_check_item_category.target_id IS NULL OR daily_check_item_category.target_id = %", $this->object_id);
     }
     $orderby = 'daily_check_item_category.`index`, daily_check_item_category.title, ';
     // Si liste des points de la HAS
     if (in_array($this->object_class, self::$_HAS_classes)) {
         $orderby .= "daily_check_item_type_id";
     } else {
         $orderby .= "`index`, title";
     }
     $itemType = new CDailyCheckItemType();
     $this->_ref_item_types = $itemType->loadGroupList($where, $orderby, null, null, $ljoin);
     foreach ($this->_ref_item_types as $type) {
         $type->loadRefsFwd();
     }
     /** @var CDailyCheckItem[] $items */
     $items = $this->loadBackRefs('items');
     if ($items) {
         foreach ($items as $item) {
             if (isset($this->_ref_item_types[$item->item_type_id])) {
                 $this->_ref_item_types[$item->item_type_id]->_checked = $item->checked;
                 $this->_ref_item_types[$item->item_type_id]->_answer = $item->getAnswer();
             }
         }
     }
     return $this->_ref_item_types;
 }