<?php
namespace ApplicationBundle\Twig;
use ApplicationBundle\Constants\ConsultancyConstant;
use ApplicationBundle\Constants\GeneralConstant;
use ApplicationBundle\Constants\ToDoConstant;
use ApplicationBundle\Constants\UserConstants;
use ApplicationBundle\Modules\Inventory\Inventory;
use ApplicationBundle\Modules\Sales\SalesOrderM;
use ApplicationBundle\Modules\Sales\Client;
use ApplicationBundle\Modules\Purchase\Purchase;
use ApplicationBundle\Modules\Purchase\Supplier;
use ApplicationBundle\Modules\Comment\Comment;
use ApplicationBundle\Modules\System\System;
use ApplicationBundle\Modules\User\Department;
use ApplicationBundle\Modules\User\Company;
use ApplicationBundle\Modules\User\Module;
use ApplicationBundle\Modules\User\Position;
use ApplicationBundle\Modules\User\Users;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
class GeneralExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
{
protected $em;
protected $em_goc;
protected $dm;
protected $encryptDecrypt;
public function __construct(\Doctrine\ORM\EntityManager $em, \Doctrine\ORM\EntityManager $em_goc, \ApplicationBundle\Helper\EncryptDecrypt $encryptDecrypt)
{
$this->em = $em;
$this->em_goc = $em_goc;
// $this->dm = $dm;
$this->encryptDecrypt = $encryptDecrypt;
}
public function getFilters()
{
return array(
'functionExists' => new \Twig_SimpleFilter('functionExists', array($this, 'functionExists')),
'isPhpSeven' => new \Twig_SimpleFilter('isPhpSeven', array($this, 'isPhpSeven')),
'getConvMultFromToWithdrawable' => new \Twig_SimpleFilter('getConvMultFromToWithdrawable', array($this, 'getConvMultFromToWithdrawable')),
'getCurrencyListByMarker' => new \Twig_SimpleFilter('getCurrencyListByMarker', array($this, 'getCurrencyListByMarker')),
'getCurrencyList' => new \Twig_SimpleFilter('getCurrencyList', array($this, 'getCurrencyList')),
'imageToBase64Encode' => new \Twig_SimpleFilter('imageToBase64Encode', array($this, 'imageToBase64Encode')),
'strToHex' => new \Twig_SimpleFilter('strToHex', array($this, 'strToHex')),
'toHex' => new \Twig_SimpleFilter('toHex', array($this, 'toHex')),
// 'getGlobalVars' => new \Twig_SimpleFilter('getGlobalVars', array($this, 'getGlobals')),
'jsonDecode' => new \Twig_SimpleFilter('jsonDecode', array($this, 'jsonDecode')),
'strpad' => new \Twig_SimpleFilter('strpad', array($this, 'strpad')),
'getRandomColor' => new \Twig_SimpleFilter('getRandomColor', array($this, 'getRandomColor')),
'getCommentsForDocument' => new \Twig_SimpleFilter('getCommentsForDocument', array($this, 'getCommentsForDocument')),
'doSerialize' => new \Twig_SimpleFilter('doSerialize', array($this, 'doSerialize')),
'getUsers' => new \Twig_SimpleFilter('getUsers', array($this, 'getUsers')),
'getExpSubTypes' => new \Twig_SimpleFilter('getExpSubTypes', array($this, 'getExpSubTypes')),
'getUserRestrictions' => new \Twig_SimpleFilter('getUserRestrictions', array($this, 'getUserRestrictions')),
'PendingDocList' => new \Twig_SimpleFilter('PendingDocList', array($this, 'PendingDocList')),
'getSystemNotice' => new \Twig_SimpleFilter('getSystemNotice', array($this, 'getSystemNotice')),
'getProductDataFromFDM' => new \Twig_SimpleFilter('getProductDataFromFDM', array($this, 'getProductDataFromFDM')),
'getPositions' => new \Twig_SimpleFilter('getPositions', array($this, 'getPositions')),
'getDepartments' => new \Twig_SimpleFilter('getDepartments', array($this, 'getDepartments')),
'getCompanies' => new \Twig_SimpleFilter('getCompanies', array($this, 'getCompanies')),
'getCountries' => new \Twig_SimpleFilter('getCountries', array($this, 'getCountries')),
'getCountriesBuddyBee' => new \Twig_SimpleFilter('getCountriesBuddyBee', array($this, 'getCountriesBuddyBee')),
'getLanguagesBuddyBee' => new \Twig_SimpleFilter('getLanguagesBuddyBee', array($this, 'getLanguagesBuddyBee')),
'getBranchList' => new \Twig_SimpleFilter('getBranchList', array($this, 'getBranchList')),
'getRegionList' => new \Twig_SimpleFilter('getRegionList', array($this, 'getRegionList')),
'getGeographicalRegionList' => new \Twig_SimpleFilter('getGeographicalRegionList', array($this, 'getGeographicalRegionList')),
'getClientList' => new \Twig_SimpleFilter('getClientList', array($this, 'getClientList')),
'getClientTypeList' => new \Twig_SimpleFilter('getClientTypeList', array($this, 'getClientTypeList')),
'getSalesPersonList' => new \Twig_SimpleFilter('getSalesPersonList', array($this, 'getSalesPersonList')),
'getColors' => new \Twig_SimpleFilter('getColors', array($this, 'getColors')),
'getCountriesIndexed' => new \Twig_SimpleFilter('getCountriesIndexed', array($this, 'getCountriesIndexed')),
'getUserCompany' => new \Twig_SimpleFilter('getUserCompany', array($this, 'getUserCompany')),
'getSupplierDataForSupplierLogin' => new \Twig_SimpleFilter('getSupplierDataForSupplierLogin', array($this, 'getSupplierDataForSupplierLogin')),
'getClientDataForClientLogin' => new \Twig_SimpleFilter('getClientDataForClientLogin', array($this, 'getClientDataForClientLogin')),
'getMonthlySummary' => new \Twig_SimpleFilter('getMonthlySummary', array($this, 'getMonthlySummary')),
'getRecentSales' => new \Twig_SimpleFilter('getRecentSales', array($this, 'getRecentSales')),
'getSoCountAndMore' => new \Twig_SimpleFilter('getSoCountAndMore', array($this, 'getSoCountAndMore')),
'getRecentPurchase' => new \Twig_SimpleFilter('getRecentPurchase', array($this, 'getRecentPurchase')),
'getPositionModuleTree' => new \Twig_SimpleFilter('getPositionModuleTree', array($this, 'getPositionModuleTree')),
'showNecessaryMenu' => new \Twig_SimpleFilter('showNecessaryMenu', array($this, 'showNecessaryMenu')),
'showMenuIfAdmin' => new \Twig_SimpleFilter('showMenuIfAdmin', array($this, 'showMenuIfAdmin')),
'showNecessaryMenuGeneral' => new \Twig_SimpleFilter('showNecessaryMenuGeneral', array($this, 'showNecessaryMenuGeneral')),
'showNecessaryCreateBtn' => new \Twig_SimpleFilter('showNecessaryCreateBtn', array($this, 'showNecessaryCreateBtn')),
'todoSummaryList' => new \Twig_SimpleFilter('todoSummaryList', array($this, 'todoSummaryList')),
'inArray' => new \Twig_SimpleFilter('inArray', array($this, 'inArray')),
'encryptMe' => new \Twig_SimpleFilter('encryptMe', array($this, 'encryptMe')),
'decryptMe' => new \Twig_SimpleFilter('decryptMe', array($this, 'decryptMe')),
);
}
// function qr_code_data_uri($qr_data, $options){
// qr_code_data_uri($qr_data, $options);
// }
public function isPhpSeven($gg='')
{
if (version_compare(PHP_VERSION, '7.3.0', '>=')) {
return true;
} else {
return false;
}
}
public function isMethodExist($object, $method)
{
return method_exists($object, $method);
}
public function functionExists($method)
{
return function_exists($method) ? $method . 'pika' : $method . 'chu';
}
public function getGlobals()
{
$session = new Session();
$class = new \ReflectionClass('ApplicationBundle\Constants\UserConstants');
$UserConstants = $class->getConstants();
$class = new \ReflectionClass('ApplicationBundle\Constants\ConsultancyConstant');
$ConsultancyConstant = $class->getConstants();
$classToDoConstant = new \ReflectionClass('ApplicationBundle\Constants\ToDoConstant');
$ToDoConstant = $classToDoConstant->getConstants();
$classTicketConstant = new \ReflectionClass('ApplicationBundle\Constants\TicketConstant');
$TicketConstant = $classTicketConstant->getConstants();
// $classReportConstant = new \ReflectionClass('ApplicationBundle\Constants\ReportConstant');
$classReportConstant = new \ReflectionClass('ApplicationBundle\Constants\ReportConstant');
return array(
'session' => $session->all(),
'url_get' => $_GET,
'UserConstants' => $UserConstants,
'ConsultancyConstant' => $ConsultancyConstant,
'ToDoConstant' => $ToDoConstant,
'TicketConstant' => $TicketConstant,
'globalReportTypeList' => $classReportConstant->getStaticPropertyValue('reportTypeList'),
'enabled_report_types' => ['pika' => 'chu']
);
}
function imageToBase64Encode($imagePath)
{
$img = file_get_contents(
$imagePath
);
return base64_encode($img);
}
function getConvMultFromToWithdrawable()
{
return ConsultancyConstant::$convMultFromToWithdrawable;
}
function getCurrencyListByMarker()
{
return ConsultancyConstant::$currency_List_by_marker;
}
function strpad($input, $padlength, $padstring = '', $pad_type = STR_PAD_LEFT)
{
return str_pad($input, $padlength, $padstring, $pad_type);
}
function getRandomColor($rmax = 200, $gmax = 200, $bmax = 200, $a = 1)
{
return '' . rand(0, $rmax) . ',' . rand(0, $gmax) . ',' . rand(0, $bmax);
}
public function doSerialize($str)
{
$encoders = array(new XmlEncoder(), new JsonEncoder());
$normalizers = array(new ObjectNormalizer());
$serializer = new Serializer($normalizers, $encoders);
return $serializer->serialize($str, 'json');
}
public function jsonDecode($str, $assoc = true, $bias = JSON_BIGINT_AS_STRING)
{
if (version_compare(PHP_VERSION, '5.4.0', '>=') && !(defined('JSON_C_VERSION') && PHP_INT_SIZE > 4)) {
$obj = json_decode($str, $assoc, 512, JSON_BIGINT_AS_STRING);
} else {
$max_int_length = strlen((string)PHP_INT_MAX) - 1;
$json_without_bigints = preg_replace('/:\s*(-?\d{' . $max_int_length . ',})/', ': "$1"', $str);
$obj = json_decode($json_without_bigints, $assoc);
}
// return json_decode($str,$assoc,512,$bias);
if ($obj == null)
$obj = [];
return $obj;
}
public function getUserRestrictions()
{
$session = new Session();
return Users::getUserApplicationAccessSettings($this->em, $session->get(UserConstants::USER_ID))['options'];
}
public function getCurrencyList()
{
$session = new Session();
return Inventory::CurrencyList($this->em);
}
public function getCountries()
{
return Company::getCountryList($this->em);
}
public function getCountriesBuddyBee($type = "", $returnType = '')
{
if ($type == '_ALL_')
return Company::getCountryList($this->em_goc, '_BUDDYBEE_ALL_', $returnType);
else if ($type == '_HAS_CONSULTANT_')
return Company::getCountryList($this->em_goc, '_HAS_CONSULTANT_', $returnType);
else
return Company::getCountryList($this->em_goc, '_BUDDYBEE_', $returnType);
// return ConsultancyConstant::$buddybee_country_List;
}
public function getLanguagesBuddyBee($type = "", $returnType = '')
{
if ($type == '_ALL_')
return Company::getLanguageList($this->em_goc, '_BUDDYBEE_ALL_', $returnType);
else
return Company::getLanguageList($this->em_goc, '_BUDDYBEE_', $returnType);
// return ConsultancyConstant::$buddybee_country_List;
}
public function getBranchList($types = '', $companyId = [])
{
if ($types == '') $types = [];
return Client::BranchList($this->em, $companyId, $types);
}
public function getRegionList($types = '')
{
if ($types == '') $types = [];
$session = new Session();
return Client::RegionList($this->em, $session->get(UserConstants::USER_COMPANY_ID));
}
public function getGeographicalRegionList($types = '')
{
if ($types == '') $types = [];
$session = new Session();
return Client::GeographicalRegionList($this->em, $session->get(UserConstants::USER_COMPANY_ID));
}
public function getClientList($types = '')
{
if ($types == '') $types = [];
$session = new Session();
return Client::GetExistingClientList($this->em, $session->get(UserConstants::USER_COMPANY_ID));
}
public function getClientTypeList($types = '')
{
if ($types == '') $types = [];
$session = new Session();
return Client::ClientTypeList($this->em, $session->get(UserConstants::USER_COMPANY_ID));
}
public function getSalesPersonList($types = '')
{
if ($types == '') $types = [];
$session = new Session();
return Client::SalesPersonList($this->em, $session->get(UserConstants::USER_COMPANY_ID));
}
public function getColors()
{
return Inventory::GetColorList($this->em);
}
public function getCommentsForDocument($entity, $entityId, $appId, $isEntityComment = 0, $conversationHash = '')
{
if ($isEntityComment == 1)
{
if ($conversationHash == '')
$conversationHash = 'D' . (str_pad($appId, 5, '0', STR_PAD_LEFT)) . (str_pad($entity, 2, '0', STR_PAD_LEFT)) . (str_pad($entityId, 8, '0', STR_PAD_LEFT));
return array(
'commentList' => Comment::getCommentsByConversationHash($this->em_goc, $conversationHash, $isEntityComment),
'hash' => $conversationHash
);
}
else {
if ($conversationHash == '')
$conversationHash = 'D' . (str_pad($appId, 5, '0', STR_PAD_LEFT)) . (str_pad($entity, 2, '0', STR_PAD_LEFT)) . (str_pad($entityId, 8, '0', STR_PAD_LEFT));
return array(
'commentList' => Comment::getCommentsByConversationHash($this->em, $conversationHash, $isEntityComment),
'hash' => $conversationHash
);
}
}
public function getCountriesIndexed()
{
return Company::getCountryListIndexed($this->em);
}
public function getProductDataFromFDM($FDM)
{
$ret_data = array(
'igId' => 0,
'categoryId' => 0,
'subCategoryId' => 0,
'brandId' => 0,
'productId' => 0,
'FDM_MODIFIED' => $FDM
);
$session = new Session();
return Inventory::GetProductDataFromFdm($this->em, $FDM, $session->get(UserConstants::USER_COMPANY_ID));
// $FDM_ARRAY = explode('_', $FDM);
//
// $ret_data['igId'] = isset($FDM_ARRAY[0]) ? (1 * $FDM_ARRAY[0]) : 0;
// $ret_data['categoryId'] = isset($FDM_ARRAY[1]) ? (1 * $FDM_ARRAY[1]) : 0;
// $ret_data['subCategoryId'] = isset($FDM_ARRAY[2]) ? (1 * $FDM_ARRAY[2]) : 0;
// $ret_data['brandId'] = isset($FDM_ARRAY[3]) ? (1 * $FDM_ARRAY[3]) : 0;
// $ret_data['productId'] = isset($FDM_ARRAY[4]) ? (1 * $FDM_ARRAY[4]) : 0;
//
//
//
//
// return $ret_data;
}
public function getPositions($department_id)
{
return Position::getPositionByDepartment($this->em, $department_id);
}
public function getUsers($company_id = '')
{
return Users::getUserListById($this->em);
}
public function getExpSubTypes($company_id = '')
{
return GeneralConstant::$expenseSubTypes;
}
public function PendingDocList()
{
$session = new Session();
// return $session;
return System::getPendingApprovalListByUserLoginId($this->em, $session->get(UserConstants::USER_LOGIN_ID));
}
public function getDepartments($company_id = '')
{
return Department::getDepartmentList($this->em);
}
public function getCompanies()
{
return Company::getCompanyList($this->em);
}
public function getUserCompany()
{
$session = new Session();
// return $session;
return Company::getCompanyData($this->em, $session->get(UserConstants::USER_COMPANY_ID, 1));
// return $session->get(UserConstants::USER_COMPANY_ID);
}
public function getSupplierDataForSupplierLogin($supplierId = 0)
{
$session = new Session();
if ($supplierId == 0)
$supplierId = $session->get('supplierId');
// return $session;
return Supplier::GetSupplierForSupplierLogin($this->em, $supplierId);
// return Company::getCompanyData($this->em, $session->get(UserConstants::USER_COMPANY_ID, 1));
// return $session->get(UserConstants::USER_COMPANY_ID);
}
public function getClientDataForClientLogin()
{
$session = new Session();
$clientId = $session->get('clientId');
// return $session;
return Client::GetClientForClientLogin($this->em, $clientId);
// return Company::getCompanyData($this->em, $session->get(UserConstants::USER_COMPANY_ID, 1));
// return $session->get(UserConstants::USER_COMPANY_ID);
}
public function getMonthlySummary()
{
$session = new Session();
return Company::getMonthlyDataForDashboard($this->em, $session->get(UserConstants::USER_COMPANY_ID, 1));
// return $session;
// $list_data=
// return $session->get(UserConstants::USER_COMPANY_ID);
}
public function getRecentSales()
{
$session = new Session();
// return $session;
return SalesOrderM::RecentSalesList($this->em, $session->get(UserConstants::USER_COMPANY_ID), 5, true);
// return $session->get(UserConstants::USER_COMPANY_ID);
}
public function getSoCountAndMore($limit = 5)
{
$session = new Session();
// return $session;
return SalesOrderM::getSoCountAndMore($this->em, $session->get(UserConstants::USER_COMPANY_ID), $limit, true);
// return $session->get(UserConstants::USER_COMPANY_ID);
}
public function getRecentPurchase()
{
$session = new Session();
// return $session;
return Purchase::RecentPurchaseList($this->em, $session->get(UserConstants::USER_COMPANY_ID), 5, true);
// return $session->get(UserConstants::USER_COMPANY_ID);
}
public function getPositionModuleTree($PositionID)
{
$list = "<ul>";
$Position = $this->em->getRepository('ApplicationBundle:SysDepartmentPosition')->findOneBy(array(
'status' => GeneralConstant::ACTIVE,
'positionId' => $PositionID
));
if ($Position) {
$PositionModules = $this->em->getRepository('ApplicationBundle:SysDeptPositionDefaultModule')->findOneBy(array(
'status' => GeneralConstant::ACTIVE,
'positionId' => $Position->getPositionId()
));
$Modules = json_decode($PositionModules->getModuleIds(), true);
foreach ($Modules as &$ModuleID) {
$ModuleInfo = Module::getModuleName($this->em, $ModuleID);
$name = $ModuleInfo['name'];
self::SeekingLevel($this->em, $ModuleInfo['parent_id'], $name);
$list .= "<li>" . $name . "</li>";
}
}
$list .= "</ul>";
return $list;
}
public function getSystemNotice()
{
$session = new Session();
if($session->get('userType')!=8)
$notice = $this->em->getRepository('ApplicationBundle:SystemNotice')->findOneBy(array(
'enabled' => GeneralConstant::ACTIVE,
'showAsNotification' => GeneralConstant::ACTIVE,
// 'CompanyId'=>$session->get(UserConstants::USER_COMPANY_ID),
// 'appId'=>$session->get(UserConstants::USER_COMPANY_ID),
));
else
$notice=null;
$systemMessage = array();
if ($notice) {
$systemMessage[] = array(
'title' => $notice->getTitle(),
'desc' => $notice->getDesc(),
'countDownEnds' => $notice->getCountDownEnds(),
);
}
return $systemMessage;
}
private function SeekingLevel($em, $module_id, &$name)
{
$ModuleInfo = Module::getModuleName($em, $module_id);
if (!empty($ModuleInfo)) {
$name .= " < " . $ModuleInfo['name'];
if ($ModuleInfo['level'] > 0) {
self::SeekingLevel($em, $ModuleInfo['parent_id'], $name);
}
}
return $name;
}
/**
* This function list all the allowed route for current logged user. I have used this function on left_menu.html.twig.
*
* @return string
*/
public function showNecessaryMenu()
{
$session = new Session();
$list = json_decode($session->get(UserConstants::USER_ROUTE_LIST), true);
// $list="";
$r = "";
if ($list) {
foreach ($list as &$listContent) {
$r .= ", .accessible-menu ." . $listContent;
}
}
return $r;
}
public function strToHex($str)
{
return System::strtohex($str);
// $s = '';
// foreach (str_split($x) as $c) $s .= sprintf("%02X", ord($c));
// return ($s);
}
public function toHex($number)
{
return dechex(1 * $number);
// return $number;
// $s = '';
// foreach (str_split($x) as $c) $s .= sprintf("%02X", ord($c));
// return ($s);
}
public function showNecessaryCreateBtn()
{
$session = new Session();
$list = json_decode($session->get(UserConstants::USER_ROUTE_LIST), true);
// $list="";
$r = "";
if ($list) {
foreach ($list as &$listContent) {
$r .= ", .btn." . $listContent;
}
}
return $r;
}
public function showMenuIfAdmin()
{
$session = new Session();
$list = Company::getCompanyRouteList($this->em, $session->get(UserConstants::USER_COMPANY_ID));
// $list=json_decode($session->get(UserConstants::USER_ROUTE_LIST), true);
// $list="";
$r = "";
if ($list) {
foreach ($list as &$listContent) {
$r .= ", .accessible-menu ." . $listContent;
}
}
return $r;
}
public function showNecessaryMenuGeneral()
{
$session = new Session();
$list = json_decode($session->get(UserConstants::USER_ROUTE_LIST), true);
$r = "";
if ($list) {
foreach ($list as &$listContent) {
$r .= ", ." . $listContent;
}
}
return $r;
}
public function todoSummaryList($assigned_to, $types = [])
{
$todoList = array();
$ToDos = $this->em->getRepository('ApplicationBundle:Ticket')->findBy(
array(
'status' => ToDoConstant::ACTIVE,
'type' => $types,
'assignedToUserId' => intval($assigned_to)
)
);
if (count($ToDos) > 0) {
foreach ($ToDos as &$ToDo) {
$todoList[] = array(
'id' => $ToDo->getId(),
'title' => $ToDo->getTitle(),
'type' => $ToDo->getType(),
'urgency' => $ToDo->getUrgency()
);
}
}
return $todoList;
}
public function inArray($key, $array)
{
return in_array($key, $array);
}
public function encryptMe($str)
{
$session = new Session();
$allSessionsArray = $session->all();
/*return Crypto::Encrypt($str, $allSessionsArray[ACLConstants::SESSION_SALT]);*/
return $this->encrypt_decrypt->encrypt($str, $allSessionsArray[GeneralConstant::SESSION_SALT]);
}
public function decryptMe($str)
{
$session = new Session();
$allSessionsArray = $session->all();
/*return Crypto::Decrypt($str, $allSessionsArray[ACLConstants::SESSION_SALT]);*/
return $this->encrypt_decrypt->decrypt($str, $allSessionsArray[GeneralConstant::SESSION_SALT]);
}
public function getName()
{
return 'general_extension';
}
}