/**
  * Action to handle the sign in, both the view and the POST request
  *
  * @since 0.0.4 Now sends the 401 header
  * @since 0.0.1
  */
 public function actionSignIn()
 {
     if (!Apollo::getInstance()->getUser()->isGuest()) {
         Apollo::getInstance()->getRequest()->sendToIndex();
     }
     $data = ['error' => null];
     if (isset($_POST['email']) && isset($_POST['password'])) {
         /**
          * @var EntityRepository $user_repository
          */
         $user_repository = DB::getEntityManager()->getRepository('\\Apollo\\Entities\\UserEntity');
         /**
          * @var UserEntity $user
          */
         $user = $user_repository->findOneBy(['email' => strtolower($_POST['email'])]);
         if ($user != null) {
             if (password_verify($_POST['password'], $user->getPassword())) {
                 //TODO: Perhaps make this more secure?
                 Session::set('fingerprint', Session::getFingerprint(md5($user->getPassword())));
                 Session::set('user_id', $user->getId());
                 if (isset($_GET['return'])) {
                     Apollo::getInstance()->getRequest()->sendTo($_GET['return'], false);
                 } else {
                     Apollo::getInstance()->getRequest()->sendToIndex();
                 }
             } else {
                 $data = ['error' => 'Invalid email/password combination.'];
             }
         } else {
             $data = ['error' => 'Invalid email/password combination.'];
         }
     }
     http_response_code(401);
     echo View::getView()->make('user.sign-in', ['data' => $data])->render();
 }
 /**
  * Given an id, this returns the number of activities with ids smaller than that
  * @param $id
  * @return int
  */
 public static function getNumSmallerIds($id)
 {
     $em = DB::getEntityManager();
     $repo = $em->getRepository(Activity::getEntityNamespace());
     $qb = $repo->createQueryBuilder('a');
     $organisation_id = Apollo::getInstance()->getUser()->getOrganisationId();
     $notHidden = $qb->expr()->eq('a' . '.is_hidden', '0');
     $sameOrgId = $qb->expr()->eq('a' . '.organisation', $organisation_id);
     $cond = $qb->expr()->andX($notHidden, $sameOrgId);
     $qb->where($cond);
     $qb->andWhere('a.id < ' . $id);
     $result = $qb->getQuery()->getResult();
     return count($result);
 }
 public static function getMin()
 {
     $em = DB::getEntityManager();
     $repo = $em->getRepository(TargetGroup::getEntityNamespace());
     $qb = $repo->createQueryBuilder('t');
     $organisation_id = Apollo::getInstance()->getUser()->getOrganisationId();
     $notHidden = $qb->expr()->eq('t' . '.is_hidden', '0');
     $sameOrgId = $qb->expr()->eq('t' . '.organisation', $organisation_id);
     $cond = $qb->expr()->andX($notHidden, $sameOrgId);
     $qb->where($cond);
     $query = $qb->getQuery()->setFirstResult(0)->setMaxResults(1);
     $result = $query->getResult();
     $item = $result[0];
     return $item;
 }
 /**
  * Returns the data field or creates it if it does not exist
  *
  * @param int $field_id
  * @return DataEntity
  * @since 0.0.7 Now takes into account the type of the field
  * @since 0.0.6
  */
 public function findOrCreateData($field_id)
 {
     /**
      * @var FieldEntity $field
      */
     $field = Field::getRepository()->find($field_id);
     $data = Data::getRepository()->findOneBy(['record' => $this->getId(), 'field' => $field_id]);
     if ($data == null) {
         $data = new DataEntity();
         $data->setRecord($this);
         $data->setField($field);
         $data->setUpdatedBy(Apollo::getInstance()->getConsole()->getEntity());
         if ($field->hasDefault()) {
             if ($field->isMultiple()) {
                 $data->setLongText(serialize([0]));
             } else {
                 $data->setInt(0);
             }
             $data->setIsDefault(true);
         } else {
             if ($field->isMultiple()) {
                 $value = [''];
                 $data->setLongText(serialize($value));
             }
         }
         DB::getEntityManager()->persist($data);
         DB::getEntityManager()->flush();
     }
     return $data;
 }
<?php

/**
 * Interface for Doctrine command line, accessed using "vendor/bin/doctrine"
 *
 * @author Timur Kuzhagaliyev <*****@*****.**>
 * @copyright 2016
 * @license http://opensource.org/licenses/mit-license.php MIT License
 * @version 0.0.1
 */
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Apollo\Components\DB;
require_once 'vendor/autoload.php';
return ConsoleRunner::createHelperSet(DB::getEntityManager());
 * User: root
 * Date: 02/03/16
 * Time: 10:38
 */
require_once 'vendor/autoload.php';
use Apollo\Apollo;
use Apollo\Components\DB;
use Apollo\Components\User;
use Apollo\Entities\OrganisationEntity;
use Apollo\Entities\PersonEntity;
use Apollo\Entities\RecordEntity;
use Apollo\Entities\UserEntity;
use Faker\Factory;
date_default_timezone_set('Europe/London');
Apollo::prepare();
$entity_manager = DB::getEntityManager();
$organisationRepo = $entity_manager->getRepository('Apollo\\Entities\\OrganisationEntity');
/**
 * @var OrganisationEntity $organisation
 */
$organisation = $organisationRepo->find(1);
$userRepo = User::getRepository();
/**
 * @var UserEntity $user
 */
$user = $userRepo->find(1);
date_default_timezone_set('Europe/London');
$date = new DateTime();
for ($i = 0; $i < 1; $i++) {
    $faker = Factory::create();
    $person = new PersonEntity();
 /**
  * @param $activity
  */
 private function writeActivityToDB($activity)
 {
     $em = DB::getEntityManager();
     $em->persist($activity);
     $em->flush();
 }
 /**
  * @return EntityRepository
  */
 public static function getRepository()
 {
     return DB::getEntityManager()->getRepository(self::getEntityNamespace());
 }
 /**
  * Returns a list of all fields belonging to user's organisation
  *
  * @since 0.1.3
  */
 public function actionFields()
 {
     $em = DB::getEntityManager();
     $fieldRepo = $em->getRepository(Field::getEntityNamespace());
     /** @var FieldEntity[] $fields */
     $fields = $fieldRepo->findBy(['organisation' => Apollo::getInstance()->getUser()->getOrganisationId(), 'is_hidden' => false]);
     $response['error'] = null;
     $data = [];
     for ($i = 0; $i < count($fields); $i++) {
         $field = $fields[$i];
         $fieldData = [];
         $fieldData['id'] = $field->getId();
         $fieldData['essential'] = $field->isEssential();
         $fieldData['name'] = $field->getName();
         $fieldData['type'] = $field->getType();
         $subtype = 0;
         if ($field->getType() == 2) {
             if ($field->hasDefault()) {
                 if ($field->isAllowOther()) {
                     $subtype = 4;
                 } elseif ($field->isMultiple()) {
                     $subtype = 5;
                 } else {
                     $subtype = 3;
                 }
             } else {
                 if ($field->isMultiple()) {
                     $subtype = 2;
                 } else {
                     $subtype = 1;
                 }
             }
         }
         $fieldData['subtype'] = $subtype;
         $defaults = $field->getDefaults();
         $defaultsData = [];
         for ($k = 0; $k < count($defaults); $k++) {
             $defaultsData[] = $defaults[$k]->getValue();
         }
         $fieldData['defaults'] = $defaultsData;
         $data[] = $fieldData;
     }
     $response['data'] = array_reverse($data);
     echo json_encode($response);
 }