/** * Postprocesses the entity after modification by handling the uploaded * files and setting the flash. * * @param Application $app * the current application * @param AbstractData $crudData * the data instance of the entity * @param Entity $instance * the entity * @param string $entity * the name of the entity * @param string $mode * whether to 'edit' or to 'create' the entity * * @return null|\Symfony\Component\HttpFoundation\RedirectResponse * the HTTP response of this modification */ protected function modifyFilesAndSetFlashBag(Application $app, AbstractData $crudData, Entity $instance, $entity, $mode) { $id = $instance->get('id'); $request = $app['request_stack']->getCurrentRequest(); $result = $mode == 'edit' ? $crudData->updateFiles($request, $instance, $entity) : $crudData->createFiles($request, $instance, $entity); if (!$result) { return null; } $app['session']->getFlashBag()->add('success', $app['translator']->trans('crudlex.' . $mode . '.success', ['%label%' => $crudData->getDefinition()->getLabel(), '%id%' => $id])); return $app->redirect($app['url_generator']->generate('crudShow', ['entity' => $entity, 'id' => $id])); }
/** * Performs the regular unique validation. * * @param $value * the value to validate * @param $data * the data instance to validate with * @param $entity * the entity of the field * @param $field * the field to validate * * @return boolean * true if everything is valid */ protected function isValidUnique($value, AbstractData $data, Entity $entity, $field) { $params = [$field => $value]; $paramsOperators = [$field => '=']; if ($entity->get('id') !== null) { $params['id'] = $entity->get('id'); $paramsOperators['id'] = '!='; } $amount = intval($data->countBy($data->getDefinition()->getTable(), $params, $paramsOperators, true)); return $amount == 0; }
/** * Setups CRUDlex with some events so the passwords get salted and * hashed properly. * * @param AbstractData $data * the AbstractData instance managing the users * * @param string $passwordField * the Entity fieldname of the password hash * * @param string $saltField * the Entity fieldname of the password hash salt */ public function addEvents(AbstractData $data, $passwordField = 'password', $saltField = 'salt') { $that = $this; $saltGenFunction = function (Entity $entity) use($saltField, $that) { $salt = $that->getSalt(40); $entity->set($saltField, $salt); return true; }; $data->pushEvent('before', 'create', $saltGenFunction); $pwHashFunction = $this->getPWHashFunction($data, $passwordField, $saltField); $data->pushEvent('before', 'create', $pwHashFunction); $data->pushEvent('before', 'update', $pwHashFunction); }