<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
class Core_Model_DbTable_MailTemplates extends Engine_Db_Table {

    protected $_rowClass = 'Core_Model_MailTemplate';

    public function getEmailTypes() {

        $enabledModuleNames = Engine_Api::_()->getDbtable('modules', 'core')->getEnabledModuleNames();
        $enabledModuleNames = array_diff($enabledModuleNames,array('payment', 'invite', 'core'));

        $excludeMailTypes = array('header', 'footer', 'header_member', 'footer_member', 'core_lostpassword');

        $name = $this->info('name');

        $select = $this->select()
            ->from($name, array('type', 'module', 'default', 'is_admin'))
            ->where('module IN(?)', $enabledModuleNames)
            ->where('type NOT IN(?)', $excludeMailTypes);

        $emailTypes = $this->fetchAll($select);

        return $emailTypes;
    }

    public function getDefaultEmails()
    {
        $excludeMailTypes = array('header', 'footer', 'header_member', 'footer_member');

        $select = $this->select()
            ->from($this->info('name'), 'type')
            ->where('`default` = ?', 1)
            ->where('type NOT IN(?)', $excludeMailTypes);

        $types = $select
            ->query()
            ->fetchAll(Zend_Db::FETCH_COLUMN);

        return $types;
    }

    public function setDefaultEmails($values)
    {
        if( !is_array($values) ){
            throw new Core_Model_Exception('setDefaultEmails requires an array of emails');
        }

        $values = array_merge($values, array('header', 'footer', 'header_member', 'footer_member'));

        $types = $this->select()
            ->from($this->info('name'), 'type')
            ->query()
            ->fetchAll(Zend_Db::FETCH_COLUMN);

        $defaults = array();
        foreach( $types as $value ){
            if( engine_in_array($value, $values) ){
                $defaults[] = $value;
            }
        }

        if( !empty($defaults) ){

            $this->update(array('default' => '1',), array('`type` IN(?)' => $defaults));

            $this->update(array('default' => '0',), array('`type` NOT IN(?)' => $defaults));
        } else {
            $this->update(array('default' => '0'), array('`default`' => '1'));
        }
    }
    
		public function isEnableNotificationType($type) {
			return $this->select()
										->from($this->info('name'), 'default')
										->where('type = ?', $type)
										->query()
										->fetchColumn();
		}
}
