<?php
/**
 * i-doit - Documentation and CMDB solution for IT environments
 *
 * This file is part of the i-doit framework. Modify at your own risk.
 *
 * Please visit http://www.i-doit.com/license for a full copyright and license information.
 *
 * @version     1.10
 * @package     i-doit
 * @author      synetics GmbH
 * @copyright   synetics GmbH
 * @url         http://www.i-doit.com
 * @license     http://www.i-doit.com/license
 */
namespace idoit\Module\Cmdb\Model\Matcher;

use idoit\Model\Dao\Base;
use idoit\Exception\Exception;

/**
 * i-doit
 *
 * Ci Models
 *
 * @package     i-doit
 * @subpackage  Cmdb
 * @author      Dennis Stücken <dstuecken@i-doit.com>
 * @version     1.8
 * @copyright   synetics GmbH
 * @license     http://www.i-doit.com/license
 */
class MatchDao extends Base
{

    /**
     * Retrieves a object match profile by id
     *
     * @param $id
     *
     * @return array
     */
    public function getMatchProfileById($id)
    {
        $sql = 'SELECT * FROM isys_obj_match WHERE isys_obj_match__id = ' . $this->convert_sql_id($id);
        return $this->retrieve($sql)->get_row();
    } // function

    /**
     * Get all object match profiles
     *
     * @return \isys_component_dao_result
     * @throws \isys_exception_database
     * @author   Van Quyen Hoang <qhoang@i-doit.com>
     */
    public function getMatchProfiles()
    {
        return $this->retrieve('SELECT * FROM isys_obj_match');
    }

    /**
     * Update a object match profile
     *
     * @param int       $id
     * @param string    $title
     * @param int       $bits
     * @param int       $minMatch
     *
     * @return bool
     * @throws \isys_exception_dao
     * @author   Van Quyen Hoang <qhoang@i-doit.com>
     */
    public function saveMatchProfile($id = null, $title, $bits, $minMatch)
    {
        $sql = 'UPDATE isys_obj_match SET
          isys_obj_match__title = ' . $this->convert_sql_text($title) . ',
          isys_obj_match__bits = ' . $this->convert_sql_int($bits) . ',
          isys_obj_match__min_match = ' . $this->convert_sql_int($minMatch) . '
          WHERE isys_obj_match__id = ' . $this->convert_sql_id($id);

        return $this->update($sql);
    } // function

    /**
     * Create a new object match profile
     *
     * @param $title
     * @param $bits
     * @param $minMatch
     *
     * @return bool
     * @throws \isys_exception_dao
     * @author   Van Quyen Hoang <qhoang@i-doit.com>
     */
    public function createMatchProfile($title, $bits, $minMatch)
    {
        $sql = 'INSERT INTO isys_obj_match (isys_obj_match__title, isys_obj_match__bits, isys_obj_match__min_match) VALUES
          (' . $this->convert_sql_text($title) . ', ' . $this->convert_sql_int($bits) . ', ' . $this->convert_sql_int($minMatch) . ');';
        return $this->update($sql);
    } // function

    /**
     * Delete object match profile
     *
     * @param $profileIDs
     *
     * @return bool
     * @throws \isys_exception_dao
     * @author   Van Quyen Hoang <qhoang@i-doit.com>
     */
    public function deleteMatchProfile($profileID)
    {
        if(is_array($profileID) && count($profileID) > 0)
        {
            $condition = 'WHERE isys_obj_match__id IN (';
            $idsCollection = '';
            foreach($profileID AS $id)
            {
                if($id == 1) continue;

                $idsCollection .= $id . ',';
            } // foreach

            if($idsCollection == '')
            {
                throw new \isys_exception_general(_L('LC__MODULE__SYSTEM__OBJECT_MATCHING__DEFAULT_PROFILE_IS_NOT_DELETABLE'));
            } // if

            $condition .= rtrim($idsCollection, ',') . ')';
        }
        elseif(is_numeric($profileID))
        {
            if($profileID == 1)
            {
                throw new \isys_exception_general(_L('LC__MODULE__SYSTEM__OBJECT_MATCHING__DEFAULT_PROFILE_IS_NOT_DELETABLE'));
            } // if

            $condition = 'WHERE isys_obj_match__id = ' . $this->convert_sql_id($profileID);
        }
        else
        {
            if($profileID)
            {
                throw new Exception(sprintf('Could not delete matching profile ID "%s".', $profileID));
            }
            else
            {
                throw new Exception(sprintf('No profiles were selected.'));
            }
        } // if

        $delete = 'DELETE FROM isys_obj_match ' . $condition;
        return $this->update($delete);
    }
}