<?php
namespace App\Utils\PartnerGroups; abstract class GeneralAbstract { protected $partnerType; protected $dbTableName; protected $partnerColumnName; protected $groupElementsStructured = null; public function __construct() { $this->dbTableName = $this->getDbTableName(); if (!$this->dbTableName) { throw new \Exception('The `dbTableName` field is not set for class '.get_called_class()); } if (!$this->partnerColumnName) { throw new \Exception('The `partnerColumnName` field is not set for class '.get_called_class()); } } public function setGroupElementsStructured(array $groupElementsStructured) { $this->groupElementsStructured = $groupElementsStructured; } public function getPartnerIdsList() { if ($this->groupElementsStructured===null) { throw new \Exception('You must call `setGroupElementsLinear()` or `setGroupElementsStructured()` before calling `getPartnerIdsList()`.'); } $db = \DB::getInstance(); $selectSQL = "SELECT DISTINCT `{$this->partnerColumnName}` FROM `{$this->dbTableName}` AS `main` WHERE 1"; foreach ($this->groupElementsStructured as $groupElements) { $groupName = uniqid("group_"); $selectSQL .= " AND EXISTS (SELECT DISTINCT `{$this->partnerColumnName}` FROM `{$this->dbTableName}` as `{$groupName}` WHERE main.{$this->partnerColumnName} = {$groupName}.{$this->partnerColumnName} AND `idGroupElement` IN (".implode(',', array_map(function($str){return "'".$str."'";}, $groupElements))."))"; } $partners = $db->getAll($selectSQL); if ($db->ErrorNo()) { throw new \DBException('The record could not be selected: '.$db->ErrorMsg()); } $partnerIdsList = []; foreach ($partners as $partner) { $partnerIdsList[] = $partner[$this->partnerColumnName]; } return $partnerIdsList; } abstract protected function getDbTableName(); public function setGroupElementsLinear(array $groupElementsLinear) { $elementsStructured = []; foreach ($groupElementsLinear as $idGroupElem) { if ($this->partnerType=='corporate') { $elementDetails = \App\Config\CorporateGroups::getInstance()->getGroupElementDetailsById($idGroupElem); } elseif ($this->partnerType=='supplier') { $elementDetails = \App\Config\SupplierGroups::getInstance()->getGroupElementDetailsById($idGroupElem); } $elementsStructured[$elementDetails['idGroup']][] = $idGroupElem; } $this->groupElementsStructured = $elementsStructured; } } 