Пример #1
0
 public function testFindAndCount()
 {
     $customField = 'foo';
     $customVal = 'bar';
     $email1 = '*****@*****.**';
     $email2 = '*****@*****.**';
     $user1 = $this->userManager->createUser($email1, 'password');
     $user1->setCustomField($customField, $customVal);
     $this->userManager->insert($user1);
     $user2 = $this->userManager->createUser($email2, 'password');
     $user2->setCustomField($customField, $customVal);
     $this->userManager->insert($user2);
     $criteria = array('email' => $email1);
     $results = $this->userManager->findBy($criteria);
     $numResults = $this->userManager->findCount($criteria);
     $this->assertCount(1, $results);
     $this->assertEquals(1, $numResults);
     $this->assertEquals($user1, reset($results));
     $criteria = array('customFields' => array($customField => $customVal));
     $results = $this->userManager->findBy($criteria);
     $numResults = $this->userManager->findCount($criteria);
     $this->assertCount(2, $results);
     $this->assertEquals(2, $numResults);
     $this->assertContains($user1, $results);
     $this->assertContains($user2, $results);
 }
Пример #2
0
 public function listAction(Application $app, Request $request)
 {
     $limit = $request->get('limit') ?: 50;
     $offset = $request->get('offset') ?: 0;
     $order_by = $request->get('order_by') ?: 'id';
     $order_dir = $request->get('order_dir') == 'DESC' ? 'DESC' : 'ASC';
     $numResults = $this->userManager->findCount();
     $users = $this->userManager->findBy(array(), array('limit' => array($offset, $limit), 'order_by' => array($order_by, $order_dir)));
     foreach ($users as $user) {
         $user->imageUrl = $this->getGravatarUrl($user->getEmail(), 40);
     }
     $nextUrl = $prevUrl = null;
     if ($numResults > $limit) {
         $nextOffset = $offset + $limit < $numResults ? $offset + $limit : null;
         $prevOffset = $offset > 0 ? $offset - $limit > 0 ? $offset - $limit : 0 : null;
         $baseUrl = $app['url_generator']->generate('user.list') . '?limit=' . $limit . '&order_by=' . $order_by . '&order_dir=' . $order_dir;
         if ($nextOffset !== null) {
             $nextUrl = $baseUrl . '&offset=' . $nextOffset;
         }
         if ($prevOffset !== null) {
             $prevUrl = $baseUrl . '&offset=' . $prevOffset;
         }
     }
     $firstResult = $offset + 1;
     $lastResult = $offset + $limit > $numResults ? $numResults : $offset + $limit;
     return $app['twig']->render($this->listTemplate, array('layout_template' => $this->layoutTemplate, 'users' => $users, 'numResults' => $numResults, 'nextUrl' => $nextUrl, 'prevUrl' => $prevUrl, 'firstResult' => $firstResult, 'lastResult' => $lastResult));
 }
Пример #3
0
 public function listAction(Application $app, Request $request)
 {
     $order_by = $request->get('order_by') ?: 'name';
     $order_dir = $request->get('order_dir') == 'DESC' ? 'DESC' : 'ASC';
     $limit = (int) ($request->get('limit') ?: 50);
     $page = (int) ($request->get('page') ?: 1);
     $offset = ($page - 1) * $limit;
     $criteria = array();
     if (!$app['security']->isGranted('ROLE_ADMIN')) {
         $criteria['isEnabled'] = true;
     }
     $users = $this->userManager->findBy($criteria, array('limit' => array($offset, $limit), 'order_by' => array($order_by, $order_dir)));
     $numResults = $this->userManager->findCount($criteria);
     $paginator = new Paginator($numResults, $limit, $page, $app['url_generator']->generate('user.list') . '?page=(:num)&limit=' . $limit . '&order_by=' . $order_by . '&order_dir=' . $order_dir);
     foreach ($users as $user) {
         $user->imageUrl = $this->getGravatarUrl($user->getEmail(), 40);
     }
     return $app['twig']->render($this->getTemplate('list'), array('layout_template' => $this->getTemplate('layout'), 'users' => $users, 'paginator' => $paginator, 'numResults' => $paginator->getTotalItems(), 'nextUrl' => $paginator->getNextUrl(), 'prevUrl' => $paginator->getPrevUrl(), 'firstResult' => $paginator->getCurrentPageFirstItem(), 'lastResult' => $paginator->getCurrentPageLastItem()));
 }