<?php

/* * ********************************************************************
 * Wordpress_Manager Product developed. (Nov 17, 2017)
 * *
 *
 *  CREATED BY MODULESGARDEN       ->       http://modulesgarden.com
 *  CONTACT                        ->       contact@modulesgarden.com
 *
 *
 * This software is furnished under a license and may be used and copied
 * only  in  accordance  with  the  terms  of such  license and with the
 * inclusion of the above copyright notice.  This software  or any other
 * copies thereof may not be provided or otherwise made available to any
 * other person.  No title to and  ownership of the  software is  hereby
 * transferred.
 *
 *
 * ******************************************************************** */

namespace ModulesGarden\HostingQuotaNotifications\App\UI\Groups\Providers;

use ModulesGarden\HostingQuotaNotifications\App\Helper\Lang\Lang;
use ModulesGarden\HostingQuotaNotifications\Core\UI\Widget\Forms\DataProviders\BaseModelDataProvider;
use ModulesGarden\HostingQuotaNotifications\Core\UI\ResponseTemplates;
use ModulesGarden\HostingQuotaNotifications\Core\UI\Traits\RequestObjectHandler;
use ModulesGarden\HostingQuotaNotifications\Core\UI\Interfaces\AdminArea;
use \ModulesGarden\HostingQuotaNotifications\App\Models;
use \ModulesGarden\HostingQuotaNotifications\Core\Helper\BuildUrl;
use \ModulesGarden\HostingQuotaNotifications\App\Libs;
use \ModulesGarden\HostingQuotaNotifications\Core\Helper;

/**
 * Description of GroupsProvider
 *
 * @author Artur Pilch <artur.pi@modulesgarden.com>
 */
class GroupsProvider extends BaseModelDataProvider implements AdminArea
{

    use RequestObjectHandler;

    public function __construct()
    {
        parent::__construct(null);
    }

    public function read()
    {
        $this->data['products']['avalibleValues'] = Libs\Groups\Manage::getProducts();
    }

    public function getGroups()
    {
        $out = [];
        foreach (Models\Groups::all() as $group)
        {
            $out[] = [
                'id'     => $group->id,
                'name'   => $group->name,
                'type'   => Lang::getAbsoulteLang('groups', $group->type),
                'status' => $group->status
            ];
        }
        return $out;
    }

    public function addGroupPrepare()
    {
        $groupModel = new Models\Groups();
        $products   = $this->formData['products'];
        unset($this->formData['products']);
        foreach ($this->formData as $key => $value)
        {
            $groupModel->{$key} = $value;
        }
        $groupModel->save();
        Libs\ProductGroups\Manage::saveRelations($groupModel->id, $products);

        return (new ResponseTemplates\HtmlDataJsonResponse())->setMessageAndTranslate('groupAdded')
            ->setCallBackFunction('mgCustomRedirect')
            ->setData(["redirectUrl" => BuildUrl::getUrl('groups', 'configuration', ['groupid' => $groupModel->id])]);
    }

    public function updateMass()
    {
        foreach ($this->getRequestValue('massActions') as $groupId)
        {
            Libs\Groups\Manage::update('id', '=', $groupId, ['status' => $this->formData['enableStatus']]);
        }
        if($this->formData['enableStatus'] == 'on')
        {
            return (new ResponseTemplates\HtmlDataJsonResponse())->setMessageAndTranslate('groupsStatusEnabled');
        }
        return (new ResponseTemplates\HtmlDataJsonResponse())->setMessageAndTranslate('groupsStatusDisabled');
    }
}
