/** * @param BuildBefore $event * @return bool */ public function onBuildBefore(BuildBefore $event) { $datagrid = $event->getDatagrid(); $config = $event->getConfig(); $entityClassName = $datagrid->getParameters()->get('class_name'); $fieldName = $datagrid->getParameters()->get('field_name'); $entityId = $datagrid->getParameters()->get('id'); $extendConfigProvider = $this->configManager->getProvider('extend'); $extendFieldConfig = $extendConfigProvider->getConfig($entityClassName, $fieldName); $targetEntityName = $extendFieldConfig->get('target_entity'); $targetFieldNames = $extendFieldConfig->get('target_grid'); // build 'assigned' field expression if ($entityId) { $extendEntityConfig = $extendConfigProvider->getConfig($entityClassName); $relations = $extendEntityConfig->get('relation'); $relation = $relations[$extendFieldConfig->get('relation_key')]; $targetFieldName = $relation['target_field_id']->getFieldName(); $fieldType = $extendFieldConfig->getId()->getFieldType(); $operator = $fieldType == 'oneToMany' ? '=' : 'MEMBER OF'; $whenExpr = '(:relation ' . $operator . ' o.' . $targetFieldName . ' OR o.id IN (:data_in))' . ' AND o.id NOT IN (:data_not_in)'; } else { $whenExpr = 'o.id IN (:data_in) AND o.id NOT IN (:data_not_in)'; } $assignedExpr = "CASE WHEN " . $whenExpr . " THEN true ELSE false END"; // build a query skeleton $query = ['select' => ['o.id', $assignedExpr . ' as assigned'], 'from' => [['table' => $targetEntityName, 'alias' => 'o']]]; $config->offsetSetByPath('[source][query]', $query); // enable AdditionalFieldsExtension to add all other fields $config->offsetSetByPath('[options][entity_name]', $targetEntityName); $config->offsetSetByPath('[options][additional_fields]', $targetFieldNames); }
/** * @param BuildBefore $event */ public function statusGridBuildBefore(BuildBefore $event) { $params = $event->getDatagrid()->getParameters(); if ($params->has('integrationType')) { $type = $params->get('integrationType'); $connectorChoices = $this->typesRegistry->getAvailableConnectorsTypesChoiceList($type); $event->getDatagrid()->getConfig()->offsetSetByPath('[filters][columns][connector][options][field_options][choices]', $connectorChoices); } }
/** * Need to change data name depends to filter value * * Event: oro_datagrid.datagrid.build.before.orocrm_report-opportunities-won_by_period * * @param BuildBefore $event */ public function onBuildBefore(BuildBefore $event) { $config = $event->getConfig(); $filters = $event->getDatagrid()->getParameters()->get(OrmFilterExtension::FILTER_ROOT_PARAM, []); $period = isset($filters[self::PERIOD_COLUMN_NAME]['value']) ? $filters[self::PERIOD_COLUMN_NAME]['value'] : self::PERIOD_FILTER_DEFAULT_VALUE; $config->offsetSetByPath(sprintf('%s[%s][%s]', FilterConfiguration::COLUMNS_PATH, self::PERIOD_COLUMN_NAME, FilterUtility::DATA_NAME_KEY), $period); $config->offsetSetByPath(sprintf('[%s][%s][%s]', FormatterConfiguration::COLUMNS_KEY, self::PERIOD_COLUMN_NAME, PropertyInterface::DATA_NAME_KEY), $period); // in order to meet sql standards on some RDBMS (e.g. Postgres) // unset columns not used in aggregation or group by statements $path = '[source][query][select]'; $aliasFields = ['monthPeriod', 'monthPeriodSorting', 'quarterPeriod', 'quarterPeriodSorting', 'yearPeriod']; foreach ($aliasFields as $index => $alias) { // skip current period alias and it's sorting helper alias if ($alias == $period || str_replace('Sorting', '', $alias) == $period) { continue; } $config->offsetUnsetByPath(sprintf('%s[%s]', $path, $index)); } // and setup separate sorting column, used as well in grouping, but not affecting grouping result // period will be always the first column, unless changed in datagrid.yml if ($period == 'yearPeriod') { $groupAlias = $period; $sortAlias = $period; } else { $groupAlias = $period; $sortAlias = sprintf('%sSorting', $period); } $config->offsetSetByPath('[source][query][groupBy]', $groupAlias); $config->offsetSetByPath(sprintf('%s[%s][%s]', OrmSorterConfiguration::COLUMNS_PATH, self::PERIOD_COLUMN_NAME, PropertyInterface::DATA_NAME_KEY), $sortAlias); }
/** * @param BuildBefore $event */ public function onBuildBefore(BuildBefore $event) { $config = $event->getConfig(); $fieldName = $event->getDatagrid()->getParameters()->get(self::GRID_PARAM_FIELD_NAME); $leftJoins = $config->offsetGetByPath(self::GRID_LEFT_JOIN_PATH, []); $leftJoins[] = ['join' => 'attachment.' . $fieldName, 'alias' => 'entity']; $config->offsetSetByPath(self::GRID_LEFT_JOIN_PATH, $leftJoins); }
/** * @param BuildBefore $event */ public function onBuildBefore(BuildBefore $event) { $dataGrid = $event->getDatagrid(); $dataGridName = $dataGrid->getName(); $parameters = $dataGrid->getParameters(); if ($this->isApplicable($dataGridName, $parameters)) { $event->stopPropagation(); } }
/** * Need to change data name depends to filter value * * Event: oro_datagrid.datagrid.build.before.orocrm_report-opportunities-won_by_period * * @param BuildBefore $event */ public function onBuildBefore(BuildBefore $event) { $config = $event->getConfig(); $filters = $event->getDatagrid()->getParameters()->get(OrmFilterExtension::FILTER_ROOT_PARAM, []); $period = isset($filters[self::PERIOD_COLUMN_NAME]['value']) ? $filters[self::PERIOD_COLUMN_NAME]['value'] : self::PERIOD_FILTER_DEFAULT_VALUE; $config->offsetSetByPath(sprintf('%s[%s][%s]', FilterConfiguration::COLUMNS_PATH, self::PERIOD_COLUMN_NAME, FilterUtility::DATA_NAME_KEY), $period); $config->offsetSetByPath(sprintf('%s[%s][%s]', OrmSorterConfiguration::COLUMNS_PATH, self::PERIOD_COLUMN_NAME, PropertyInterface::DATA_NAME_KEY), $period); $config->offsetSetByPath(sprintf('[%s][%s][%s]', FormatterConfiguration::COLUMNS_KEY, self::PERIOD_COLUMN_NAME, PropertyInterface::DATA_NAME_KEY), $period); }
/** * @param BuildBefore $event */ public function onBuildBefore(BuildBefore $event) { $datagrid = $event->getDatagrid(); $config = $event->getConfig(); // remember the current datagrid to further usage in getLinkProperty method $this->addVisitedDatagrid($datagrid); // enable DynamicFieldsExtension to add custom fields $config->offsetSet('extended_entity_name', $datagrid->getParameters()->get('class_name')); }
public function testEventCreation() { $grid = $this->getMockForAbstractClass('Oro\\Bundle\\DataGridBundle\\Datagrid\\DatagridInterface'); $config = DatagridConfiguration::create([]); $event = new BuildBefore($grid, $config); $this->assertSame($grid, $event->getDatagrid()); $this->assertSame($config, $event->getConfig()); // test config passed as link $event->getConfig()->offsetSet(self::TEST_STRING, self::TEST_STRING . 'value'); $this->assertEquals(self::TEST_STRING . 'value', $config->offsetGet(self::TEST_STRING)); }
/** * Remove useless fields in case of filtering * * @param BuildBefore $event */ public function onBuildBefore(BuildBefore $event) { $config = $event->getConfig(); $parameters = $event->getDatagrid()->getParameters(); if ($parameters->has('contactId')) { $this->removeColumn($config, 'contactName'); } if ($parameters->has('accountId')) { $this->removeColumn($config, 'accountName'); } }
/** * {@inheritdoc} */ public function onBuildBefore(BuildBefore $event) { $datagrid = $event->getDatagrid(); list($entityName, $alias) = $this->getEntityNameWithAlias($datagrid); $alias = $alias ?: 'e'; if (!$this->isApplicable($event, $entityName)) { return; } // configure mass action $event->getConfig()->offsetSetByPath(sprintf(self::ACTION_CONFIGURATION_KEY, MassUpdateActionHandler::ACTION_NAME), ['type' => 'window', 'frontend_type' => 'update-mass', 'route' => 'oro_datagrid_mass_action', 'dialogWindowOptions' => ['route' => 'trustify_mass_update', 'route_parameters' => ['entityName' => str_replace('\\', '_', $entityName)]], 'data_identifier' => $alias . '.' . $this->doctrineHelper->getSingleEntityIdentifierFieldName($entityName), 'handler' => MassUpdateActionHandler::SERVICE_ID, 'label' => 'trustify.mass_update.dialog.title', 'success_message' => 'trustify.mass_update.success_message', 'error_message' => 'trustify.mass_update.error_message']); }
/** * @param BuildBefore $event */ public function onBuildBefore(BuildBefore $event) { $request = $this->requestStack->getCurrentRequest(); if ($request) { $entityId = $request->get('entityId'); $entityClass = $request->get('entityClass'); $shareGridParams = $request->get('shared-datagrid'); if ($shareGridParams) { $entityId = $shareGridParams['entityId']; $entityClass = $shareGridParams['entityClass']; } $event->getDatagrid()->getParameters()->add(['entityId' => $entityId, 'entityClass' => $entityClass]); } }
/** * @param BuildBefore $event */ public function onBuildBefore(BuildBefore $event) { $config = $event->getConfig(); $parameters = $event->getDatagrid()->getParameters(); $permission = $parameters->get('permission'); $entityClass = str_replace('_', '\\', $parameters->get('entity')); $entityId = $parameters->get('entity_id'); if ($entityId) { $object = $this->em->getRepository($entityClass)->find((int) $entityId); } else { $object = 'entity:' . $entityClass; } $observer = new OneShotIsGrantedObserver(); $this->aclVoter->addOneShotIsGrantedObserver($observer); $this->getSecurityContext()->isGranted($permission, $object); $user = $this->getSecurityContext()->getToken()->getUser(); $organization = $this->getSecurityContext()->getToken()->getOrganizationContext(); $accessLevel = $observer->getAccessLevel(); $where = $config->offsetGetByPath('[source][query][where][and]', []); /** todo: refactor this check usages */ if ($accessLevel == AccessLevel::BASIC_LEVEL) { $where = array_merge($where, ['u.id = ' . $user->getId()]); } elseif ($accessLevel == AccessLevel::GLOBAL_LEVEL) { $leftJoins = $config->offsetGetByPath('[source][query][join][inner]', []); $leftJoins[] = ['join' => 'u.organizations', 'alias' => 'org']; $config->offsetSetByPath('[source][query][join][inner]', $leftJoins); $where = array_merge($where, ['org.id in (' . $organization->getId() . ')']); } elseif ($accessLevel !== AccessLevel::SYSTEM_LEVEL) { $resultBuIds = []; if ($accessLevel == AccessLevel::LOCAL_LEVEL) { $resultBuIds = $this->treeProvider->getTree()->getUserBusinessUnitIds($user->getId(), $organization->getId()); } elseif ($accessLevel == AccessLevel::DEEP_LEVEL) { $resultBuIds = $this->treeProvider->getTree()->getUserSubordinateBusinessUnitIds($user->getId(), $organization->getId()); } $leftJoins = $config->offsetGetByPath('[source][query][join][inner]', []); $leftJoins[] = ['join' => 'u.businessUnits', 'alias' => 'bu']; $config->offsetSetByPath('[source][query][join][inner]', $leftJoins); $where = array_merge($where, ['bu.id in (' . implode(', ', $resultBuIds) . ')']); } if (count($where)) { $config->offsetSetByPath('[source][query][where][and]', $where); } }
/** * @param BuildBefore $event */ public function onBuildBefore(BuildBefore $event) { $parameters = $event->getDatagrid()->getParameters(); $permission = $parameters->get('permission'); if ($parameters->get('entity')) { $entityClass = str_replace('_', '\\', $parameters->get('entity')); } else { $entityClass = 'Oro\\Bundle\\UserBundle\\Entity\\User'; } $entityId = $parameters->get('entity_id'); if ($entityId) { $object = $this->em->getRepository($entityClass)->find((int) $entityId); } else { $object = 'entity:' . $entityClass; } $observer = new OneShotIsGrantedObserver(); $this->aclVoter->addOneShotIsGrantedObserver($observer); $this->getSecurityContext()->isGranted($permission, $object); $accessLevel = $observer->getAccessLevel(); $config = $event->getConfig(); $user = $this->getSecurityContext()->getToken()->getUser(); $organization = $this->getSecurityContext()->getToken()->getOrganizationContext(); $this->applyACL($config, $accessLevel, $user, $organization); }
/** * Used only for auditfield-log-grid grid (subscribed in services.yml) * * @param BuildBefore $event */ public function onBuildBefore(BuildBefore $event) { $config = $event->getConfig(); $fieldName = $event->getDatagrid()->getParameters()->get(self::GRID_PARAM_FIELD_NAME, false); $config->offsetSetByPath('[columns][diffs][context][field_name]', $fieldName); }