<?php
namespace CorporateTrainingBundle\Controller;
use AppBundle\Controller\BaseController;
use CorporateTrainingBundle\Biz\Org\Service\OrgService;
use CorporateTrainingBundle\Common\OrgToolkit;
use Symfony\Component\HttpFoundation\Request;
class OrgController extends BaseController
{
public const MAX_ORG_COUNT = 1000;
public function orgSelectAction(Request $request, $domId = 'select-orgCode', $orgCode = '', $disabled = false)
{
return $this->render(
'@CorporateTraining/org-select/hinclude.html.twig',
['domId' => $domId, 'orgCode' => $orgCode, 'disabled' => $disabled]
);
}
public function oneSelfOrgSelectAction(Request $request)
{
$orgIds = [];
$content = json_decode($request->getContent(), true);
if (!empty($content['orgIds'])) {
$orgIds = $content['orgIds'];
}
return $this->render(
'@CorporateTraining/org-select/one-slef-hinclude.html.twig',
[
'domId' => $request->query->get('domId', 'select-orgIds'),
'hasModalOrg' => $request->query->get('hasModalOrg', 0),
'orgIds' => $orgIds,
'hasManageType' => $content['hasManageType'] ?? 0,
]
);
}
public function orgVisibleSelectAction(Request $request, $resourceType, $resourceId, $domId, $treeName = 'publishOrg', $disableUpdate = false)
{
return $this->render(
'@CorporateTraining/select/org-visible/hinclude.html.twig',
[
'resourceType' => $resourceType,
'resourceId' => $resourceId,
'domId' => $domId,
'treeName' => $treeName,
'disableUpdate' => $disableUpdate,
]
);
}
public function buildOrgTreeAction(Request $request)
{
$orgCodes = $request->request->get('orgCodes', '');
if (empty($orgCodes)) {
return $this->createJsonResponse();
}
$full = false;
$orgCount = $this->getOrgService()->countOrgs([]);
if ($orgCount <= self::MAX_ORG_COUNT) {
$tree = $this->getOrgService()->buildVisibleOrgTreeByOrgCodes($orgCodes);
$full = true;
return $this->createJsonResponse(
[
'tree' => $tree,
'full' => $full,
'more' => false,
]
);
}
$parentId = $request->request->get('parentId', 1);
$limit = $request->request->get('limit', self::MAX_ORG_COUNT);
$offset = $request->request->get('offset', 0);
$childrenCount = $this->getOrgService()->countOrgs(['parentId' => $parentId]);
$tree = [];
if (!empty($childrenCount)) {
$tree = $this->getOrgService()->buildOrgTreeByParentId(
$parentId,
$offset,
$limit,
true
);
}
return $this->createJsonResponse(
[
'tree' => $tree,
'full' => $full,
'more' => $childrenCount > (!empty($offset) ? $offset : $limit),
]
);
}
public function buildCanManageOrgTreeAction(Request $request)
{
$fields = $request->request->all();
$parentId = $fields['parentId'] ?? 1;
$limit = $fields['limit'] ?? self::MAX_ORG_COUNT;
$offset = $fields['offset'] ?? 0;
$fields['orgIds'] = empty($fields['orgIds']) ? 1 : $fields['orgIds'];
$settingOrgIds = !empty($fields['orgIds']) ? explode(',', $fields['orgIds']) : [];
$orgCodes = array_column($this->getOrgService()->findOrgsByIds($settingOrgIds), 'orgCode');
$childrenCount = $this->getOrgService()->countOrgs(['parentId' => $parentId]);
if (!empty($childrenCount)) {
$tree = $this->getOrgService()->buildCanManageOrgTreeByParentId(
$parentId,
$settingOrgIds,
$offset,
$limit,
true
);
} else {
$tree = $this->getOrgService()->findOrgsByOrgCodes($orgCodes);
$tree = empty($tree) ? $tree : $tree[0];
}
return $this->createJsonResponse(
[
'tree' => $this->buildManageTypeTree($request, $tree),
'full' => false,
'more' => $childrenCount > $offset + $limit,
]
);
}
public function orgMatchAction(Request $request)
{
$orgCondition = [
'name' => $request->query->get('name', ''),
];
$orgs = $this->getOrgService()->searchOrgs($orgCondition, ['id' => 'ASC'], 0, 20);
$orgPaths = OrgToolkit::buildOrgPathName(array_column($orgs, 'id'), 'short');
foreach ($orgs as &$org) {
$org['name'] = $orgPaths[$org['id']]['name'];
}
return $this->createJsonResponse($orgs);
}
protected function buildManageTypeTree(Request $request, $tree)
{
$hasManageType = $request->query->get('hasManageType', 0);
if (1 == $hasManageType && isset($tree['id']) && 1 == $tree['id']) {
$all = [
'id' => '',
'name' => $this->trans('group.detail.all_btn'),
'parentId' => 0,
'depth' => 1,
'seq' => 0,
'code' => '',
'orgCode' => '0.',
'selectable' => false,
'disableCheckbox' => true,
];
$all['nodes'][0] = [
'id' => 0,
'name' => $this->getCurrentUser()->isSuperAdmin() ? '指定管理员' : '仅自己',
'parentId' => 0,
'depth' => 1,
'seq' => 0,
'code' => '',
'orgCode' => '0.',
'selectable' => true,
'disableCheckbox' => false,
];
$all['nodes'][1] = $tree;
$tree = $all;
}
return $tree;
}
protected function getStrategyContext()
{
return $this->getBiz()['resource_scope_strategy_context'];
}
/**
* @return OrgService
*/
protected function getOrgService()
{
return $this->createService('CorporateTrainingBundle:Org:OrgService');
}
}