getContainer()->get('entityManager'); } protected function getRecordService($name = null) { if (empty($name)) { $name = $this->name; } if ($this->getServiceFactory()->checkExists($name)) { $service = $this->getServiceFactory()->create($name); } else { $service = $this->getServiceFactory()->create('Record'); $service->setEntityName($name); } return $service; } public function actionRead($params) { $id = $params['id']; $entity = $this->getRecordService()->getEntity($id); if (empty($entity)) { throw new NotFound(); } return $entity->toArray(); } public function actionPatch($params, $data) { return $this->actionUpdate($params, $data); } public function actionCreate($params, $data) { if (!$this->getAcl()->check($this->name, 'edit')) { throw new Forbidden(); } $service = $this->getRecordService(); if ($entity = $service->createEntity($data)) { return $entity->toArray(); } throw new Error(); } public function actionUpdate($params, $data) { if (!$this->getAcl()->check($this->name, 'edit')) { throw new Forbidden(); } $id = $params['id']; if ($entity = $this->getRecordService()->updateEntity($id, $data)) { return $entity->toArray(); } throw new Error(); } public function actionList($params, $data, $request) { if (!$this->getAcl()->check($this->name, 'read')) { throw new Forbidden(); } $where = $request->get('where'); $offset = $request->get('offset'); $maxSize = $request->get('maxSize'); $asc = $request->get('asc') === 'true'; $sortBy = $request->get('sortBy'); $q = $request->get('q'); if (empty($maxSize)) { $maxSize = self::MAX_SIZE_LIMIT; } if (!empty($maxSize) && $maxSize > self::MAX_SIZE_LIMIT) { throw new Forbidden(); } $result = $this->getRecordService()->findEntities(array( 'where' => $where, 'offset' => $offset, 'maxSize' => $maxSize, 'asc' => $asc, 'sortBy' => $sortBy, 'q' => $q, )); return array( 'total' => $result['total'], 'list' => $result['collection']->toArray() ); } public function actionListLinked($params, $data, $request) { $id = $params['id']; $link = $params['link']; $where = $request->get('where'); $offset = $request->get('offset'); $maxSize = $request->get('maxSize'); $asc = $request->get('asc') === 'true'; $sortBy = $request->get('sortBy'); $q = $request->get('q'); if (empty($maxSize)) { $maxSize = self::MAX_SIZE_LIMIT; } if (!empty($maxSize) && $maxSize > self::MAX_SIZE_LIMIT) { throw new Forbidden(); } $result = $this->getRecordService()->findLinkedEntities($id, $link, array( 'where' => $where, 'offset' => $offset, 'maxSize' => $maxSize, 'asc' => $asc, 'sortBy' => $sortBy, 'q' => $q, )); return array( 'total' => $result['total'], 'list' => $result['collection']->toArray() ); } public function actionDelete($params) { $id = $params['id']; if ($this->getRecordService()->deleteEntity($id)) { return true; } throw new Error(); } public function actionExport($params, $data, $request) { if ($this->getConfig()->get('disableExport') && !$this->getUser()->isAdmin()) { throw new Forbidden(); } if (!$this->getAcl()->check($this->name, 'read')) { throw new Forbidden(); } $ids = $request->get('ids'); $where = $request->get('where'); $byWhere = $request->get('byWhere'); $params = array(); if ($byWhere) { $params['where'] = $where; } else { $params['ids'] = $ids; } return array( 'id' => $this->getRecordService()->export($params) ); } public function actionMassUpdate($params, $data, $request) { if (!$this->getAcl()->check($this->name, 'edit')) { throw new Forbidden(); } if (empty($data['attributes'])) { throw new BadRequest(); } $params = array(); if (array_key_exists('where', $data) && !empty($data['byWhere'])) { $params['where'] = json_decode(json_encode($data['where']), true); } else if (array_key_exists('ids', $data)) { $params['ids'] = $data['ids']; } $attributes = $data['attributes']; $idsUpdated = $this->getRecordService()->massUpdate($attributes, $params); return $idsUpdated; } public function actionMassDelete($params, $data, $request) { if (!$this->getAcl()->check($this->name, 'delete')) { throw new Forbidden(); } $params = array(); if (array_key_exists('where', $data) && !empty($data['byWhere'])) { $where = json_decode(json_encode($data['where']), true); $params['where'] = $where; } if (array_key_exists('ids', $data)) { $where = json_decode(json_encode($data['where']), true); $params['ids'] = $data['ids']; } $idsRemoved = $this->getRecordService()->massRemove($params); return $idsRemoved; } public function actionCreateLink($params, $data) { if (empty($params['id']) || empty($params['link'])) { throw BadRequest(); } $id = $params['id']; $link = $params['link']; if (!empty($data['massRelate'])) { if (!is_array($data['where'])) { throw new BadRequest(); } $where = json_decode(json_encode($data['where']), true); return $this->getRecordService()->linkEntityMass($id, $link, $where); } else { $foreignIds = array(); if (isset($data['id'])) { $foreignIds[] = $data['id']; } if (isset($data['ids']) && is_array($data['ids'])) { foreach ($data['ids'] as $foreignId) { $foreignIds[] = $foreignId; } } $result = false; foreach ($foreignIds as $foreignId) { if ($this->getRecordService()->linkEntity($id, $link, $foreignId)) { $result = true; } } if ($result) { return true; } } throw new Error(); } public function actionRemoveLink($params, $data) { $id = $params['id']; $link = $params['link']; if (empty($params['id']) || empty($params['link'])) { throw BadRequest(); } $foreignIds = array(); if (isset($data['id'])) { $foreignIds[] = $data['id']; } if (isset($data['ids']) && is_array($data['ids'])) { foreach ($data['ids'] as $foreignId) { $foreignIds[] = $foreignId; } } $result = false; foreach ($foreignIds as $foreignId) { if ($this->getRecordService()->unlinkEntity($id, $link, $foreignId)) { $result = $result || true; } } if ($result) { return true; } throw new Error(); } public function actionFollow($params) { if (!$this->getAcl()->check($this->name, 'read')) { throw new Forbidden(); } $id = $params['id']; return $this->getRecordService()->follow($id); } public function actionUnfollow($params) { if (!$this->getAcl()->check($this->name, 'read')) { throw new Forbidden(); } $id = $params['id']; return $this->getRecordService()->unfollow($id); } public function actionMerge($params, $data, $request) { if (!$request->isPost()) { throw new BadRequest(); } if (empty($data['targetId']) || empty($data['sourceIds']) || !is_array($data['sourceIds'])) { throw new BadRequest(); } $targetId = $data['targetId']; $sourceIds = $data['sourceIds']; if (!$this->getAcl()->check($this->name, 'edit')) { throw new Forbidden(); } return $this->getRecordService()->merge($targetId, $sourceIds); } }