<?php

namespace ModulesGarden\HostingQuotaNotifications\App\Libs\ProductGroups;

use \ModulesGarden\HostingQuotaNotifications\App\Models;
use \ModulesGarden\HostingQuotaNotifications\Core\Models\Whmcs;

/**
 * Description of Manage
 *
 * @author Artur Pilch <artur.pi@modulesgarden.com>
 */
class Manage
{
    public static function saveRelations($gid, $products, $deleteOld = true)
    {
        if($deleteOld)
        {
            self::deleteRelations($gid);
        }


        foreach($products as $product)
        {
            $model = new Models\ProductGroup();
            $model->pid = $product;
            $model->gid = $gid;
            $model->save();
        }
    }
    
    public static function deleteRelations($gid)
    {
        Models\ProductGroup::where('gid', '=', $gid)->delete();
    }
    
    public static function getRelations($gid)
    {
        return Models\ProductGroup::where('gid', '=', $gid)->get()->toArray();
    }
    
    public static function getProducts($gid)
    {
        return Models\ProductGroup::select('pid')->where('gid', '=', $gid)->get()->pluck('pid')->toArray();
    }
}
