<?php
namespace OceanExpertBundle\Controller;
use CommerceGuys\Addressing\Formatter\DefaultFormatter;
use CommerceGuys\Addressing\Model\Address;
use CommerceGuys\Addressing\Repository\AddressFormatRepository;
use CommerceGuys\Addressing\Repository\CountryRepository;
use CommerceGuys\Addressing\Repository\SubdivisionRepository;
use DateTime;
use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\Query;
use OceanExpertBundle\Controller\Api\ApiReportsController;
use OceanExpertBundle\Entity\Institutions;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class InstitutionController extends AbstractController
{
public function getInstitutionByCountryAction(Request $request): JsonResponse
{
$institutionList = array();
if ($request->query->get('countryCode') == NULL) {
$institutionList = array(
'institution' => 'Select Country First.'
);
} else {
$countryCode = $request->query->get('countryCode');
$keyword = $request->query->get('keyword');
$repo = $this->getDoctrine()
->getRepository('OceanExpertBundle:Institutions');
$query = $repo->createQueryBuilder('a')
->select('distinct a.instName, a.idInst, a.instAddress, a.city, a.state, a.postcode, a.idInst, a.activated')
// ->where('a.instName LIKE :title')
->where('a.countryCode = :code')
->andWhere('a.activated in (0,1)')
->orderBy('a.instName', 'ASC')
// ->setParameter('title', '%'.$keyword.'%')
->setParameter('code', $countryCode)
->getQuery();
$institutionList = $query->getResult();
// $institutionList = array_column($institutionList, 'instName');
}
return new JsonResponse($institutionList);
}
/**
* get the informationa about a given institution
*
* @param Request $request
*
* @return JsonResponse
*/
public function getInstitutionAddressAction(Request $request): Response
{
$institutionDetails = array();
if ($request->query->get('instName') != NULL) {
$title = $request->query->get('instName');
$repo = $this->getDoctrine()
->getRepository('OceanExpertBundle:Institutions');
$query = $repo->createQueryBuilder('a')
->select('a.instName, a.instAddress, a.city, a.state, a.postcode, a.instTel,a.addr2')
->where('a.idInst = :idInst')
// ->setParameter('title',$title)
->setParameter('idInst', $title)
->getQuery();
$institutionDetails = $query->getResult();
return new JsonResponse($institutionDetails);
} else {
return new JsonResponse(
array(
'status' => 0,
'message' => 'need a name of the institution'
)
);
}
}
public function addInstitutionAction(Request $request): Response
{
$data = $request->request->all();
$security_context = $this->get('security.authorization_checker');
if ($security_context->isGranted('IS_AUTHENTICATED_FULLY')) {
$em = $this->getDoctrine()->getManager();
$userId = $this->get('security.token_storage')->getToken()->getUser()->getId();
//let's check if the logged-in user has a 'real' profile
//meaning there is at least a firstname, lastname and language
// Arno 21/06/23 : we will not check this for the time being see alse #521
//if people cannot create their institute while registering they will not make / add it later
/*
if (!SecurityController::checkUserProfile($em, $userId)) {
return $this->redirect(
$this->generateUrl(
'user_profile_edit'
)
);
}
*/
if (trim($request->request->get("iedmo")) == ''
|| !is_numeric($request->request->get("iedmo"))
) {
$edmo_code = 0;
} else {
$edmo_code = $request->request->get("iedmo");
}
$institutionDetails = array();
$repo = $this->getDoctrine()
->getRepository('OceanExpertBundle:Institutions');
$query = $repo->createQueryBuilder('a')
->select('a.instName, a.instAddress, a.city, a.state, a.postcode, a.instTel')
->Where('a.instName like :name')
->setParameter('name', $request->request->get("instName"))
->getQuery();
$institutionDetails = $query->getResult();
if (count($institutionDetails) == 0) {
try {
$institution = new Institutions();
$institution->setCountryCode($request->request->get('countryCode'));
$institution->setInstName($request->request->get('instName'));
$institution->setInstNameEng($request->request->get('instNameEng'));
$institution->setInstTypeID($request->request->get('instType'));
$institution->setParentId($request->request->get('parentinstitute'));
if (null == $institution->getParentId()) {
$institution->setParentId(0);
}
$institution->setInstAddress($request->request->get('iaddress'));
$institution->setAcronym($request->request->get('iacronym'));
$institution->setAddr2($request->request->get('iaddress2'));
$institution->setPostcode($request->request->get('ipostcode'));
$institution->setCity($request->request->get('icity'));
$institution->setState($request->request->get('istate'));
$institution->setInstTel($request->request->get('icontact'));
$institution->setInstFax($request->request->get('ifax'));
$institution->setInstEmail($request->request->get('iemail'));
$institution->setInstURL($request->request->get('iwebsite'));
$institution->setEdmoCode($edmo_code);
$institution->setActivities($request->request->get('iactivity'));
if (null !== $request->request->get('isearegion')
&& is_array($request->request->get('isearegion'))
&& count($request->request->get('isearegion')) > 1
) {
$iseaRegion = implode(',', $request->request->get('isearegion'));
} else {
$iseaRegion = '';
}
$institution->setInstRegion($iseaRegion);
$institution->setPopularity(0);
if ($this->get('security.authorization_checker')->isGranted('ROLE_LME')) {
$institution->setActivated(1);
} else {
$institution->setActivated(0);
}
$institution->setFDateEntered(new DateTime('now'));
$institution->setLDateUpdated(new DateTime('now'));
$institution->setCreatedBy($userId);
$em = $this->getDoctrine()->getManager();
$em->persist($institution); //marks object to be saved in the next transaction.
$em->flush(); //performs all saves and transactions.
if (null != $request->files->get('instLogo')) {
$file = $request->files->get('instLogo');
$filetype = $file->guessClientExtension();
$filename = 'instituteLogo.jpg';
$directory = 'uploads/institutes/' . $institution->getIdInst() . '/';
$uploadfile = $file->move($directory, $filename);
$institution->setInstLogo($filename);
} else {
if (isset($data['textLogo']) && $data['textLogo'] == '') {
$institution->setInstLogo('');
$filename = 'uploads/institutes/' . $institution->getIdInst() . '/instituteLogo.jpg';
if (file_exists($filename)) {
unlink($filename);
}
}
}
$em->persist($institution);
$em->flush();
return new JsonResponse(
array(
'status' => 1,
'id' => $institution->getIdInst(),
'message' => '<br /><br /><h3 class="text-success">institution created</h3>
Your new institution has been created successfully.<br />
Please select your newly created institution in the list.
<br /><br />'
)
);
} catch (Exception $e) {
return new JsonResponse(
array(
'status' => 0,
'message' => 'Error in adding Institution. Please try again later.'
)
);
}
} else {
return new JsonResponse(
array(
'status' => 2,
'message' => 'Institute name/acronym already exist.'
)
);
}
} else {
return new JsonResponse(
array(
'status' => 2,
'message' => 'you do not have the correct rights to do this'
)
);
}
}
/**
* show all existing institutes
*
* @param Request $request
*
* @return Response|void
*/
function viewInstitutesAction( Request $request ): Response
{
$em = $this->get('doctrine')->getManager();
$connection = $em->getConnection();
$limits = array(
10 => 10,
25 => 25,
50 => 50,
100 => 100,
500 => 500
);
$limit = $request->query->get('limit', 10);
$query = $request->query->get('search', '');
$orderby = $request->query->get('orderby', 'order');
$dir = $request->query->get('dir', 'asc');
if (!in_array($orderby, array('id', 'title'))) {
$orderby = 'title';
}
if (!in_array($dir, array('asc', 'desc'))) {
$dir = 'asc';
}
//get the activated institutes
$repo = $this->getDoctrine()
->getRepository('OceanExpertBundle:Institutions');
$institutes = $repo->createQueryBuilder('i');
$institutes->select('
i.idInst,
i.parentId,
i.instTypeId,
i.instName,
i.instNameEng,
i.countryCode,
c.country,
it.insttypeName'
);
$institutes->leftJoin(
'OceanExpertBundle:Countries',
'c',
'WITH',
'i.countryCode = c.idCountry');
$institutes->leftJoin(
'OceanExpertBundle:Insttypes',
'it',
'WITH',
'i.instTypeId = it.idInsttype');
if (!$this->get('security.authorization_checker')->isGranted('ROLE_SUPERADMIN')
&& !$this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')
) {
$institutes->where('i.activated = 1');
}
//store all the found institutes to be able to find the name of the parents
$institutesQuery = $institutes->getQuery();
$institutesTmp = $institutesQuery->execute();
foreach($institutesTmp as $institute) {
$parentInstitutes[$institute['idInst']] = $institute['instName'];
}
if (trim($query) != '') {
$institutes->andwhere('i.instName LIKE :query OR i.instNameEng LIKE :query');
$institutes->setParameter(
'query',
'%' . trim($query) . '%'
);
}
$institutes->getQuery();
$paginator = $this->get('knp_paginator');
$data = $paginator->paginate(
$institutes,
$request->query->getInt('page', 1),
$limit,
array(
'pageParameterName' => 'page',
'sortDirectionParameterName' => 'dir',
'defaultSortFieldName' => 'i.idInst',
'defaultSortDirection' => 'desc'
)
);
$data->setCustomParameters([
'limits' => $limits,
'title' => 'Institutes'
]);
return $this->render(
'Institution/viewInstitutes.html.twig',
array(
'institutesData' => $data,
'parentInstitutes' => $parentInstitutes
)
);
}
public function viewInstitutionAction($instId, Request $request)
{
/*
* container to hold to OIH/ODIS JSON-LD info
* see also https://book.oceaninfohub.org/thematics/expinst/README.html
* in the end we will need something like
* {
"@context": {
"@vocab": "https://schema.org/"
},
"@id": "https://example.org/id/org/x",
"@type": "Organization",
"address": {
"@type": "PostalAddress",
"addressLocality": "Paris, France",
"postalCode": "F-75002",
"streetAddress": "38 avenue de l'Opera"
},
"email": "secretariat(at)example.org",
"name": "Organization X",
"description": "Description of org ...",
"telephone": "( 33 1) 42 68 53 00",
"member": [
{
"@type": "Organization",
"name": "Organization A",
"description": "Org A is a potential parent organization of Org X"
}
],
"identifier": {
"@id": "https://grid.ac/institutes/grid.475727.4",
"@type": "PropertyValue",
"description": "UN Department of Economic and Social Affairs Sustainable Development",
"propertyID": "https://registry.identifiers.org/registry/grid",
"url": "https://grid.ac/institutes/grid.475727.4"
}
}
*/
$OIHData = array(
'@context' => array(
'@vocab' => 'https://schema.org/'
),
'@id' => "https://oceanexpert.org/institution/$instId",
'@type' => 'Organization'
);
$qb = $this->getDoctrine()->getManager()->createQueryBuilder();
$qb->add(
'select',
'ins.idInst,
ins.instTypeId,
ins.instName,
ins.instNameEng,
ins.instAddress,
ins.addr2,
ins.city,
ins.state,
ins.postcode,
ins.countryCode,
ins.acronym,
ins.instLogo,
ins.instTel,
ins.instFax,
ins.instEmail,
ins.instUrl,
ins.edmoCode,
ins.instRegion,
ins.activities,
ins.popularity,
ins.activated,
ins.fDateEntered,
ins.lDateUpdated,
ins.createdBy,
ins.lastEditBy,
it.insttypeName,
c.country,
c.countryCode as instCtryCode,
ins.parentId as parentIdInst,
p.instName as parentInstName,
p.instNameEng as parentInstNameEng,
p.acronym as parentAcronym,
p.instLogo as parentInstLogo')
->add(
'from',
'OceanExpertBundle:Institutions ins')
->leftJoin(
'OceanExpertBundle:Insttypes',
'it',
'WITH',
'it.idInsttype = ins.instTypeId')
->leftJoin(
'OceanExpertBundle:Countries',
'c',
'WITH',
'c.idCountry = ins.countryCode')
->leftJoin(
'OceanExpertBundle:Institutions',
'p',
'WITH',
'p.idInst = ins.parentId AND p.activated = 1')
->where('ins.idInst = :instId')
->setParameter(
'instId',
$instId
);
$institution = $this->getDoctrine()
->getRepository('OceanExpertBundle:Institutions')
->findOneByIdInst($instId);
$userId = '-1';
$security_context = $this->get('security.authorization_checker');
if ($security_context->isGranted('IS_AUTHENTICATED_FULLY')) {
$userId = $this->get('security.token_storage')->getToken()->getUser()->getId();
}
if (isset($institution)
&& (($security_context->isGranted('ROLE_GLOBAL_EDITOR'))
or ($userId == $institution->getCreatedBy())
or ($userId == $institution->getLastEditBy())
)
) {
//@todo what happens here??? Arno 23/06/2021
} else {
$qb->andWhere('ins.activated = 1');
}
$routeName = $request->get('_route');
$institute = $qb->getQuery()->getResult();
if (!$institute) {
return $this->render(
'Default/error.html.twig',
array(
'message' => 'Institution does not exists or the institution has not been approved by OceanExpert editor.'
)
);
}
$OIHData['name'] = $institute[0]['instName'];
if (isset($institute[0]['instUrl'])
&& $institute[0]['instUrl'] != ''
) {
$OIHData['url'] = $institute[0]['instUrl'];
} else {
$OIHData['url'] = "https://oceanexpert.org/institution/$instId";
}
if (isset($institute[0]['instTel'])
&& $institute[0]['instTel'] != ''
) {
$OIHData['telephone'] = $institute[0]['instTel'];
}
if (isset($institute[0]['parentIdInst'])
&& $institute[0]['parentIdInst'] != ''
&& $institute[0]['parentIdInst'] != 0
) {
$OIHData['memberOf'] = array(
'@type' => 'Organization',
'@id' => 'https://oceanexpert.org/institution/' . $institute[0]['parentIdInst'],
'url' => 'https://oceanexpert.org/institution/' . $institute[0]['parentIdInst'],
'name' => $institute[0]['parentInstName']
);
}
$qb = $this->getDoctrine()->getManager()->createQueryBuilder();
$qb->add(
'select',
'i.idInd,
i.fname,
i.mname,
i.sname,
i.jobtitle,
i.deceased,
i.retired,
i.qualityChecked')
->add(
'from',
'OceanExpertBundle:Indiv i')
->leftJoin(
'OceanExpertBundle:IndivInstitution',
'iins',
'WITH',
'iins.idInd = i.idInd')
->leftJoin(
'OceanExpertBundle:Institutions',
'ins',
'WITH',
'ins.idInst = iins.idInst')
->where('ins.idInst = :instId')
->andWhere('i.status = 1')
->orderBy(
'i.sname',
'ASC')
->setParameter(
'instId',
$instId
);
$resultData = $qb->getQuery()->getResult();
if (is_array($resultData)) {
foreach ($resultData as $member) {
$OIHData['member'][] = array(
'member' => array(
'@type' => 'Person',
'@id' => 'https://oceanexpert.org/expert/' . $member['idInd'],
'url' => 'https://oceanexpert.org/expert/' . $member['idInd'],
'name' => $member['fname'] . ' ' . $member['mname'] . ' ' . $member['sname']
)
);
}
}
$paginator = $this->get('knp_paginator');
$memberlimit = $request->query->getInt('mlimit', 5);
$memberpage = $request->query->getInt('members', 1);
if (!in_array($memberlimit, array(5, 10, "All"))) {
$memberlimit = 5;
$memberpage = 1;
}
if ($memberlimit == "All") {
$memberlimit = 9999;
$memberpage = 1;
}
$members = $paginator->paginate(
$resultData,
$memberpage,
$memberlimit,
array(
'pageParameterName' => 'members',
'sortDirectionParameterName' => 'dir'
)
);
// child members
$qb = $this->getDoctrine()->getManager()->createQueryBuilder();
$qb->add(
'select',
'i.idInd,
i.fname,
i.mname,
i.sname,
i.jobtitle,
i.deceased,
i.retired,
i.qualityChecked,
ins.idInst,
ins.instName')
->add(
'from',
'OceanExpertBundle:Indiv i')
->leftJoin(
'OceanExpertBundle:IndivInstitution',
'iins',
'WITH',
'iins.idInd = i.idInd')
->leftJoin(
'OceanExpertBundle:Institutions',
'ins',
'WITH',
'ins.idInst = iins.idInst')
->where('ins.parentId = :instId')
->andWhere('i.status = 1')
->andWhere('ins.activated = 1')
->orderBy(
'i.sname',
'ASC')
->setParameter(
'instId',
$instId
);
$resultData = $qb->getQuery()->getResult();
if (is_array($resultData)) {
foreach ($resultData as $member) {
$instUrl = 'https://oceanexpert.org/institution/' . $member['idInst'];
$instName = $member['instName'];
$OIHData['member'][] = array(
'member' => array(
'@type' => 'Person',
'@id' => 'https://oceanexpert.org/expert/' . $member['idInd'],
'url' => 'https://oceanexpert.org/expert/' . $member['idInd'],
'name' => $member['fname'] . ' ' . $member['mname'] . ' ' . $member['sname'],
'description' => "is member of '$instName' ($instUrl), a child of this organization"
)
);
}
}
$childMembers = array();
$childMemberLimit = $request->query->get('cmlimit');
if (!in_array($childMemberLimit, array(5, 10, "All"))) {
$childMemberLimit = 5;
}
if ($childMemberLimit == "All") {
$childMemberLimit = 9999;
}
$childMembers = $paginator->paginate(
$resultData,
$request->query->getInt('childmembers', 1),
$childMemberLimit,
array(
'pageParameterName' => 'childmembers',
'sortDirectionParameterName' => 'dir'
)
);
// child institutes
$qb = $this->getDoctrine()->getManager()->createQueryBuilder();
$qb->add(
'select',
'ins.idInst,
ins.instTypeId,
ins.instName,
ins.instNameEng,
ins.instAddress,
ins.addr2,
ins.city,
ins.state,
ins.postcode,
ins.countryCode,
ins.acronym,
ins.instLogo,
ins.instTel,
ins.instFax,
ins.instEmail,
ins.instUrl,
ins.edmoCode,
ins.instRegion,
ins.activities,
ins.popularity,
ins.activated,
ins.fDateEntered,
ins.lDateUpdated,
ins.createdBy,
ins.lastEditBy,
it.insttypeName,
c.country,
c.countryCode as instCtryCode')
->add(
'from',
'OceanExpertBundle:Institutions ins')
->leftJoin(
'OceanExpertBundle:Insttypes',
'it',
'WITH',
'it.idInsttype = ins.instTypeId')
->leftJoin(
'OceanExpertBundle:Countries',
'c',
'WITH',
'c.idCountry = ins.countryCode')
->where('ins.parentId = :instId')
->andWhere('ins.activated = 1')
->orderBy(
'ins.instName',
'ASC')
->setParameter(
'instId',
$instId);
$resultData = $qb->getQuery()->getResult();
if (is_array($resultData)) {
foreach ($resultData as $member) {
$OIHData['member'][] = array(
'member' => array(
'@type' => 'Organization',
'@id' => 'https://oceanexpert.org/institution/' . $member['idInst'],
'url' => 'https://oceanexpert.org/institution/' . $member['idInst'],
'name' => $member['instName']
)
);
}
}
$childInstitutes = array();
$childInstituteLimit = $request->query->get('cilimit');
if (!in_array($childInstituteLimit, array(5, 10, "All"))) {
$childInstituteLimit = 5;
}
if ($childInstituteLimit == "All") {
$childInstituteLimit = 9999;
}
$childInstitutes = $paginator->paginate(
$resultData,
$request->query->getInt('childinstitutes', 1),
$childInstituteLimit,
array(
'pageParameterName' => 'childinstitutes',
'sortDirectionParameterName' => 'dir'
)
);
$repository = $this->getDoctrine()
->getRepository('OceanExpertBundle:Regions');
$institute[0]['seaRegions'] = $repository->createQueryBuilder('r')
->select(
'r.idRegion,
r.name')
->where('r.idRegion IN(:seaRegions) ')
->setParameter(
'seaRegions',
array_values(
explode(
',',
$institute[0]['instRegion']
)
)
)
->getQuery()->getResult();
$institute[0]['availableSeaRegions'] = $this->getDoctrine()
->getRepository('OceanExpertBundle:Regions')
->createQueryBuilder('e')
->select('e.idRegion as id, e.name')
->orderBy('e.name')
->getQuery()
->getResult(AbstractQuery::HYDRATE_ARRAY);
if ($routeName == 'edit_institution') {
$securityContext = $this->container->get('security.authorization_checker');
if ($securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
$user = $this->container->get('security.token_storage')->getToken()->getUser();
//let's check if the logged-in user has a 'real' profile
//meaning there is at least a firstname, lastname and language
$em = $this->getDoctrine()->getManager();
$userId = $this->get('security.token_storage')->getToken()->getUser()->getId();
if (!SecurityController::checkUserProfile($em, $userId)) {
return $this->redirect(
$this->generateUrl(
'user_profile_edit'
)
);
}
if ($user->getId() == $institute[0]['createdBy']
|| $this->get('security.authorization_checker')->isGranted('ROLE_GLOBAL_EDITOR')
) {
if (!empty($data = $request->request->all())) {
$em = $this->getDoctrine()->getManager();
$inst = $em
->getRepository('OceanExpertBundle:Institutions')
->findOneBy(
array(
'idInst' => $data['instId']
)
);
if ($inst) {
if (trim($data['iedmo']) == ''
|| !is_numeric($data['iedmo'])
) {
$edmo_code = 0;
} else {
$edmo_code = $data['iedmo'];
}
$inst->setInstTypeId($data['instType']);
$inst->setInstName($data['instName']);
$inst->setInstNameEng($data['instNameEng']);
$inst->setInstAddress($data['iaddress']);
$inst->setAddr2($data['iaddressline2']);
$inst->setCity($data['icity']);
$inst->setState($data['istate']);
$inst->setPostcode($data['ipostcode']);
$inst->setCountryCode($data['icountryCode']);
$inst->setAcronym($data['iacronym']);
$inst->setInstTel($data['icontact']);
$inst->setInstFax($data['ifax']);
$inst->setInstEmail($data['iemail']);
$inst->setInstUrl($data['iwebsite']);
$inst->setEdmoCode($edmo_code);
$inst->setActivities($data['iactivity']);
$inst->setParentId($data['parentinstitute']);
if (null == $institution->getParentId()) {
$institution->setParentId(0);
}
if (isset($data['studyregion'])
&& count($data['studyregion']) > 0
) {
$inst->setInstRegion(implode(',', $data['studyregion']));
} else {
$inst->setInstRegion('');
}
$inst->setLDateUpdated(new DateTime("now"));
$inst->setLastEditBy($user->getId());
$em->persist($inst);
$em->flush();
if (null != $request->files->get('instLogo')) {
$file = $request->files->get('instLogo');
$filetype = $file->guessClientExtension();
$filename = 'instituteLogo.jpg';
$directory = 'uploads/institutes/' . $inst->getIdInst() . '/';
$uploadfile = $file->move($directory, $filename);
$inst->setInstLogo($filename);
} else {
if (isset($data['textLogo'])
&& $data['textLogo'] == ''
) {
$inst->setInstLogo('');
$filename = 'uploads/institutes/' . $inst->getIdInst() . '/instituteLogo.jpg';
if (file_exists($filename)) {
unlink($filename);
}
}
}
$em->persist($inst);
$em->flush();
if ($inst) {
return $this->redirect(
$this->generateUrl(
'edit_institute_success',
array(
'instId' => $data['instId']
)
)
);
}
}
}
$countries = $this->getDoctrine()
->getRepository('OceanExpertBundle:Countries')
->findBy(
[],
['country' => 'ASC']
);
return $this->render(
'Institution/editinstitution.html.twig',
array(
'institute' => $institute[0],
'countries' => $countries
)
);
} else {
return $this->redirect(
$this->generateUrl(
'frontend_homepage'
)
);
}
} else {
return $this->redirect(
$this->generateUrl(
'frontend_homepage'
)
);
}
}
$addressFormatRepository = new AddressFormatRepository();
$countryRepository = new CountryRepository();
$subdivisionRepository = new SubdivisionRepository();
$formatter = new DefaultFormatter($addressFormatRepository, $countryRepository, $subdivisionRepository);
$address = new Address();
$address = $address
->withCountryCode($institute[0]['instCtryCode'])
->withAdministrativeArea($institute[0]['state'])
->withLocality($institute[0]['city'])
->withPostalCode($institute[0]['postcode'])
->withAddressLine2($institute[0]['addr2'])
->withAddressLine1($institute[0]['instAddress']);
$institute[0]['address'] = $formatter->format($address);
//make the address
$streetAddress = $institute[0]['instAddress'];
if (trim($institute[0]['addr2']) != '') {
$streetAddress .= ', ' . trim($institute[0]['addr2']);
}
$address = $streetAddress . ', ' .
$institute[0]['postcode'] . ' ' . $institute[0]['city'] . ', ' .
$institute[0]['country'];
$OIHData['location'] = array(
'@type' => 'Place',
'address' => trim($address)
);
$OIHData['address'] = array(
'@type' => 'PostalAddress',
'addressLocality' => $institute[0]['city'] . ', ' . $institute[0]['country'],
'postalCode' => $institute[0]['postcode'],
'streetAddress' => $streetAddress
);
/*
$OIHData['location'] = array(
'@type' => 'Place',
'address' => array(
'@type' => 'PostalAddress',
'addressLocality' => $institute[0]['city'] . ', ' . $institute[0]['country'],
'postalCode' => $institute[0]['postcode'],
'streetAddress' => $streetAddress
)
);
*/
if ($institute[0]['createdBy']) {
$institute[0]['createdId'] = $institute[0]['createdBy'];
$institute[0]['createdBy'] = $this->getUserById($institute[0]['createdBy']);
}
if ($institute[0]['lastEditBy']) {
$institute[0]['lastEditId'] = $institute[0]['lastEditBy'];
$institute[0]['lastEditBy'] = $this->getUserById($institute[0]['lastEditBy']);
}
$limits = array(
'memberlimit' => array(
'options' => array(
'5' => 5,
'10' => 10,
'9999' => 'All'
),
'selected' => $memberlimit
),
'childmemberlimit' => array(
'options' => array(
'5' => 5,
'10' => 10,
'9999' => 'All'
),
'selected' => $childMemberLimit
),
'childinstitutelimit' => array(
'options' => array(
'5' => 5,
'10' => 10,
'9999' => 'All'
),
'selected' => $childInstituteLimit
),
);
$OIHData = json_encode(
$OIHData,
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK
);
return $this->render(
'Institution/viewinstitution.html.twig',
array(
'OIHData' => $OIHData,
'institute' => $institute[0],
'members' => $members,
'limits' => $limits,
'childmembers' => $childMembers,
'childinstitutes' => $childInstitutes
)
);
}
public function getUserById($id = '')
{
$updatedBy = $this->getDoctrine()->getRepository('OceanExpertBundle:Indiv')->findOneByIdInd($id);
if ($updatedBy) {
$name = $updatedBy->getfname() . " " . $updatedBy->getsname();
} else {
$name = false;
}
return $name;
}
public function activateInstituteAction($instituteId)
{
$security_context = $this->get('security.authorization_checker');
if ($security_context->isGranted('ROLE_GLOBAL_EDITOR')) {
if ($instituteId) {
$institute = $this->getDoctrine()
->getRepository('OceanExpertBundle:Institutions')
->findOneByIdInst($instituteId);
if ($institute) {
$institute->setActivated(1);
$institute->setLDateUpdated(new DateTime("now"));
$institute->setLastEditBy(
$this->get('security.token_storage')
->getToken()
->getUser()
->getId()
);
$em = $this->getDoctrine()->getManager();
$em->persist($institute); //marks object to be saved in the next transaction.
$em->flush(); //performs all saves and transactions.
}
return new JsonResponse(
array(
'status' => true,
'msg' => 'Record updated successfully'
)
);
} else {
return new Response('Cannot update record.');
}
}
return new Response('Cannot update record.');
}
public function deactivateInstituteAction($instituteId)
{
$security_context = $this->get('security.authorization_checker');
if ($security_context->isGranted('ROLE_GLOBAL_EDITOR')) {
if ($instituteId) {
$institute = $this->getDoctrine()
->getRepository('OceanExpertBundle:Institutions')
->findOneByIdInst($instituteId);
if ($institute) {
$institute->setActivated(0);
$institute->setLDateUpdated(new DateTime("now"));
$institute->setLastEditBy(
$this->get('security.token_storage')
->getToken()
->getUser()
->getId());
$em = $this->getDoctrine()->getManager();
$em->persist($institute); //marks object to be saved in the next transaction.
$em->flush(); //performs all saves and transactions.
}
return new JsonResponse(
array(
'status' => true,
'msg' => 'updated successfully'
)
);
} else {
return new Response('Cannot update record.');
}
}
return new Response('Cannot update record.');
}
public function editInstituteSuccessAction($instId)
{
return $this->render(
'Institution/editInstitutionSuccess.html.twig',
array(
'institute' => $instId
)
);
}
/**
* @param Request $request
*
* @return Response
*/
public function getInstitutionsAjaxAction(Request $request)
{
if (!null == $request->query->get('q')) {
$inst = $request->query->get('q');
$em = $this->getDoctrine()->getManager();
$qb = $em->createQueryBuilder();
$qb->select('i.idInst as id,i.instName');
$qb->from('OceanExpertBundle:Institutions', 'i');
$qb->where('i.instName like :name OR i.acronym like :name');
if (!null == $request->query->get('instId')) {
$qb->andWhere('i.idInst != :instId');
$qb->setParameter('instId', $request->query->get('instId'));
}
$qb->andWhere('i.activated = 1');
$qb->setParameter('name', '%' . $inst . '%');
$qb->setParameter('name', '%' . $inst . '%');
$qb->orderBy('i.instName', 'ASC');
$institutes = $qb->getQuery()->getResult();
$data = array();
if ($institutes) {
$data = array(
'incomplete_results' => false,
'items' => $institutes,
'total_count' => count($institutes),
);
} else {
$data = array(
'incomplete_results' => false,
'items' => array(),
'total_count' => 0,
);
}
return new JsonResponse($data);
}
return new JsonResponse();
}
}