createQueryBuilder() public method

public createQueryBuilder ( string $alias = NULL, string $indexBy = NULL ) : Kdyby\Doctrine\QueryBuilder
$alias string
$indexBy string The index for the from.
return Kdyby\Doctrine\QueryBuilder
 public function getFreeAddress() : Address
 {
     if ($this->addressRepository->countBy([]) == 0) {
         $this->generateAndPersistNewAddresses(10);
     }
     $this->entityManager->beginTransaction();
     $addressMaxAge = new \DateTime($this->addressLockTime);
     $qb = $this->addressRepository->createQueryBuilder('address');
     $qb->addSelect('count(address) as free')->where('address.lastUsed IS NULL')->orWhere('address.lastUsed < :maxAge')->setParameter('maxAge', $addressMaxAge)->setMaxResults(1);
     /** @var Address $address */
     $result = $qb->getQuery()->getSingleResult();
     $address = $result[0];
     $free = $result['free'];
     $address->useAddress();
     $this->entityManager->flush($address);
     $qb = $this->entityManager->createQueryBuilder();
     $qb->select('COUNT(address) AS total')->from(Address::getClassName(), 'address');
     $total = $qb->getQuery()->getSingleScalarResult();
     $occupied = $total - $free;
     if ($occupied / $total >= $this->occupiedAddressesTreshold) {
         $this->generateAndPersistNewAddresses(round($total * $this->increaseRatio));
     }
     $this->entityManager->commit();
     return $address;
 }
Example #2
0
 /**
  * @param \Venne\Queue\Job $job
  * @param integer $priority
  */
 public function run(Job $job, $priority)
 {
     /** @var Notification $notificationEntity */
     $notificationEntity = $this->notificationRepository->find($job->arguments[0]);
     if ($notificationEntity === null) {
         return;
     }
     $qb = $this->notificationSettingRepository->createQueryBuilder('a')->andWhere('a.type IS NULL OR a.type = :type')->setParameter('type', $notificationEntity->type->id);
     if ($notificationEntity->user) {
         $qb = $qb->andWhere('a.targetUser IS NULL OR a.targetUser = :targetUser')->setParameter('targetUser', $notificationEntity->user->id);
     }
     if ($notificationEntity->target) {
         $qb = $qb->andWhere('a.target IS NULL OR a.target = :target')->setParameter('target', $notificationEntity->target);
     }
     if ($notificationEntity->targetKey) {
         $qb = $qb->andWhere('a.targetKey IS NULL OR a.targetKey = :targetKey')->setParameter('targetKey', $notificationEntity->targetKey);
     }
     $users = array();
     foreach ($qb->getQuery()->getResult() as $setting) {
         $user = $setting->user !== null ? $setting->user : $notificationEntity->user;
         if (isset($users[$user->id]) && !$users[$user->id]['email']) {
             $users[$user->id]['email'] = $setting->email;
         } else {
             $users[$user->id] = array('user' => $user, 'email' => $setting->user !== null ? $setting->email : $notificationEntity->user->getEmail());
         }
     }
     foreach ($users as $user) {
         $notificationUser = new NotificationUser($notificationEntity, $user['user']);
         $this->entityManager->persist($notificationUser);
         $this->entityManager->flush($notificationUser);
         if ($user['email']) {
             $this->jobManager->scheduleJob(new Job(EmailJob::getName(), null, array('user' => $user['user'] instanceof User ? $user['user']->id : $user['user'], 'notification' => $notificationUser->id)), $priority);
         }
     }
 }
Example #3
0
 /**
  * @return \Venne\System\Components\AdminGrid\AdminGrid
  */
 protected function createComponentTable()
 {
     $qb = $this->notificationSettingRepository->createQueryBuilder('a')->andWhere('a.user = :user')->setParameter('user', $this->presenter->user->identity->getId());
     $admin = $this->adminGridFactory->create($this->notificationSettingRepository);
     $table = $admin->getTable();
     $table->setTranslator($this->translator);
     $table->setModel(new Doctrine($qb));
     // columns
     $table->addColumnText('user', 'User')->getCellPrototype()->width = '15%';
     $table->addColumnText('type', 'Type')->setSortable()->setCustomRender(function (NotificationSetting $entity) {
         return $entity->type !== null ? $this->notificationManager->getType($entity->type->type)->getHumanName() : '';
     })->getCellPrototype()->width = '20%';
     $table->addColumnText('action', 'Action')->setCustomRender(function (NotificationSetting $entity) {
         return $entity->type !== null ? $entity->type->action : '';
     })->getCellPrototype()->width = '15%';
     $table->addColumnText('target', 'Target')->getCellPrototype()->width = '20%';
     $table->addColumnText('targetKey', 'Target key')->getCellPrototype()->width = '10%';
     $table->addColumnText('targetUser', 'Target user')->getCellPrototype()->width = '20%';
     // actions
     $event = $table->addActionEvent('edit', 'Edit');
     $event->getElementPrototype()->class[] = 'ajax';
     $form = $admin->addForm('notification', 'Notification setting', function (NotificationSetting $notificationSetting = null) {
         return $this->notificationSettingFormService->getFormFactory($notificationSetting !== null ? $notificationSetting->getId() : null);
     });
     $admin->connectFormWithAction($form, $event);
     // Toolbar
     $toolbar = $admin->getNavbar();
     $section = $toolbar->addSection('new', 'Create', 'file');
     $admin->connectFormWithNavbar($form, $section);
     $deleteEvent = $table->addActionEvent('delete', 'Delete');
     $deleteEvent->getElementPrototype()->class[] = 'ajax';
     $admin->connectActionAsDelete($deleteEvent);
     return $admin;
 }
Example #4
0
 /**
  * @return \Venne\System\Components\AdminGrid\AdminGrid
  */
 public function create()
 {
     $admin = $this->adminGridFactory->create($this->invitationRepository);
     $qb = $this->invitationRepository->createQueryBuilder('a')->andWhere('a.author = :author')->setParameter('author', $this->netteUser->identity->getId());
     $table = $admin->getTable();
     $table->setModel(new Doctrine($qb));
     $table->setTranslator($this->translator);
     $table->addColumnText('email', 'E-mail')->setSortable()->getCellPrototype()->width = '60%';
     $table->getColumn('email')->setFilterText()->setSuggestion();
     $table->addColumnText('type', 'Type')->setCustomRender(function (Invitation $invitationEntity) {
         return $invitationEntity->registration->getName();
     })->getCellPrototype()->width = '40%';
     $form = $admin->addForm('invitation', 'Invitation', function (Invitation $invitation = null) {
         return $this->invitationFormService->getFormFactory($invitation !== null ? $invitation->getId() : null);
     });
     $toolbar = $admin->getNavbar();
     $newSection = $toolbar->addSection('new', 'Create', 'file');
     $editAction = $table->addActionEvent('edit', 'Edit');
     $editAction->getElementPrototype()->class[] = 'ajax';
     $deleteAction = $table->addActionEvent('delete', 'Delete');
     $deleteAction->getElementPrototype()->class[] = 'ajax';
     $admin->connectFormWithNavbar($form, $newSection);
     $admin->connectFormWithAction($form, $editAction);
     $admin->connectActionAsDelete($deleteAction);
     return $admin;
 }
Example #5
0
 /**
  * @param int $id
  * @return Episode|NULL
  */
 public function find($id)
 {
     try {
         return $this->episodesRepository->createQueryBuilder('e')->innerJoin('e.scenarios', 'scenarios')->addSelect('scenarios')->innerJoin('e.season', 'season')->addSelect('season')->leftJoin('scenarios.character', 'character')->addSelect('character')->andWhere('e.id = :id')->setParameter('id', $id)->addOrderBy('scenarios.line')->getQuery()->getSingleResult();
     } catch (NoResultException $e) {
         return NULL;
     }
 }
 /**
  * @param string $email
  * @return Invitation
  * @throws InvitationNotFoundException
  */
 public function getInvitationByEmail($email)
 {
     $qb = $this->invitationsRepository->createQueryBuilder('i')->where('i.email = :email')->setParameter('email', $email);
     try {
         return $qb->getQuery()->getSingleResult();
     } catch (NoResultException $e) {
         $this->onInfo("Email: {$email} NOT found. [getInvitationByEmail]", $e, self::class);
         throw new InvitationNotFoundException();
     }
 }
Example #7
0
 /**
  * Setup permission by role
  *
  * @param \Venne\Security\Authorizator $permission
  * @param string $role
  * @return \Venne\Security\Authorizator
  */
 private function setPermissionsByRole(Authorizator $permission, $role)
 {
     // add role
     if (!$permission->hasRole($role)) {
         $permission->addRole($role);
     }
     // add resources
     $resources = $this->permissionRepository->createQueryBuilder('a')->select('a.resource')->andWhere('a.role = :role')->setParameter('role', $role)->groupBy('a.resource')->getQuery()->getResult();
     foreach ($resources as $resource) {
         if (!$permission->hasResource($resource)) {
             $permission->addResource($resource);
         }
     }
     // set allow/deny
     $roleEntity = $this->roleRepository->findOneByName($role);
     if ($roleEntity) {
         if ($roleEntity->parent) {
             $this->setPermissionsByRole($permission, $roleEntity->parent->name);
         }
         if ($roleEntity && !$permission->hasRole($role)) {
             $permission->addRole($role, $roleEntity->parent ? $roleEntity->parent->name : null);
         }
         foreach ($roleEntity->permissions as $perm) {
             if ($perm->resource === $permission::ALL || $permission->hasResource($perm->resource)) {
                 if ($perm->allow) {
                     $permission->allow($role, $perm->resource, $perm->privilege ? $perm->privilege : null);
                 } else {
                     $permission->deny($role, $perm->resource, $perm->privilege ? $perm->privilege : null);
                 }
             }
         }
     }
     return $permission;
 }
Example #8
0
 /**
  * @return \Grido\Grid
  */
 protected function createComponentTable()
 {
     $grid = $this->gridoFactory->create();
     if ($this->repository) {
         $grid->setModel(new Doctrine($this->repository->createQueryBuilder('a')));
     }
     return $grid;
 }
Example #9
0
 /**
  * @return \Venne\System\Components\AdminGrid\AdminGrid
  */
 protected function createComponentFileTable()
 {
     $admin = $this->createTable();
     $table = $admin->getTable();
     $qb = $this->fileRepository->createQueryBuilder('a')->andWhere('a.invisible = :invisible')->setParameter('invisible', false);
     if ($this->dir === null) {
         $qb->andWhere('a.parent IS NULL');
     } else {
         $qb->andWhere('a.parent = :par')->setParameter('par', $this->dir->getId());
     }
     $table->setModel(new Doctrine($qb));
     $table->setDefaultSort(array('name' => 'ASC'));
     $form = $admin->addForm('file', 'File', function (File $file = null) {
         return $this->fileFormService->getFormFactory($file !== null ? $file->getId() : null, $this->dir !== null ? $this->dir->getId() : null, FileFormService::TYPE_EDIT);
     });
     $form->onSuccess[] = function () {
         if ($this->sideComponent !== null) {
             $this->sideComponent->redrawContent();
         }
     };
     $openAction = $table->addActionEvent('open', 'Open');
     $openAction->onClick[] = function ($id) use($table) {
         $this->onFileOpen($this, $this->fileRepository->find($id));
         $this->redirect('this', array('fileId' => $id));
         $this->redrawControl('content');
     };
     $downloadAction = $table->addActionEvent('download', 'Download');
     $downloadAction->onClick[] = function ($id) use($table) {
         $file = $this->fileRepository->find($id);
         $this->getPresenter()->sendResponse(new FileResponse($file->getFilePath()));
     };
     $editAction = $table->addActionEvent('edit', 'Edit');
     $editAction->getElementPrototype()->class[] = 'ajax';
     $deleteAction = $table->addActionEvent('delete', 'Delete');
     $deleteAction->getElementPrototype()->class[] = 'ajax';
     $deleteAction->onClick[] = function () {
         $this->redrawControl('content');
         if ($this->sideComponent !== null) {
             $this->sideComponent->redrawContent();
         }
     };
     $admin->connectFormWithAction($form, $editAction);
     $admin->connectActionAsDelete($deleteAction);
     return $admin;
 }
Example #10
0
 /**
  * @param string|null $parent
  * @return mixed[]
  */
 public function getFiles($parent = null)
 {
     $parent = $parent ? substr($parent, 2) : null;
     $this->setState((int) $parent, true);
     if ($parent === null) {
         $rootData = array(array('title' => $this->getTranslator()->translate('Root'), 'key' => 'd:', 'folder' => true, 'expanded' => true, 'children' => array()));
         $data =& $rootData[0]['children'];
     }
     $dql = $this->dirRepository->createQueryBuilder('a')->orderBy('a.name', 'ASC');
     if ($parent) {
         $dql = $dql->andWhere('a.parent = ?1')->setParameter(1, $parent);
     } else {
         $dql = $dql->andWhere('a.parent IS NULL');
     }
     $dql = $dql->andWhere('a.invisible = :invisible')->setParameter('invisible', false);
     foreach ($dql->getQuery()->getResult() as $page) {
         $item = array('title' => $page->name, 'key' => 'd:' . $page->id);
         $item['folder'] = true;
         if (count($page->children) > 0 || count($page->files) > 0) {
             $item['lazy'] = true;
         }
         if ($this->getState($page->id)) {
             $item['expanded'] = true;
             $item['children'] = $this->getFiles('d:' . $page->id);
         }
         $data[] = $item;
     }
     $dql = $this->fileRepository->createQueryBuilder('a')->orderBy('a.name', 'ASC');
     if ($parent) {
         $dql = $dql->andWhere('a.parent = ?1')->setParameter(1, $parent);
     } else {
         $dql = $dql->andWhere('a.parent IS NULL');
     }
     $dql = $dql->andWhere('a.invisible = :invisible')->setParameter('invisible', false);
     foreach ($dql->getQuery()->getResult() as $page) {
         $item = array('title' => $page->name, 'key' => 'f:' . $page->id);
         $data[] = $item;
     }
     return $parent !== null ? $data : $rootData;
 }
Example #11
0
 /**
  * @param \Venne\Queue\Worker $worker
  * @return bool
  * @internal
  */
 public function doNextWork(Worker $worker)
 {
     $this->configManager->lock();
     $jobEntity = $this->jobRepository->createQueryBuilder('a')->addOrderBy('a.priority', 'DESC')->addOrderBy('a.date', 'ASC')->andWhere('a.date <= :now')->setParameter('now', new \DateTime())->andWhere('a.state = :state')->setParameter('state', Job::STATE_SCHEDULED)->setMaxResults(1)->getQuery()->getOneOrNullResult();
     $data = $this->configManager->loadConfigFile();
     $data['worker'][$worker->getId()]['lastCheck'] = (new \DateTime())->format('Y-m-d H:i:s');
     $this->configManager->saveConfigFile($data);
     if ($jobEntity) {
         $jobEntity->state = $jobEntity::STATE_IN_PROGRESS;
         $this->entityManager->flush($jobEntity);
         $this->configManager->unlock();
         try {
             $this->doJob($jobEntity, self::PRIORITY_DEFAULT);
         } catch (JobFailedException $e) {
             $failed = true;
             $jobEntity->state = $jobEntity::STATE_FAILED;
             $this->entityManager->flush($jobEntity);
             $worker->log('Error: ' . $e->getPrevious()->getMessage());
         }
         if (!isset($failed)) {
             if ($jobEntity->dateInterval && $jobEntity->round !== 0) {
                 $zero = new \DateTime('00:00');
                 $diff = $zero->diff($jobEntity->dateInterval);
                 $jobEntity->round--;
                 $jobEntity->date = $jobEntity->date->add($diff);
                 $jobEntity->state = $jobEntity::STATE_SCHEDULED;
                 $this->entityManager->persist($jobEntity);
             }
         }
         $this->configManager->lock();
         $data = $this->configManager->loadConfigFile();
         $data['worker'][$worker->getId()]['lastJob'] = (new \DateTime())->format('Y-m-d H:i:s');
         $this->configManager->saveConfigFile($data);
         $this->configManager->unlock();
         return true;
     } else {
         $this->configManager->unlock();
     }
     return false;
 }
Example #12
0
 /**
  * @return \Venne\System\Components\AdminGrid\AdminGrid
  */
 protected function createComponentTable()
 {
     $session = $this->session;
     $admin = $this->adminGridFactory->create($this->loginRepository);
     $table = $admin->getTable();
     if ($this->user->identity instanceof User) {
         $table->setModel(new Doctrine($this->loginRepository->createQueryBuilder('a')->andWhere('a.user = :user')->setParameter('user', $this->user->identity)));
     } else {
         $table->setModel(new Doctrine($this->loginRepository->createQueryBuilder('a')->andWhere('a.user IS NULL')));
     }
     $table->setTranslator($this->translator);
     $table->addColumnDate('current', 'Current')->setCustomRender(function (Login $entity) use($session) {
         $el = Html::el('span');
         $el->class[] = 'glyphicon ' . ($session->id == $entity->getSessionId() ? 'glyphicon-ok' : 'glyphicon-remove');
         return $el;
     })->getCellPrototype()->width = '10%';
     $table->addColumnDate('created', 'Date')->setSortable()->getCellPrototype()->width = '40%';
     $table->addColumnText('sessionId', 'Session ID')->getCellPrototype()->width = '50%';
     $deleteAction = $table->addActionEvent('delete', 'Delete');
     $deleteAction->getElementPrototype()->class[] = 'ajax';
     $admin->connectActionAsDelete($deleteAction);
     return $admin;
 }
Example #13
0
 /**
  * @return QueryBuilder
  */
 public function getQueryBuilder()
 {
     return $this->repository->createQueryBuilder('u');
 }
Example #14
0
 /**
  * @return int
  */
 public function countNotifications()
 {
     return $this->notificationUserRepository->createQueryBuilder('a')->select('COUNT(a.id)')->leftJoin('a.notification', 'l')->andWhere('a.user = :user')->setParameter('user', $this->getUser()->getId())->getQuery()->getSingleScalarResult();
 }