<?php

/**
 * i-doit
 *
 * DAO: ObjectType list for manuals
 *
 * @package     i-doit
 * @subpackage  CMDB_Category_lists
 * @author      Niclas Potthast <npotthast@i-doit.org> - 2007-08-21
 * @copyright   synetics GmbH
 * @license     http://www.i-doit.com/license
 */
class isys_cmdb_dao_list_cats_file_object extends isys_component_dao_category_table_list
{
    /**
     * Return category constant.
     *
     * @return  integer
     */
    public function get_category()
    {
        return defined_or_default('C__CATS__FILE_OBJECTS');
    }

    /**
     * Return category type constant.
     *
     * @return  integer
     */
    public function get_category_type()
    {
        return C__CMDB__CATEGORY__TYPE_SPECIFIC;
    }

    /**
     * @param string $table
     * @param integer $objectId
     * @param integer $recordStatus
     * @return isys_component_dao_result
     * @throws isys_exception_database
     */
    public function get_result($table = null, $objectId = null, $recordStatus = null)
    {
        $objectId = $this->convert_sql_id($objectId);
        $recordStatus = $this->convert_sql_int(empty($recordStatus) ? $this->get_rec_status() : $recordStatus);

        $sql = "SELECT
                connectionList.connection as connectionId,
                fileObject.isys_obj__id,
                fileObject.isys_obj__title,
                targetObject.isys_obj__id as objectId,
                targetObject.isys_obj__title as objectTitle,
                targetObjectType.isys_obj_type__title as objectTypeTitle,
                connectionList.catId as categoryEntryId,
                (SELECT count(*) FROM isys_catg_file_list WHERE isys_catg_file_list__isys_connection__id = connectionList.connection) AS fileCategory,
                (SELECT count(*) FROM isys_catg_manual_list WHERE isys_catg_manual_list__isys_connection__id = connectionList.connection) AS manualCategory,
                (SELECT count(*) FROM isys_catg_emergency_plan_list WHERE isys_catg_emergency_plan_list__isys_connection__id = connectionList.connection) AS emergencyPlanCategory
            FROM (
                SELECT -- Select for file
                    fl.isys_catg_file_list__isys_connection__id AS connection,
                    fl.isys_catg_file_list__isys_obj__id target,
                    fl.isys_catg_file_list__id catId
                FROM isys_catg_file_list fl
                WHERE fl.isys_catg_file_list__status = {$recordStatus}

                UNION

                SELECT -- Select for manuals
                    ml.isys_catg_manual_list__isys_connection__id,
                    ml.isys_catg_manual_list__isys_obj__id,
                    ml.isys_catg_manual_list__id
                FROM isys_catg_manual_list ml
                WHERE ml.isys_catg_manual_list__status = {$recordStatus}

                UNION

                SELECT -- Select for emergency plan
                    epl.isys_catg_emergency_plan_list__isys_connection__id,
                    epl.isys_catg_emergency_plan_list__isys_obj__id,
                    epl.isys_catg_emergency_plan_list__id
                FROM isys_catg_emergency_plan_list epl
                WHERE epl.isys_catg_emergency_plan_list__status = {$recordStatus}
            ) connectionList
            INNER JOIN isys_connection con ON con.isys_connection__id = connectionList.connection
            INNER JOIN isys_obj fileObject ON con.isys_connection__isys_obj__id = fileObject.isys_obj__id
            INNER JOIN isys_obj targetObject ON connectionList.target = targetObject.isys_obj__id
            INNER JOIN isys_obj_type targetObjectType ON targetObject.isys_obj__isys_obj_type__id = targetObjectType.isys_obj_type__id
            WHERE fileObject.isys_obj__id = {$objectId}";

        // @see ID-11363 Implement the same check as in ID-8287.
        if (!isys_tenantsettings::get('cmdb.only-show-ranked-entries-as-such', 0)) {
            $sql .= " AND targetObject.isys_obj__status = {$recordStatus}";
        }

        return $this->retrieve($sql . ';');
    }

    /**
     * @param array $row
     *
     * @throws Exception
     */
    public function modify_row(&$row)
    {
        // Calculate targetd category
        $categoryConstant = defined_or_default('C__CATG__FILE');

        if (defined('C__CATG__MANUAL') && !!$row['manualCategory']) {
            $categoryConstant = C__CATG__MANUAL;
        } elseif (defined('C__CATG__EMERGENCY_PLAN') && !!$row['emergencyPlanCategory']) {
            $categoryConstant = defined_or_default('C__CATG__EMERGENCY_PLAN');
        }

        // Set category title
        $row['categoryTitle'] = isys_application::instance()->container->get('language')->get(
            isys_application::instance()->container->get('cmdb_dao')->get_catg_name_by_id_as_string($categoryConstant)
        );

        // Create quick info for object
        $row['objectTitle'] = isys_ajax_handler_quick_info::instance()->getQuickInfoReplacement(
            $row['objectId'],
            $row['objectTitle']
        );

        // Use connection id as identifier
        $row['isys_cats_file_list__id'] = $row['connectionId'];
    }

    /**
     *
     * @return  array
     */
    public function get_fields()
    {
        return [
            "objectTitle"     => "LC__CMDB__CATG__GLOBAL_TITLE",
            "objectTypeTitle" => "LC__CMDB__OBJTYPE",
            "categoryTitle"   => "LC__CMDB__CATG__CATEGORY"
        ];
    }
}
