/** * @return \Symforce\AdminBundle\Compiler\Generator\PhpClass */ public function compile() { $class = parent::compile(); $twig_writer = $this->_twig->getWriter(); $twig_writer->writeln('{% block action_form_scripts %}'); if ($this->admin_object->_final_template) { $twig_writer->writeln('{% ' . sprintf('import "%s" as admin_%s_macro', $this->admin_object->_final_template, $this->admin_name) . ' %}')->writeln('{% ' . sprintf('if twig_macro_exists(admin_%s_macro, "action_form_scripts") ', $this->admin_name) . ' %}')->indent()->writeln('{{ ' . sprintf('admin_%s_macro.action_form_scripts(admin,action,form)', $this->admin_name) . '}}')->outdent()->writeln('{% endif %}'); } $twig_writer->writeln('{% endblock %}'); return $class; }
/** * @return \Symforce\AdminBundle\Compiler\Generator\PhpClass */ public function compile() { parent::compile(); // $this->children->properties $rc = $this->admin_object->reflection; $admin_class = $this->admin_object->getCompileClass(); $class = $this->getCompileClass(); $twig_writer = $this->_twig->getWriter(); // generate twig template $twig_writer->writeln('{% set _object = object %}')->writeln('{% block content_view %}')->writeln('<div class="form-horizontal">')->indent(); $children = array(); foreach ($this->admin_object->form->children->properties as $property_name => $_property) { if (isset($this->children->properties[$property_name])) { $property = $this->children->properties[$property_name]; $property->_annot = true; } else { $property = new ViewProperty($this->children, $this->admin_object, $property_name); $property->lazyInitialize(); $children[$property_name] = $property; } if (null !== $_property->group && null === $property->group) { $property->set_group($_property->group); } if (null !== $_property->position && null === $property->position) { $property->set_position($_property->position); } } foreach ($rc->getProperties() as $p) { if ($p->isStatic()) { continue; } $property_name = $p->getName(); if (isset($children[$property_name])) { continue; } if (isset($this->children->properties[$property_name])) { $property = $this->children->properties[$property_name]; } else { $property = new ViewProperty($this->children, $this->admin_object, $property_name); $property->lazyInitialize(); $property->_no_form_and_view = true; } $children[$property_name] = $property; } $macro_writer = new \Symforce\AdminBundle\Compiler\Generator\PhpWriter(); foreach ($children as $property_name => $property) { $label = null; if ($property->form_element) { $label = $property->form_element->label; } else { if (isset($this->admin_object->action_collection->children['list']->children->properties[$property_name])) { $label = $this->admin_object->action_collection->children['list']->children->properties[$property_name]->label; } else { $label = $property->label; } } $admin_twig_calss = var_export($this->admin_object->class_name, 1); $admin_twig_code = sprintf('sf_admin_class(%s)', $admin_twig_calss); $admin_class->addLazyArray('properties_label', $property_name, array($label->getPath(), $label->getDomain())); $macro_writer->write('{% ' . sprintf('macro macro_label_%s(admin=false)', $property_name) . ' %}')->write('{% if admin is same as(false) %}{% set admin = ' . $admin_twig_code . ' %}{% endif %}'); if ($this->admin_object->_final_template) { $macro_writer->write('{% ' . sprintf('import "%s" as parent_macro', $this->admin_object->_final_template) . ' %}')->write('{% ' . sprintf('if twig_macro_exists(parent_macro, "macro_label_%s") ', $property_name) . ' %}')->indent()->write('{{ ' . sprintf('parent_macro.macro_label_%s(admin)', $property_name) . '}}')->outdent()->write('{% else %}')->indent()->write('{{' . $property->compileLabel() . ' }}')->outdent()->write('{% endif %}'); } else { $macro_writer->write('{{ ' . $property->compileLabel() . ' }}'); } $macro_writer->writeln('{% endmacro %} '); $macro_writer->writeln('{% ' . sprintf('macro macro_value_%s(_object, _property_value=false, admin=false)', $property_name) . ' %}')->indent()->writeln('{% if admin is same as(false) %}{% set admin = ' . $admin_twig_code . ' %}{% endif %}'); if ($this->admin_object->generator->getParameter('kernel.debug')) { $macro_writer->writeln(sprintf('{{ sf_check_class(_object, %s) }}', $admin_twig_calss)); } $macro_writer->writeln('{% if _property_value is same as(false) %}{% set _property_value = ' . $property->getTwigValue() . ' %}{% endif %}'); if ($this->admin_object->_final_template) { $macro_writer->writeln('{% ' . sprintf('import "%s" as parent_macro', $this->admin_object->_final_template) . ' %}')->writeln('{% ' . sprintf('if twig_macro_exists(parent_macro, "macro_value_%s") ', $property_name) . ' %}')->indent()->writeln('{{ ' . sprintf('parent_macro.macro_value_%s(_object, _property_value, admin)', $property_name) . '}}')->outdent()->writeln('{% else %}')->indent()->writeln($property->compileValue())->outdent()->writeln('{% endif %}'); } else { $macro_writer->writeln($property->compileValue()); } $macro_writer->outdent()->writeln('{% endmacro %} '); $macro_writer->write("\n"); } if ($this->admin_object->workflow) { $macro_writer->writeln('{% macro macro_workflow_report(admin=false) %}')->indent()->writeln('{% if admin is same as(false) %}{% set admin = ' . $admin_twig_code . ' %}{% endif %}'); $this->admin_object->workflow->compileListAction($macro_writer); $macro_writer->outdent()->writeln('{% endmacro %}'); } \Dev::write_file($this->admin_object->_template_path, $macro_writer->getContent()); if ($this->admin_object->form->groups) { foreach ($this->admin_object->form->groups as $_group) { $_annot = array('id' => $_group->id, 'label' => $_group->label); if ($_group->name) { $_annot['name'] = $_group->name; } if (null !== $_group->position) { $_annot['position'] = $_group->position; } $this->_groups[$_group->id] = new ActionPropertyGroup($_annot); } } if (!isset($this->_groups['default'])) { $this->_groups['default'] = new ActionPropertyGroup('default'); } foreach ($children as $property_name => $property) { if ($property->form_element && 'apppassword' === $property->form_element->compile_form_type) { continue; } if ($property->_no_form_and_view) { continue; } if ($property->group) { if (isset($this->_groups[$property->group])) { $group = $this->_groups[$property->group]; } else { $group = new ActionPropertyGroup($property->group); $this->_groups[$property->group] = $group; } } else { $group = $this->_groups['default']; } $group->add($property->class_property, $property->position); } foreach ($this->_groups as $group) { $group->fixLabel($this->admin_object->form->tr_node, $this->admin_object->generator->sf_domain); $group->sort(); } $_anonymous_children = array(); foreach ($this->admin_object->_route_assoc->_anonymous_children as $child_admin_name => $child_properties) { $child_admin = $this->admin_object->generator->getAdminByName($child_admin_name); if (!$child_admin->_final_template) { continue; } $_anonymous_children[$child_admin_name] = $child_properties; } if (count($this->_groups) > 1) { foreach ($this->_groups as $group) { if (!count($group->properties)) { continue; } $twig_writer->writeln('{% set view_property_count = 0 %}')->writeln('<fieldset class="view-group">')->indent(); foreach ($group->properties as $property_name) { $children[$property_name]->compileView($twig_writer); } $twig_writer->write('{% if view_property_count > 0 %}')->write('<legend class="view-group-header">')->write($group->label->getTwigCode())->write('</legend>')->writeln('{% endif %}')->outdent()->writeln('</fieldset>'); } if (!empty($_anonymous_children)) { $twig_writer->writeln('{% set view_property_count = 0 %}')->writeln('<fieldset class="view-group">')->indent(); $this->compileAnonymousChildren($_anonymous_children, $twig_writer); $twig_writer->write('{% if view_property_count > 0 %}')->write('<legend class="view-group-header">Others</legend>')->writeln('{% endif %}')->outdent()->writeln('</fieldset>'); } } else { foreach ($this->_groups as $group) { $twig_writer->writeln('{% set view_property_count = 0 %}'); foreach ($group->properties as $property_name) { $children[$property_name]->compileView($twig_writer); } } $this->compileAnonymousChildren($_anonymous_children, $twig_writer); } $twig_writer->writeln('{% block _admin_view_extra %}')->writeln('{% endblock %}')->outdent()->writeln('</div>')->writeln('{% endblock %}'); return $class; }
/** * @return \Symforce\AdminBundle\Compiler\Generator\PhpClass */ public function compile() { parent::compile(); $_anonymous_children = $this->admin_object->_route_assoc->_anonymous_children; $class = $this->getCompileClass(); $twig_writer = $this->_twig->getWriter(); $twig_column_header = array(); $twig_column_body = array(); $class->addUseStatement('Symfony\\Component\\Form\\FormBuilder'); foreach ($this->children->properties as $field_name => $property) { if ($property instanceof ListProperty) { $twig_column_header[$field_name] = $property->compileTh(); $twig_column_body[$field_name] = $property->compileTd(); } } // generate twig template $twig_writer->writeln('{% block content_list_table %}')->writeln('<table class="table table-bordered">')->indent()->writeln('<tr>')->indent(); foreach ($twig_column_header as $field_name => $header) { $twig_writer->write('{% if admin.isPropertyVisiable("' . $field_name . '", action) %}')->write('{% ' . sprintf('block _admin_%s_label', $field_name) . ' %}')->write($header)->write('{% endblock %}')->writeln('{% endif %}'); } foreach ($_anonymous_children as $child_admin_name => $child_properties) { $child_admin = $this->admin_object->generator->getAdminByName($child_admin_name); if (!$child_admin->_final_template) { continue; } $twig_writer->writeln('{# if ' . sprintf('sf_auth("%s", "list")', $child_admin->name) . ' #}')->indent()->writeln('{% ' . sprintf('import "%s" as child_macro', $child_admin->_final_template) . ' %}'); foreach ($child_properties as $child_property) { $macro_name = 'admin_parent_' . $child_property; $twig_writer->writeln('{% ' . sprintf('if twig_macro_exists(child_macro, "%s") ', $macro_name) . ' %}')->writeln('<th>')->indent()->writeln('{% ' . sprintf('if twig_macro_exists(child_macro, "%s_label") ', $macro_name) . ' %}')->indent()->writeln('{{ ' . sprintf('child_macro.%s_label(admin)', $macro_name) . ' }}')->outdent()->writeln('{% else %}')->indent()->writeln($child_admin->label->getTwigCode())->outdent()->writeln('{% endif %}')->outdent()->writeln('</th>')->writeln('{% endif %}'); } $twig_writer->outdent()->writeln('{# endif #}'); } $twig_writer->writeln('{% block admin_action_th %}<th>{% block admin_action_label %}#{% endblock %}</th>{% endblock %}')->outdent()->writeln('<tr/>')->writeln('{% for _object in pagination %}')->indent()->writeln('<tr>'); foreach ($twig_column_body as $field_name => $body) { $twig_writer->write('{% if admin.isPropertyVisiable("' . $field_name . '", action, _object ) %}')->write('{% set _property_value = ' . $this->children->properties[$field_name]->getTwigValue() . ' %}')->write('{% ' . sprintf(' block _admin_%s_value ', $field_name) . ' %}')->write($body)->write('{% endblock %}')->writeln('{% endif %}'); } foreach ($_anonymous_children as $child_admin_name => $child_properties) { $child_admin = $this->admin_object->generator->getAdminByName($child_admin_name); if (!$child_admin->_final_template) { continue; } $twig_writer->writeln('{#% if ' . sprintf('sf_auth("%s", "list")', $child_admin->name) . ' #}')->indent()->writeln('{% ' . sprintf('import "%s" as child_macro', $child_admin->_final_template) . ' %}'); foreach ($child_properties as $child_property) { $macro_name = 'admin_parent_' . $child_property; $twig_writer->writeln('{% ' . sprintf('if twig_macro_exists(child_macro, "%s") ', $macro_name) . ' %}')->writeln('<td>')->indent()->writeln('{{ ' . sprintf('child_macro.%s(sf_admin_class(%s), admin, _object)', $macro_name, var_export($child_admin->class_name, 1)) . '}}')->outdent()->writeln('</td>')->writeln('{% endif %}'); } $twig_writer->outdent()->writeln('{# endif #}'); } // actions $twig_writer->writeln('{% block admin_action_td %}<td>{% block admin_action_value %}')->indent()->writeln('{% block admin_action_block %}')->indent(); foreach ($this->admin_object->action_collection->children as $action) { if ($action->table) { $twig_writer->write('{% if admin.auth("' . $action->name . '", _object) %}')->write(' <a href="{{ admin.path("' . $action->name . '", _object ) }}">'); if ($action->isWorkflowAuth() && $this->admin_object->workflow) { $twig_writer->write('{{ admin.getWorkflowUpdateLabel(_object, "' . $action->name . '") }}'); } else { $twig_writer->write('{{ admin.action("' . $action->name . '").label }}'); } $twig_writer->write('</a>')->writeln('{% endif %}'); } } $twig_writer->outdent()->writeln('{% endblock admin_action_block %}'); if ($this->admin_object->tree) { $twig_writer->writeln('{% block admin_action_tree %}')->write('{% if admin.auth("list", _object) %}')->write(' <a href="{{ admin.path("list", _object ) }}">{{ "sf.tree.child" | trans({ "%admin%": admin.label }, "' . $this->admin_object->sf_domain . '") }}</a>')->write('{% endif %}')->writeln('{% endblock admin_action_tree %}'); } if ($this->admin_object->workflow) { $twig_writer->writeln('{% block admin_action_workflow %}')->indent(); foreach ($this->admin_object->workflow->children as $node) { if ($node->isInternal()) { continue; } if ($node->target && in_array('removed', $node->target)) { $twig_writer->write('{% if admin.checkWorkflowPermission(_object, "' . $node->name . '") %}')->write(' <a class="api_call" uri="{{ admin.getWorkflowActionPath(_object, "' . $node->name . '") }}">' . $node->action->getTwigCode() . '</a>')->writeln('{% endif %}'); } } $twig_writer->outdent()->writeln('{% endblock admin_action_workflow %}'); } $twig_writer->outdent()->write('{% endblock admin_action_value %}')->write('</td>')->writeln('{% endblock admin_action_td %}'); $twig_writer->outdent()->writeln('<tr/>')->writeln('{% endfor %}')->outdent()->writeln('</table>')->writeln('{% endblock %}'); if ($this->admin_object->workflow) { $twig_writer->writeln('{% block content_list_filter %}')->writeln('{{ admin_macro.macro_workflow_report(admin) }}')->writeln('{% endblock %}'); } return $class; }
/** * @return \Symforce\AdminBundle\Compiler\Generator\PhpClass */ public function compile() { $class = parent::compile(); return $class; }