Beispiel #1
0
 /**
  * Remove test entities.
  */
 public static function tearDownAfterClass()
 {
     foreach (static::$entityCollection as $setting) {
         Kernel::getService('em')->remove($setting);
     }
     Kernel::getService('em')->flush();
 }
Beispiel #2
0
 public function __construct()
 {
     $themes = Kernel::getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\Theme')->findAll();
     $choices = [];
     $finder = new Finder();
     // Extracting the PHP files from every Theme folder
     $iterator = $finder->followLinks()->files()->name('config.yml')->depth(1)->in(ROADIZ_ROOT . '/themes');
     // And storing it into an array, used in the form
     foreach ($iterator as $file) {
         $data = Yaml::parse($file->getPathname());
         $classname = '\\Themes\\' . $data['themeDir'] . "\\" . $data['themeDir'] . "App";
         /*
          * Parsed file is not or does not contain any PHP Class
          * Bad Theme !
          */
         $choices[$classname] = $data['name'];
     }
     foreach ($themes as $theme) {
         if (array_key_exists($theme->getClassName(), $choices)) {
             unset($choices[$theme->getClassName()]);
         }
         if (array_key_exists(Kernel::INSTALL_CLASSNAME, $choices)) {
             unset($choices[Kernel::INSTALL_CLASSNAME]);
         }
     }
     $this->choices = $choices;
 }
 /**
  * {@inheritdoc}
  */
 public function configureOptions(OptionsResolver $resolver)
 {
     $translations = Kernel::getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\Translation')->findAll();
     $choices = [];
     foreach ($translations as $translation) {
         $choices[$translation->getId()] = $translation->getName();
     }
     $resolver->setDefaults(['choices' => $choices]);
 }
Beispiel #4
0
 /**
  * Refreshes the user for the account interface.
  *
  * It is up to the implementation to decide if the user data should be
  * totally reloaded (e.g. from the database), or if the RZ\Roadiz\Core\Entities\User
  * object can just be merged into some internal array of users / identity
  * map.
  *
  * @param RZ\Roadiz\Core\Entities\User $user
  *
  * @return RZ\Roadiz\Core\Entities\User
  * @throws Symfony\Component\Security\Core\Exception\UnsupportedUserException if the account is not supported
  */
 public function refreshUser(UserInterface $user)
 {
     $refreshUser = Kernel::getService('em')->find('RZ\\Roadiz\\Core\\Entities\\User', (int) $user->getId());
     if ($refreshUser !== null) {
         return $refreshUser;
     } else {
         throw new UnsupportedUserException();
     }
 }
Beispiel #5
0
 /**
  * {@inheritdoc}
  */
 public function configureOptions(OptionsResolver $resolver)
 {
     $nodeTypes = Kernel::getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\NodeType')->findBy(['newsletterType' => false, 'visible' => true]);
     $choices = [];
     foreach ($nodeTypes as $nodeType) {
         $choices[$nodeType->getId()] = $nodeType->getDisplayName();
     }
     $resolver->setDefaults(['choices' => $choices]);
 }
 /**
  * Remove test entities.
  */
 public static function tearDownAfterClass()
 {
     foreach (static::$entityCollection as $setting) {
         Kernel::getService('em')->remove($setting);
     }
     Kernel::getService('em')->flush();
     Kernel::getService('em')->clear();
     // Detaches all objects from Doctrine!
 }
 /**
  * {@inheritdoc}
  */
 public function configureOptions(OptionsResolver $resolver)
 {
     $groups = Kernel::getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\SettingGroup')->findAll();
     $choices = [];
     foreach ($groups as $group) {
         $choices[$group->getId()] = $group->getName();
     }
     $resolver->setDefaults(['choices' => $choices, 'placeholder' => '---------']);
 }
 /**
  * @dataProvider findBySearchQueryAndTranslationProvider
  */
 public function testFindBySearchQueryAndTranslation($query, $expectedClass, Translation $translation)
 {
     $nSources = Kernel::getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\NodesSources')->findBySearchQueryAndTranslation($query, $translation);
     if (null !== $nSources) {
         foreach ($nSources as $key => $source) {
             //echo PHP_EOL.$source->getTitle();
             $this->assertEquals(get_class($source), $expectedClass);
         }
     }
 }
 /**
  * Reset current node-type fields positions.
  *
  * @return int Return the next position after the **last** field
  */
 public function cleanFieldsPositions()
 {
     $fields = $this->customForm->getFields();
     $i = 1;
     foreach ($fields as $field) {
         $field->setPosition($i);
         $i++;
     }
     Kernel::getService('em')->flush();
     return $i;
 }
Beispiel #10
0
 /**
  * Set every tags s default choices values.
  *
  * @param OptionsResolver $resolver
  */
 public function configureOptions(OptionsResolver $resolver)
 {
     $tags = Kernel::getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\Tag')->findAllWithDefaultTranslation();
     $choices = [];
     foreach ($tags as $tag) {
         if (!$this->tags->contains($tag)) {
             $choices[$tag->getId()] = $tag->getTranslatedTags()->first()->getName();
         }
     }
     $resolver->setDefaults(['choices' => $choices]);
 }
Beispiel #11
0
 /**
  * {@inheritdoc}
  */
 public function configureOptions(OptionsResolver $resolver)
 {
     $groups = Kernel::getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\Group')->findAll();
     $choices = [];
     foreach ($groups as $group) {
         if (!$this->groups->contains($group)) {
             $choices[$group->getId()] = $group->getName();
         }
     }
     $resolver->setDefaults(['choices' => $choices]);
 }
 /**
  * Duplicate newsletter
  */
 public function duplicate()
 {
     $newNode = $this->newsletter->getNode()->getHandler()->duplicate();
     Kernel::getService('em')->persist($newNode);
     Kernel::getService('em')->refresh($this->newsletter);
     $newsletter = clone $this->newsletter;
     Kernel::getService('em')->persist($newsletter);
     $newsletter->setNode($newNode);
     Kernel::getService('em')->flush();
     return $newsletter;
 }
Beispiel #13
0
 /**
  * Remove test entities.
  */
 public static function tearDownAfterClass()
 {
     foreach (static::$entityCollection as $node) {
         $node = Kernel::getService("em")->find("RZ\\Roadiz\\Core\\Entities\\Node", $node->getId());
         Kernel::getService('em')->remove($node);
     }
     foreach (static::$runtimeCollection as $node) {
         $node = Kernel::getService("em")->find("RZ\\Roadiz\\Core\\Entities\\Node", $node->getId());
         Kernel::getService('em')->remove($node);
     }
     Kernel::getService('em')->flush();
 }
Beispiel #14
0
 /**
  * Get a document from its setting name.
  *
  * @param string $settingName
  *
  * @return RZ\Roadiz\Core\Entities\Document|null
  */
 public static function getDocument($settingName)
 {
     if (!isset(static::$settings[$settingName]) && Kernel::getService('em') !== null) {
         try {
             $id = Kernel::getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\Setting')->getValue($settingName);
             static::$settings[$settingName] = Kernel::getService('em')->find('RZ\\Roadiz\\Core\\Entities\\Document', (int) $id);
         } catch (\Exception $e) {
             return false;
         }
     }
     return isset(static::$settings[$settingName]) ? static::$settings[$settingName] : false;
 }
Beispiel #15
0
 /**
  * @dataProvider encodeUserProvider
  */
 public function testEncodeUser($userName, $email, $plainPassword)
 {
     $user = new User();
     $user->setUserName($userName);
     $user->setEmail($email);
     $user->setPlainPassword($plainPassword);
     Kernel::getService("em")->persist($user);
     Kernel::getService("em")->flush();
     $this->assertTrue($user->getHandler()->isPasswordValid($plainPassword));
     Kernel::getService("em")->remove($user);
     Kernel::getService("em")->flush();
 }
Beispiel #16
0
 /**
  * Get role by name or create it if non-existant.
  *
  * @param string $roleName
  *
  * @return RZ\Roadiz\core\Entities\Role
  */
 public static function get($roleName)
 {
     if (!isset(static::$roles[$roleName])) {
         static::$roles[$roleName] = Kernel::getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\Role')->findOneBy(['name' => $roleName]);
         if (null === static::$roles[$roleName]) {
             static::$roles[$roleName] = new Role();
             static::$roles[$roleName]->setName($roleName);
             Kernel::getService('em')->persist(static::$roles[$roleName]);
             Kernel::getService('em')->flush();
         }
     }
     return static::$roles[$roleName];
 }
 /**
  * Deserializes a json file into a readable array of datas.
  *
  * @param string $jsonString
  *
  * @return ArrayCollection
  */
 public static function deserialize($jsonString)
 {
     if ($jsonString == "") {
         throw new \Exception('File is empty.');
     }
     $roles = json_decode($jsonString, true);
     $data = new ArrayCollection();
     foreach ($roles as $role) {
         if (!empty($role['name'])) {
             $tmp = Kernel::getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\Role')->findOneByName($role['name']);
             $data[] = $tmp;
         }
     }
     return $data;
 }
 /**
  * Set current translation as default one.
  *
  * @return $this
  */
 public function makeDefault()
 {
     $defaults = Kernel::getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\Translation')->findBy(['defaultTranslation' => true]);
     foreach ($defaults as $default) {
         $default->setDefaultTranslation(false);
     }
     Kernel::getService('em')->flush();
     $this->translation->setDefaultTranslation(true);
     Kernel::getService('em')->flush();
     $cacheDriver = Kernel::getService('em')->getConfiguration()->getResultCacheImpl();
     if ($cacheDriver !== null) {
         $cacheDriver->deleteAll();
     }
     return $this;
 }
Beispiel #19
0
 /**
  * This method does not flush ORM. You'll need to manually call it.
  *
  * @param RZ\Roadiz\Core\Entities\Group $newGroup
  *
  * @throws \RuntimeException If newGroup param is null
  */
 public function diff(Group $newGroup)
 {
     if (null !== $newGroup) {
         if ("" != $newGroup->getName()) {
             $this->group->setName($newGroup->getName());
         }
         $existingRolesNames = $this->group->getRoles();
         foreach ($newGroup->getRolesEntities() as $newRole) {
             if (false === in_array($newRole->getName(), $existingRolesNames)) {
                 $role = Kernel::getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\Role')->findOneByName($newRole->getName());
                 $this->group->addRole($role);
             }
         }
     } else {
         throw new \RuntimeException("New group is null", 1);
     }
 }
Beispiel #20
0
 /**
  * Import a Json file (.rzt) containing setting and setting group.
  *
  * @param string $serializedData
  *
  * @return bool
  */
 public static function importJsonFile($serializedData)
 {
     $return = false;
     $nodeType = NodeTypeJsonSerializer::deserialize($serializedData);
     $existingNodeType = Kernel::getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\NodeType')->findOneByName($nodeType->getName());
     if ($existingNodeType === null) {
         Kernel::getService('em')->persist($nodeType);
         $existingNodeType = $nodeType;
         foreach ($nodeType->getFields() as $field) {
             Kernel::getService('em')->persist($field);
             $field->setNodeType($nodeType);
         }
     } else {
         $existingNodeType->getHandler()->diff($nodeType);
     }
     Kernel::getService('em')->flush();
     $existingNodeType->getHandler()->regenerateEntityClass();
     return $return;
 }
Beispiel #21
0
 /**
  * Remove test entities.
  */
 public static function tearDownAfterClass()
 {
     $solr = Kernel::getService('solr');
     if (null !== $solr) {
         try {
             // get an update query instance
             $update = $solr->createUpdate();
             // add the delete query and a commit command to the update query
             foreach (static::$entityCollection as $document) {
                 $update->addDeleteById($document->id);
             }
             $update->addCommit();
             // this executes the query and returns the result
             $result = $solr->update($update);
         } catch (HttpException $e) {
             echo PHP_EOL . 'No Solr server available.' . PHP_EOL;
             return;
         }
     }
 }
 /**
  * Deserializes a Json into readable datas
  * @param string $string
  *
  * @return ArrayCollection
  */
 public static function deserialize($string)
 {
     if ($string == "") {
         throw new \Exception('File is empty.');
     }
     $collection = new ArrayCollection();
     $array = json_decode($string, true);
     foreach ($array as $groupAssoc) {
         if (!empty($groupAssoc["roles"]) && !empty($groupAssoc["name"])) {
             $group = new Group();
             $group->setName($groupAssoc['name']);
             foreach ($groupAssoc["roles"] as $role) {
                 $role = Kernel::getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\Role')->findOneByName($role['name']);
                 $group->addRole($role);
             }
             $collection[] = $group;
         }
     }
     return $collection;
 }
Beispiel #23
0
 /**
  * {@inheritdoc}
  *
  * @param OptionsResolver $resolver
  */
 public function configureOptions(OptionsResolver $resolver)
 {
     $callback = function ($object, ExecutionContextInterface $context) {
         if (is_array($object)) {
             $documents = Kernel::getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\Document')->findBy(['id' => $object]);
             foreach (array_values($object) as $key => $value) {
                 // Vérifie si le nom est bidon
                 if (isset($documents[$key]) && null !== $value && null === $documents[$key]) {
                     $context->addViolationAt(null, 'Document #' . $value . ' does not exists', [], null);
                 }
             }
         } else {
             $document = Kernel::getService('em')->find('RZ\\Roadiz\\Core\\Entities\\Document', (int) $object);
             // Vérifie si le nom est bidon
             if (null !== $object && null === $document) {
                 $context->addViolationAt(null, 'Document ' . $object . ' does not exists', [], null);
             }
         }
     };
     $resolver->setDefaults(['class' => '\\RZ\\Roadiz\\Core\\Entities\\Document', 'multiple' => true, 'property' => 'id', 'constraints' => [new Callback($callback)]]);
 }
Beispiel #24
0
 /**
  * Import a Json file (.rzt) containing group.
  *
  * @param string $serializedData
  *
  * @return bool
  */
 public static function importJsonFile($serializedData)
 {
     $groups = GroupCollectionJsonSerializer::deserialize($serializedData);
     foreach ($groups as $group) {
         $existingGroup = Kernel::getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\Group')->findOneBy(['name' => $group->getName()]);
         if (null === $existingGroup) {
             foreach ($group->getRolesEntities() as $role) {
                 /*
                  * then persist each role
                  */
                 $role = Kernel::getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\Role')->findOneByName($role->getName());
             }
             Kernel::getService('em')->persist($group);
             // Flush before creating group's roles.
             Kernel::getService('em')->flush();
         } else {
             $existingGroup->getHandler()->diff($group);
         }
         Kernel::getService('em')->flush();
     }
     return true;
 }
Beispiel #25
0
 /**
  * Remove document assets and db row.
  *
  * @return boolean
  */
 public function removeWithAssets()
 {
     Kernel::getService('em')->remove($this->document);
     if (file_exists($this->document->getAbsolutePath())) {
         if (unlink($this->document->getAbsolutePath())) {
             $this->cleanParentDirectory();
             Kernel::getService('em')->flush();
             return true;
         } else {
             throw new \Exception("document.cannot_delete", 1);
         }
     } else {
         /*
          * Only remove from DB
          * and check directory
          */
         $this->cleanParentDirectory();
         Kernel::getService('em')->flush();
         return true;
     }
 }
Beispiel #26
0
 /**
  * @return RZ\Roadiz\Core\Handlers\LoggerHandler
  */
 public function persistAndFlush()
 {
     Kernel::getService('em')->persist($this->log);
     Kernel::getService('em')->flush();
     return $this;
 }
Beispiel #27
0
 /**
  * Generate a resampled document Url.
  *
  * Generated URL will be **absolute** and **static** if
  * a static domain name has been setup.
  *
  * - width
  * - height
  * - crop ({w}x{h}, for example : 100x200)
  * - fit ({w}x{h}, for example : 100x200)
  * - rotate (1-359 degrees, for example : 90)
  * - grayscale / greyscale (boolean)
  * - quality (1-100) - default: 90
  * - blur (1-100)
  * - sharpen (1-100)
  * - contrast (1-100)
  * - background (hexadecimal color without #)
  * - progressive (boolean)
  * - noProcess (boolean) : Disable image resample
  *
  * @param array $args
  *
  * @return string Url
  */
 public function getDocumentUrlByArray($args = null)
 {
     if ($args === null || isset($args['noProcess']) && $args['noProcess'] === true || !$this->document->isImage()) {
         return Kernel::getService('request')->getStaticBaseUrl() . '/files/' . $this->document->getRelativeUrl();
     } else {
         $slirArgs = [];
         if (!empty($args['width'])) {
             $slirArgs['w'] = 'w' . (int) $args['width'];
         }
         if (!empty($args['height'])) {
             $slirArgs['h'] = 'h' . (int) $args['height'];
         }
         if (!empty($args['crop'])) {
             $slirArgs['c'] = 'c' . strip_tags($args['crop']);
         }
         if (!empty($args['blur'])) {
             $slirArgs['l'] = 'l' . strip_tags($args['blur']);
         }
         if (!empty($args['fit'])) {
             $slirArgs['f'] = 'f' . strip_tags($args['fit']);
         }
         if (!empty($args['rotate'])) {
             $slirArgs['r'] = 'r' . strip_tags($args['rotate']);
         }
         if (!empty($args['sharpen'])) {
             $slirArgs['s'] = 's' . strip_tags($args['sharpen']);
         }
         if (!empty($args['contrast'])) {
             $slirArgs['k'] = 'k' . strip_tags($args['contrast']);
         }
         if (!empty($args['grayscale']) && $args['grayscale'] === true || !empty($args['greyscale']) && $args['greyscale'] === true) {
             $slirArgs['g'] = 'g1';
         }
         if (!empty($args['quality'])) {
             $slirArgs['q'] = 'q' . (int) $args['quality'];
         } else {
             $slirArgs['q'] = 'q90';
             // Set default quality to 90%
         }
         if (!empty($args['background'])) {
             $slirArgs['b'] = 'b' . strip_tags($args['background']);
         }
         if (!empty($args['progressive']) && $args['progressive'] === true) {
             $slirArgs['p'] = 'p1';
         }
         $url = Kernel::getService('urlGenerator')->generate('interventionRequestProcess', ['queryString' => implode('-', $slirArgs), 'filename' => $this->document->getRelativeUrl()], UrlGenerator::ABSOLUTE_PATH);
         return Kernel::getService('request')->convertUrlToStaticDomainUrl($url);
     }
 }
 /**
  * Search nodes sources by using Solr search engine
  * and a specific translation.
  *
  * @param string      $query       Solr query string (for example: `text:Lorem Ipsum`)
  * @param Translation $translation Current translation
  *
  * @return ArrayCollection | null
  */
 public function findBySearchQueryAndTranslation($query, Translation $translation)
 {
     // Update Solr Serach engine if setup
     if (true === Kernel::getService('solr.ready')) {
         $service = Kernel::getService('solr');
         $queryObj = $service->createSelect();
         $queryObj->setQuery('collection_txt:' . $query);
         // create a filterquery
         $queryObj->createFilterQuery('translation')->setQuery('locale_s:' . $translation->getLocale());
         $queryObj->addSort('score', $queryObj::SORT_DESC);
         // this executes the query and returns the result
         $resultset = $service->select($queryObj);
         if (0 === $resultset->getNumFound()) {
             return null;
         } else {
             $sources = new ArrayCollection();
             foreach ($resultset as $document) {
                 $sources->add($this->_em->find('RZ\\Roadiz\\Core\\Entities\\NodesSources', $document['node_source_id_i']));
             }
             return $sources;
         }
     }
     return null;
 }
Beispiel #29
0
 /**
  * Generate a font download url.
  *
  * @param string $extension Select a specific font file.
  * @param string $token     Csrf token to protect from requesting font more than once.
  *
  * @return string
  */
 public function getDownloadUrl($extension, $token)
 {
     return Kernel::getService('urlGenerator')->generate('FontFile', ['filename' => $this->font->getHash(), 'variant' => $this->font->getVariant(), 'extension' => $extension, 'token' => $token]);
 }
Beispiel #30
0
 /**
  * Send an email to reset user password.
  *
  * @param  UrlGenerator $urlGenerator
  *
  * @return boolean
  */
 public function sendPasswordResetLink(UrlGenerator $urlGenerator)
 {
     $emailContact = SettingsBag::get('email_sender');
     if (empty($emailContact)) {
         $emailContact = "*****@*****.**";
     }
     $siteName = SettingsBag::get('site_name');
     if (empty($siteName)) {
         $siteName = "Unnamed site";
     }
     $assignation = ['resetLink' => $urlGenerator->generate('loginResetPage', ['token' => $this->user->getConfirmationToken()], true), 'user' => $this->user, 'site' => $siteName, 'mailContact' => $emailContact];
     $emailBody = Kernel::getService('twig.environment')->render('users/reset_password_email.html.twig', $assignation);
     /*
      * inline CSS
      */
     $htmldoc = new InlineStyle($emailBody);
     $htmldoc->applyStylesheet(file_get_contents(ROADIZ_ROOT . "/src/Roadiz/CMS/Resources/css/transactionalStyles.css"));
     // Create the message
     $message = \Swift_Message::newInstance();
     // Give the message a subject
     $message->setSubject(Kernel::getService('translator')->trans('reset.password.request'));
     // Set the From address with an associative array
     $message->setFrom([$emailContact => $siteName]);
     // Set the To addresses with an associative array
     $message->setTo([$this->user->getEmail()]);
     // Give it a body
     $message->setBody($htmldoc->getHTML(), 'text/html');
     // Send the message
     return Kernel::getService('mailer')->send($message);
 }