getEntityManager()->getEntity('KnowledgeBaseArticle', $id); if (!$entity) { throw new NotFound(); } if (!$this->getAcl()->checkEntity($entity, 'read')) { throw new Forbidden(); } $entity->loadLinkMultipleField('attachments'); $attachmentsIds = $entity->get('attachmentsIds'); foreach ($attachmentsIds as $attachmentId) { $source = $this->getEntityManager()->getEntity('Attachment', $attachmentId); if ($source) { $attachment = $this->getEntityManager()->getEntity('Attachment'); $attachment->set('role', 'Attachment'); $attachment->set('type', $source->get('type')); $attachment->set('size', $source->get('size')); $attachment->set('global', $source->get('global')); $attachment->set('name', $source->get('name')); $attachment->set('sourceId', $source->getSourceId()); if (!empty($parentType) && !empty($parentId)) { $attachment->set('parentType', $parentType); $attachment->set('parentId', $parentId); } if ($this->getFileManager()->isFile('data/upload/' . $source->getSourceId())) { $this->getEntityManager()->saveEntity($attachment); $this->getFileManager()->putContents('data/upload/' . $attachment->id, $contents); $ids[] = $attachment->id; $names->{$attachment->id} = $attachment->get('name'); } } } return array( 'ids' => $ids, 'names' => $names ); } public function moveUp($id, $where = null) { $entity = $this->getEntityManager()->getEntity('KnowledgeBaseArticle', $id); if (!$entity) throw new NotFound(); if (!$this->getAcl()->check($entity, 'edit')) throw new Forbidden(); $currentIndex = $entity->get('order'); if (!is_int($currentIndex)) throw new Error(); if (!$where) { $where = array(); } $selectManager = $this->getSelectManager(); $selectParams = $selectManager->buildSelectParams($where, true, true); $selectParams['whereClause'][] = array( 'order<' => $currentIndex ); $selectManager->applyOrder('order', true, $selectParams); $previousEntity = $this->getRepository()->findOne($selectParams); if (!$previousEntity) return; $entity->set('order', $previousEntity->get('order')); $previousEntity->set('order', $currentIndex); $this->getEntityManager()->saveEntity($entity); $this->getEntityManager()->saveEntity($previousEntity); } public function moveDown($id, $where = null) { $entity = $this->getEntityManager()->getEntity('KnowledgeBaseArticle', $id); if (!$entity) throw new NotFound(); if (!$this->getAcl()->check($entity, 'edit')) throw new Forbidden(); $currentIndex = $entity->get('order'); if (!is_int($currentIndex)) throw new Error(); if (!$where) { $where = array(); } $selectManager = $this->getSelectManager(); $selectParams = $selectManager->buildSelectParams($where, true, true); $selectParams['whereClause'][] = array( 'order>' => $currentIndex ); $selectManager->applyOrder('order', false, $selectParams); $nextEntity = $this->getRepository()->findOne($selectParams); if (!$nextEntity) return; $entity->set('order', $nextEntity->get('order')); $nextEntity->set('order', $currentIndex); $this->getEntityManager()->saveEntity($entity); $this->getEntityManager()->saveEntity($nextEntity); } }