<?php
namespace Solwin\Verticalmenu\Model\ResourceModel\Menu;

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
    /**
     * ID Field Name
     * 
     * @var string
     */
    protected $_idFieldName = 'menu_id';

    /**
     * Event prefix
     * 
     * @var string
     */
    protected $_eventPrefix = 'solwin_verticalmenu_menu_collection';

    /**
     * Event object
     * 
     * @var string
     */
    protected $_eventObject = 'menu_collection';

    /**
     * Define resource model
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('Solwin\Verticalmenu\Model\Menu', 'Solwin\Verticalmenu\Model\ResourceModel\Menu');
    }

    /**
     * Get SQL for get record count.
     * Extra GROUP BY strip added.
     *
     * @return \Magento\Framework\DB\Select
     */
    public function getSelectCountSql()
    {
        $countSelect = parent::getSelectCountSql();
        $countSelect->reset(\Zend_Db_Select::GROUP);
        return $countSelect;
    }
    /**
     * @param string $valueField
     * @param string $labelField
     * @param array $additional
     * @return array
     */
    protected function _toOptionArray($valueField = 'menu_id', $labelField = 'menu_title', $additional = [])
    {
        return parent::_toOptionArray($valueField, $labelField, $additional);
    }

    /**
     * add if filter
     *
     * @param $menuIds
     * @return $this
     */
    public function addIdFilter($menuIds)
    {
        if (is_array($menuIds)) {
            if (empty($menuIds)) {
                $condition = '';
            } else {
                $condition = ['in' => $menuIds];
            }
        } elseif (is_numeric($menuIds)) {
            $condition = $menuIds;
        } elseif (is_string($menuIds)) {
            $ids = explode(',', $menuIds);
            if (empty($ids)) {
                $condition = $menuIds;
            } else {
                $condition = ['in' => $ids];
            }
        }
        $this->addFieldToFilter('menu_id', $condition);
        return $this;
    }
}
