/**
  * Transforms a string (number) to an object (issue).
  *
  * @param  string $value
  * @return \File|null
  */
 public function reverseTransform($value)
 {
     if (!$value) {
         return null;
     }
     return $this->admin_loader->getAdminByClass($this->entity_class)->getObjectById($value);
 }
 private function getDutyCount(array &$tree, array &$duty_count, \Symforce\AdminBundle\Compiler\Loader\AdminLoader $loader)
 {
     foreach ($tree as $admin_name => $child) {
         $admin = $loader->getAdminByName($admin_name);
         if ($admin->workflow) {
             foreach ($admin->workflow['status'] as $step_name => $step) {
                 if (!$step['duty']) {
                     continue;
                 }
                 if ($step['role'] && !$this->get('security.context')->isGranted($step['role'])) {
                     continue;
                 }
                 // count the status
                 $count = $admin->getRouteWorkflowCount($step_name);
                 if ($count < 1) {
                     continue;
                 }
                 $duty_count[$admin_name][$step_name] = $count;
             }
         }
         if ($child) {
             $this->getDutyCount($child, $duty_count, $loader);
         }
     }
 }
 /**
  * Transforms a string (number) to an object (issue).
  *
  * @param  string $value
  * @return object|null
  */
 public function reverseTransform($value)
 {
     if (empty($value)) {
         return null;
     }
     $this->admin_loader = $this->container->get('sf.admin.loader');
     $admin = $this->admin_loader->getAdminByClass($this->entity_class);
     return $admin->getRepository()->find($value);
 }
 /**
  * Transforms an object (issue) to a string (number).
  *
  * @param  object|null $value
  * @return string
  */
 public function transform($value)
 {
     if (!$value) {
         return 0;
     }
     if (!is_object($value)) {
         return $value;
     }
     $admin = $this->admin_loader->getAdminByClass($this->entity_class);
     return $admin->getId($value);
 }
 public function setReverseData($object, $property)
 {
     if (!$this->admin_loader) {
         $this->admin_loader = $this->container->get('sf.admin.loader');
     }
     $this->plain_property = $property;
     $this->entity_class = get_class($object);
     $admin = $this->admin_loader->getAdminByClass($this->entity_class);
     $this->salt_value = $admin->getReflectionProperty($this->salt_property)->getValue($object);
     $this->password_value = $admin->getReflectionProperty($this->password_property)->getValue($object);
     $this->object_hash = spl_object_hash($object);
 }
示例#6
0
 protected function buildFormReferer(\Symfony\Component\HttpFoundation\Request $request, \Symfony\Component\Form\FormBuilder $builder, $object, $url = null)
 {
     $matcher = $this->admin->getService('router_default')->getMatcher();
     $baseUrl = $request->getBaseUrl();
     $referer = parse_url($request->headers->get('referer'));
     $referer_url = $referer['path'];
     if (strlen($baseUrl) > 1 && substr($referer['path'], 0, strlen($baseUrl)) == $baseUrl) {
         $referer_url = substr($referer['path'], strlen($baseUrl));
     }
     if ($referer_url === $referer['path']) {
         $baseUrl = null;
     }
     $referer_parameters = null;
     try {
         $referer_parameters = $matcher->match($referer_url);
     } catch (\Symfony\Component\Routing\Exception\ResourceNotFoundException $e) {
     }
     $parameters = $matcher->match($request->getPathInfo());
     if (!$referer_parameters || $parameters['_route'] !== $referer_parameters['_route']) {
         $url = $referer['path'] . (isset($referer['query']) ? '?' . $referer['query'] : '');
     }
     $builder->add('sf_admin_form_referer', 'sf_referer', array('referer_url_default' => $url, 'referer_url_route' => $parameters['_route'], 'referer_url_request' => $request, 'referer_url_matcher' => $matcher, 'referer_base_url' => $baseUrl));
     $builder->add('sf_admin_form_dynamic', 'sf_dynamic', array());
     if ($this->admin_loader->getContainer()->getParameter('kernel.debug')) {
         $builder->add('sf_admin_form_debug', 'choice', array('label' => 'Debug', 'mapped' => false, 'expanded' => true, 'data' => 0, 'widget_type' => 'inline', 'choices' => array('1' => 'Yes', '0' => 'No')));
     }
 }
 private function tree(array &$tree, InputInterface $input, OutputInterface $output)
 {
     foreach ($tree as $admin_name => $children) {
         $admin = $this->loader->getAdminByName($admin_name);
         if ($admin->workflow) {
             $this->check($admin, $input, $output);
         }
         if ($children) {
             $this->tree($children, $input, $output);
         }
     }
 }
 private function getActionByObject(&$action, $object)
 {
     $pos = strpos($action, ':');
     if (false === $pos) {
         if (!$object) {
             throw new \Exception(sprintf("can not find web page `%s`", $action));
         }
         if ($object instanceof \Doctrine\ORM\Proxy\Proxy) {
             $class = get_parent_class($object);
         } else {
             $class = get_class($object);
         }
         if (!$this->loader->hasAdminClass($class)) {
             throw new \Exception(sprintf("`%s` is not admin class", $class));
         }
         return $this->loader->getNameByClass($class);
     }
     $admin = substr($action, 0, $pos);
     if (!$this->loader->hasAdminName($admin)) {
         throw new \Exception(sprintf("`%s` is not admin", $admin));
     }
     $action = substr($action, $pos + 1);
     return $admin;
 }
示例#9
0
 public function onUpdate(Controller $controller, Request $request, ActionCache $action, $object, \Symfony\Component\Form\Form $form)
 {
     if ($this->copy_properties) {
         $em = $this->getManager();
         foreach ($this->copy_properties as $property_name => $config) {
             $prop = $this->getReflectionProperty($property_name);
             $value = $prop->getValue($object);
             // \Dev::dump($value);
             $admin = $this->admin_loader->getAdminByClass(get_class($value));
             foreach ($config as $from_property => $to_property) {
                 $from_prod = $this->getReflectionProperty($from_property);
                 $to_prop = $admin->getReflectionProperty($to_property);
                 $to_prop->setValue($value, $from_prod->getValue($object));
             }
             // \Dev::dump($value); exit;
             $em->persist($value);
         }
     }
 }
 public function sf_admin_path($admin, $action, $object = null, $options = array())
 {
     $admin = $this->admin_loader->getAdminByName($admin);
     return $admin->path($action, $object, $options);
 }
 public function setReverseData($data)
 {
     $admin = $this->admin_loader->getAdminByClass($this->admin_class);
     $this->reverse_data = $admin->getObjectOwner($data);
 }
 public function addRoute(\ReflectionMethod $m, \Symforce\AdminBundle\Compiler\Annotation\Route $annot, array $as = null)
 {
     $this->err_msg = sprintf("%s->%s(@%s): ", $m->getDeclaringClass()->getName(), $m->getName(), self::ROUTE_ANNOT_CLASS);
     $admin_name = null;
     if ($annot->admin) {
         if (!preg_match('/^\\w+$/', $annot->admin)) {
             throw new \Exception(sprintf("%s admin `%s` invalid ", $this->err_msg, $annot->admin));
         }
         $admin_name = $annot->admin;
     } else {
         $admin_name = $this->page_loader->getControlerOwnerName($m);
     }
     $action_name = null;
     if ($annot->action) {
         if (!preg_match('/^\\w+$/', $annot->action)) {
             throw new \Exception(sprintf("%s admin `%s` invalid ", $this->err_msg, $annot->action));
         }
         $action_name = $annot->action;
     } else {
         $action_name = $annot->action ?: strtolower(preg_replace('/Action$/', '', $m->getName()));
     }
     if ($admin_name !== $this->admin_name) {
         $this->page_loader->addOtherControlerAction($admin_name, $action_name, $m);
         return;
     }
     $this->page_loader->addPageAction($admin_name, $action_name, $m);
     $controller_name = basename(str_replace('\\', '/', $m->getDeclaringClass()->getName()));
     if (!$annot->name) {
         $annot->name = $admin_name . '.' . $action_name;
     }
     if ($annot->alias) {
         if (!preg_match('/^(.+?)\\s*\\:\\s*(.+)$/', $annot->alias, $ms)) {
             throw new \Exception(sprintf("%s alias `%s` invalid ", $this->err_msg, $annot->alias));
         }
         $admin_alias = $ms[1];
         $action_alias = $ms[2];
         if (!$this->loader->hasAdminName($admin_alias)) {
             throw new \Exception(sprintf("%s admin name `%s` not exists in alias `%s`", $this->err_msg, $admin_alias, $annot->alias));
         }
         $page_generator_alias = $this->page_loader->getPageGeneratorByName($admin_alias);
         $page_generator_alias->addAlias($action_alias, $admin_name, $action_name, $m);
     }
     if (!$annot->template) {
         $annot->template = $this->admin->getBundleName() . ':' . preg_replace('/Controller$/', '', $controller_name) . ':' . $admin_name . '.' . $action_name . '.html.twig';
     }
     $requirement_keys = array();
     if (!$annot->path) {
         if ('index' === $action_name && !$annot->entity) {
             $annot->path = '/' . $this->route_path . '/';
         } else {
             if ('view' === $action_name && null === $annot->entity) {
                 $annot->entity = true;
             }
             if ($annot->entity) {
                 $annot->path = '/' . $this->route_path . '/' . $action_name . '/{' . $this->eneity_id_name . '}';
                 $requirement_keys[$this->eneity_id_name] = true;
             } else {
                 $annot->path = '/' . $this->route_path . '/' . $action_name . '/';
             }
         }
     } else {
         $annot->path = '/' . ltrim($annot->path, '/');
         if (preg_match_all('/\\{(.+?)\\}/', $annot->path, $ms)) {
             foreach ($ms[1] as $requirement_key) {
                 if (isset($requirement_keys[$requirement_key])) {
                     throw new \Exception(sprintf("%s : `%s` duplicate in path `%s`", $this->err_msg, $annot->path, $requirement_key));
                 }
                 $requirement_keys[$requirement_key] = true;
             }
         }
         if (isset($requirement_keys['entity_id'])) {
             $annot->path = str_replace('{entity_id}', '{' . $this->eneity_id_name . '}', $annot->path);
             $annot->entity = true;
             $requirement_keys[$this->eneity_id_name] = true;
             unset($requirement_keys['entity_id']);
         }
         if (isset($requirement_keys[$this->eneity_id_name])) {
             $annot->entity = true;
         }
     }
     $generator = new \Symforce\AdminBundle\Compiler\Generator\PhpWriter();
     $dispatcher = new \Symforce\AdminBundle\Compiler\Generator\PhpWriter();
     $dispatcher_args = array();
     $entity_object_name = '$' . $this->admin_name;
     $generator->write('function(\\Symfony\\Component\\PropertyAccess\\PropertyAccessorInterface $accessor,');
     if ($annot->entity) {
         $generator->write(sprintf('%s %s, ', '\\' . $this->admin_class, $entity_object_name));
     } else {
         if ($this->page_parent_entity) {
             $entity_object_name = '$' . $this->parent_admin->getName();
             $generator->write(sprintf('%s %s, ', '\\' . $this->parent_admin->getClassName(), $entity_object_name));
         }
     }
     $generator->writeln('array $options = array() ){')->indent();
     $dispatcher->writeln('function(\\Symfony\\Component\\PropertyAccess\\PropertyAccessorInterface $accessor, \\' . $m->getDeclaringClass()->getName() . ' $controller, \\Symfony\\Component\\HttpFoundation\\Request $request){')->indent();
     $dispatcher->writeln(sprintf('$controller->setPageAdmin("%s");', $this->admin_name));
     $requirements = array();
     $requirements_entitys = array();
     $route_config = array('_controller' => $m->getDeclaringClass()->getName() . '::dispatchAction', '_sf_web_page' => $admin_name . ':' . $action_name);
     $path = $this->getRoutePath($requirements_entitys, $requirements, $requirement_keys, $generator, $dispatcher, $annot->entity, $annot->path);
     $defaults = array();
     if ($annot->defaults) {
         $defaults = $annot->defaults;
     }
     foreach ($requirements_entitys as $requirement_key) {
         if (isset($requirement_keys[$requirement_key])) {
             unset($requirement_keys[$requirement_key]);
         }
     }
     if ($annot->requirements) {
         foreach ($annot->requirements as $requirement_key => $value) {
             if (isset($requirements[$requirement_key])) {
                 throw new \Exception(sprintf("%s `%s` duplicate requirements:%s ", $this->err_msg, $path, $requirement_key));
             }
             $requirements[$requirement_key] = $value;
         }
     }
     $add_default = function ($i, $name, $value) use(&$dispatcher_args, $generator) {
         $dispatcher_args[$i] = sprintf('$request->get("%s", %s)', $name, var_export($value, 1));
         $generator->writeln(sprintf(' if( !isset($options["%s"]) ) $options["%s"] = %s;', $name, $name, var_export($value, 1)));
     };
     foreach ($m->getParameters() as $i => $p) {
         $requirement_key = $p->getName();
         if (isset($requirement_keys[$requirement_key])) {
             if (isset($defaults[$requirement_key])) {
                 $add_default($i, $requirement_key, $defaults[$requirement_key]);
                 unset($defaults[$requirement_key]);
             } else {
                 if ($p->isDefaultValueAvailable()) {
                     $add_default($i, $requirement_key, $p->getDefaultValue());
                 } else {
                     $dispatcher_args[$i] = sprintf('$request->get("%s")', $p->getName());
                 }
             }
             unset($requirement_keys[$requirement_key]);
         } else {
             if (!$p->isDefaultValueAvailable()) {
                 // check if is request
                 if ($p->getClass()) {
                     $requirement_class = $p->getClass()->name;
                     if ('Symfony\\Component\\HttpFoundation\\Request' === $requirement_class) {
                         $dispatcher_args[$i] = '$request';
                     } else {
                         if (isset($requirements_entitys[$requirement_class])) {
                             $dispatcher_args[$i] = '$' . $this->loader->getNameByClass($requirement_class);
                         } else {
                             if ($this->loader->hasSuperAdminClass($requirement_class)) {
                                 $super_class = $this->loader->getSuperAdminClass($requirement_class);
                                 $dispatcher_args[$i] = '$controller->getAdminByClass(' . var_export($super_class, 1) . ')';
                             } else {
                                 throw new \Exception(sprintf("%s parameter:(%s %s) not defined on path:%s", $this->err_msg, $requirement_class, $requirement_key, $annot->path));
                             }
                         }
                     }
                 } else {
                     if (isset($defaults[$requirement_key])) {
                         $add_default($i, $requirement_key, $defaults[$requirement_key]);
                         unset($defaults[$requirement_key]);
                     } else {
                         throw new \Exception(sprintf("`%s` no value", $requirement_key));
                     }
                 }
             } else {
                 // maybe need add to other place
                 $dispatcher_args[$i] = var_export($p->getDefaultValue(), 1);
             }
         }
     }
     if (!empty($defaults)) {
         throw new \Exception(sprintf("%s defaults:%s not defined on path:%s", $this->err_msg, json_encode($defaults), $annot->path));
     }
     if (!empty($requirement_keys)) {
         throw new \Exception(sprintf("%s path option `{%s}` not defined on path:%s", $this->err_msg, json_encode($requirement_keys), $annot->path));
     }
     $this->action_cache[$action_name] = $annot;
     $route = new \Symfony\Component\Routing\Route($path, $route_config, $requirements);
     $this->page_loader->addPageRoute($admin_name, $action_name, $route, $m, $annot);
     $page_class = $this->page_loader->getCompileClass();
     $writer = $page_class->getLazyWriter();
     $generator->writeln('return $options;');
     $dispatcher->writeln(sprintf('return $controller->%s(%s);', $m->getName(), join(', ', $dispatcher_args)));
     $writer->write(sprintf('$this->cache["%s:%s"]["generator"] = ', $admin_name, $action_name))->indent()->write($generator->getContent())->writeln('}')->outdent()->writeln(";");
     $writer->write(sprintf('$this->cache["%s:%s"]["dispatcher"] = ', $admin_name, $action_name))->indent()->write($dispatcher->getContent())->writeln('}')->outdent()->writeln(";");
     return true;
 }