| @@ 28-231 (lines=204) @@ | ||
| 25 | * |
|
| 26 | * @abstract |
|
| 27 | */ |
|
| 28 | abstract class BaseController extends Controller |
|
| 29 | { |
|
| 30 | /** |
|
| 31 | * @return \Symfony\Component\Security\Core\SecurityContextInterface |
|
| 32 | */ |
|
| 33 | /*public function getSecurity() |
|
| 34 | { |
|
| 35 | return $this->container->get('security.context'); |
|
| 36 | }*/ |
|
| 37 | ||
| 38 | /** |
|
| 39 | * @return TwigEngine |
|
| 40 | */ |
|
| 41 | public function getTemplate() |
|
| 42 | { |
|
| 43 | return $this->container->get('templating'); |
|
| 44 | } |
|
| 45 | ||
| 46 | /** |
|
| 47 | * @return NotFoundHttpException |
|
| 48 | */ |
|
| 49 | public function abort() |
|
| 50 | { |
|
| 51 | return new NotFoundHttpException(); |
|
| 52 | } |
|
| 53 | ||
| 54 | /** |
|
| 55 | * Converts string 'Chamilo\CoreBundle\Controller\Admin\QuestionManager' into |
|
| 56 | * 'admin/question_manager' |
|
| 57 | */ |
|
| 58 | public function getTemplatePath() |
|
| 59 | { |
|
| 60 | $parts = $this->classParts; |
|
| 61 | ||
| 62 | $newPath = array(); |
|
| 63 | foreach ($parts as $part) { |
|
| 64 | if (in_array($part, array('chamilo_lms', 'controller')) |
|
| 65 | //strpos($part, '_controller') > 0 |
|
| 66 | ) { |
|
| 67 | continue; |
|
| 68 | } |
|
| 69 | $newPath[] = $part; |
|
| 70 | } |
|
| 71 | ||
| 72 | $template = implode('/', $newPath); |
|
| 73 | return str_replace('_controller', '', $template); |
|
| 74 | } |
|
| 75 | ||
| 76 | /** |
|
| 77 | * Transforms 'QuestionManagerController' to 'question_manager.controller' |
|
| 78 | * @return string |
|
| 79 | */ |
|
| 80 | public function getControllerAlias() |
|
| 81 | { |
|
| 82 | $parts = $this->classParts; |
|
| 83 | $parts = array_reverse($parts); |
|
| 84 | $alias = str_replace('_controller', '.controller', $parts[0]); |
|
| 85 | return $alias; |
|
| 86 | } |
|
| 87 | ||
| 88 | /** |
|
| 89 | * Translator shortcut |
|
| 90 | * @param string $variable |
|
| 91 | * @return string |
|
| 92 | */ |
|
| 93 | public function trans($variable) |
|
| 94 | { |
|
| 95 | return $this->container->get('translator')->trans($variable); |
|
| 96 | } |
|
| 97 | ||
| 98 | /** |
|
| 99 | * Returns the class name label |
|
| 100 | * @example RoleController -> Role |
|
| 101 | * |
|
| 102 | * @return string the class name label |
|
| 103 | */ |
|
| 104 | public function getClassNameLabel() |
|
| 105 | { |
|
| 106 | return $this->classNameLabel; |
|
| 107 | } |
|
| 108 | ||
| 109 | /** |
|
| 110 | * @return MenuFactoryInterface |
|
| 111 | */ |
|
| 112 | public function getMenuFactory() |
|
| 113 | { |
|
| 114 | return $this->container->get('knp_menu.factory'); |
|
| 115 | } |
|
| 116 | ||
| 117 | /** |
|
| 118 | * @param string $action |
|
| 119 | * @return MenuItemInterface |
|
| 120 | */ |
|
| 121 | protected function getBreadcrumbs($action) |
|
| 122 | { |
|
| 123 | $breadcrumbs = $this->buildBreadcrumbs($action); |
|
| 124 | ||
| 125 | return $breadcrumbs; |
|
| 126 | } |
|
| 127 | ||
| 128 | /** Main home URL |
|
| 129 | * @return MenuItemInterface |
|
| 130 | */ |
|
| 131 | protected function getHomeBreadCrumb() |
|
| 132 | { |
|
| 133 | $menu = $this->getMenuFactory()->createItem( |
|
| 134 | 'root', |
|
| 135 | array( |
|
| 136 | 'childrenAttributes' => array( |
|
| 137 | 'class' => 'breadcrumb', |
|
| 138 | 'currentClass' => 'active' |
|
| 139 | ) |
|
| 140 | ) |
|
| 141 | ); |
|
| 142 | ||
| 143 | $menu->addChild( |
|
| 144 | $this->trans('Home'), |
|
| 145 | array('uri' => $this->generateUrl('home')) |
|
| 146 | ); |
|
| 147 | ||
| 148 | return $menu; |
|
| 149 | } |
|
| 150 | ||
| 151 | /** |
|
| 152 | * @param $action |
|
| 153 | * @param MenuItemInterface $menu |
|
| 154 | * @return MenuItemInterface |
|
| 155 | */ |
|
| 156 | public function buildBreadcrumbs($action, MenuItemInterface $menu = null) |
|
| 157 | { |
|
| 158 | if (!$menu) { |
|
| 159 | $menu = $this->getHomeBreadCrumb(); |
|
| 160 | } |
|
| 161 | ||
| 162 | $menu->addChild( |
|
| 163 | $this->trans($this->getClassnameLabel().'List'), |
|
| 164 | array('uri' => $this->generateControllerUrl('listingAction')) |
|
| 165 | ); |
|
| 166 | ||
| 167 | $action = str_replace( |
|
| 168 | array($this->getControllerAlias().':', 'Action'), |
|
| 169 | '', |
|
| 170 | $action |
|
| 171 | ); |
|
| 172 | ||
| 173 | switch ($action) { |
|
| 174 | case 'add': |
|
| 175 | case 'edit': |
|
| 176 | $menu->addChild( |
|
| 177 | $this->trans($this->getClassnameLabel().ucfirst($action)) |
|
| 178 | //array('uri' => $this->generateControllerUrl($action.'Action')) |
|
| 179 | ); |
|
| 180 | break; |
|
| 181 | } |
|
| 182 | ||
| 183 | return $menu; |
|
| 184 | } |
|
| 185 | ||
| 186 | /** |
|
| 187 | * @param array $breadCrumbList |
|
| 188 | * @return string |
|
| 189 | */ |
|
| 190 | protected function parseLegacyBreadCrumb($breadCrumbList = array()) |
|
| 191 | { |
|
| 192 | $menu = $this->getHomeBreadCrumb(); |
|
| 193 | foreach ($breadCrumbList as $item) { |
|
| 194 | $menu->addChild( |
|
| 195 | $this->trans($item['title']), |
|
| 196 | array('uri' => $item['url']) |
|
| 197 | ); |
|
| 198 | } |
|
| 199 | ||
| 200 | $renderer = new ListRenderer(new \Knp\Menu\Matcher\Matcher()); |
|
| 201 | $result = $renderer->render($menu); |
|
| 202 | ||
| 203 | return $result; |
|
| 204 | } |
|
| 205 | ||
| 206 | /** |
|
| 207 | * Renders the current controller template |
|
| 208 | * @param string $name |
|
| 209 | * @param array $elements |
|
| 210 | * @return mixed |
|
| 211 | */ |
|
| 212 | public function renderTemplate($name, $elements = array()) |
|
| 213 | { |
|
| 214 | $name = $this->getTemplatePath().'/'.$name; |
|
| 215 | ||
| 216 | $renderer = new ListRenderer(new \Knp\Menu\Matcher\Matcher()); |
|
| 217 | $action = $this->getRequest()->get('_route'); |
|
| 218 | $result = $renderer->render($this->getBreadcrumbs($action)); |
|
| 219 | $elements['new_breadcrumb'] = $result; |
|
| 220 | ||
| 221 | return $this->getTemplate()->renderTemplate($name, $elements); |
|
| 222 | } |
|
| 223 | ||
| 224 | /** |
|
| 225 | * @return Course |
|
| 226 | */ |
|
| 227 | public function getCourse() |
|
| 228 | { |
|
| 229 | return $this->getRequest()->getSession()->get('course'); |
|
| 230 | } |
|
| 231 | } |
|
| 232 | ||
| @@ 28-231 (lines=204) @@ | ||
| 25 | * |
|
| 26 | * @abstract |
|
| 27 | */ |
|
| 28 | abstract class BaseResourceController extends ResourceController |
|
| 29 | { |
|
| 30 | /** |
|
| 31 | * @return \Symfony\Component\Security\Core\SecurityContextInterface |
|
| 32 | */ |
|
| 33 | /*public function getSecurity() |
|
| 34 | { |
|
| 35 | return $this->container->get('security.context'); |
|
| 36 | }*/ |
|
| 37 | ||
| 38 | /** |
|
| 39 | * @return object |
|
| 40 | */ |
|
| 41 | public function getTemplate() |
|
| 42 | { |
|
| 43 | return $this->container->get('templating'); |
|
| 44 | } |
|
| 45 | ||
| 46 | /** |
|
| 47 | * @return NotFoundHttpException |
|
| 48 | */ |
|
| 49 | public function abort() |
|
| 50 | { |
|
| 51 | return new NotFoundHttpException(); |
|
| 52 | } |
|
| 53 | ||
| 54 | /** |
|
| 55 | * Converts string 'Chamilo\CoreBundle\Controller\Admin\QuestionManager' into |
|
| 56 | * 'admin/question_manager' |
|
| 57 | */ |
|
| 58 | public function getTemplatePath() |
|
| 59 | { |
|
| 60 | $parts = $this->classParts; |
|
| 61 | ||
| 62 | $newPath = array(); |
|
| 63 | foreach ($parts as $part) { |
|
| 64 | if (in_array($part, array('chamilo_lms', 'controller')) |
|
| 65 | //strpos($part, '_controller') > 0 |
|
| 66 | ) { |
|
| 67 | continue; |
|
| 68 | } |
|
| 69 | $newPath[] = $part; |
|
| 70 | } |
|
| 71 | ||
| 72 | $template = implode('/', $newPath); |
|
| 73 | return str_replace('_controller', '', $template); |
|
| 74 | } |
|
| 75 | ||
| 76 | /** |
|
| 77 | * Transforms 'QuestionManagerController' to 'question_manager.controller' |
|
| 78 | * @return string |
|
| 79 | */ |
|
| 80 | public function getControllerAlias() |
|
| 81 | { |
|
| 82 | $parts = $this->classParts; |
|
| 83 | $parts = array_reverse($parts); |
|
| 84 | $alias = str_replace('_controller', '.controller', $parts[0]); |
|
| 85 | return $alias; |
|
| 86 | } |
|
| 87 | ||
| 88 | /** |
|
| 89 | * Translator shortcut |
|
| 90 | * @param string $variable |
|
| 91 | * @return string |
|
| 92 | */ |
|
| 93 | public function trans($variable) |
|
| 94 | { |
|
| 95 | return $this->container->get('translator')->trans($variable); |
|
| 96 | } |
|
| 97 | ||
| 98 | /** |
|
| 99 | * Returns the class name label |
|
| 100 | * @example RoleController -> Role |
|
| 101 | * |
|
| 102 | * @return string the class name label |
|
| 103 | */ |
|
| 104 | public function getClassNameLabel() |
|
| 105 | { |
|
| 106 | return $this->classNameLabel; |
|
| 107 | } |
|
| 108 | ||
| 109 | /** |
|
| 110 | * @return MenuFactoryInterface |
|
| 111 | */ |
|
| 112 | public function getMenuFactory() |
|
| 113 | { |
|
| 114 | return $this->container->get('knp_menu.factory'); |
|
| 115 | } |
|
| 116 | ||
| 117 | /** |
|
| 118 | * @param string $action |
|
| 119 | * @return MenuItemInterface |
|
| 120 | */ |
|
| 121 | protected function getBreadcrumbs($action) |
|
| 122 | { |
|
| 123 | $breadcrumbs = $this->buildBreadcrumbs($action); |
|
| 124 | ||
| 125 | return $breadcrumbs; |
|
| 126 | } |
|
| 127 | ||
| 128 | /** Main home URL |
|
| 129 | * @return MenuItemInterface |
|
| 130 | */ |
|
| 131 | protected function getHomeBreadCrumb() |
|
| 132 | { |
|
| 133 | $menu = $this->getMenuFactory()->createItem( |
|
| 134 | 'root', |
|
| 135 | array( |
|
| 136 | 'childrenAttributes' => array( |
|
| 137 | 'class' => 'breadcrumb', |
|
| 138 | 'currentClass' => 'active' |
|
| 139 | ) |
|
| 140 | ) |
|
| 141 | ); |
|
| 142 | ||
| 143 | $menu->addChild( |
|
| 144 | $this->trans('Home'), |
|
| 145 | array('uri' => $this->generateUrl('home')) |
|
| 146 | ); |
|
| 147 | ||
| 148 | return $menu; |
|
| 149 | } |
|
| 150 | ||
| 151 | /** |
|
| 152 | * @param $action |
|
| 153 | * @param MenuItemInterface $menu |
|
| 154 | * @return MenuItemInterface |
|
| 155 | */ |
|
| 156 | public function buildBreadcrumbs($action, MenuItemInterface $menu = null) |
|
| 157 | { |
|
| 158 | if (!$menu) { |
|
| 159 | $menu = $this->getHomeBreadCrumb(); |
|
| 160 | } |
|
| 161 | ||
| 162 | $menu->addChild( |
|
| 163 | $this->trans($this->getClassnameLabel().'List'), |
|
| 164 | array('uri' => $this->generateControllerUrl('listingAction')) |
|
| 165 | ); |
|
| 166 | ||
| 167 | $action = str_replace( |
|
| 168 | array($this->getControllerAlias().':', 'Action'), |
|
| 169 | '', |
|
| 170 | $action |
|
| 171 | ); |
|
| 172 | ||
| 173 | switch ($action) { |
|
| 174 | case 'add': |
|
| 175 | case 'edit': |
|
| 176 | $menu->addChild( |
|
| 177 | $this->trans($this->getClassnameLabel().ucfirst($action)) |
|
| 178 | //array('uri' => $this->generateControllerUrl($action.'Action')) |
|
| 179 | ); |
|
| 180 | break; |
|
| 181 | } |
|
| 182 | ||
| 183 | return $menu; |
|
| 184 | } |
|
| 185 | ||
| 186 | /** |
|
| 187 | * @param array $breadCrumbList |
|
| 188 | * @return string |
|
| 189 | */ |
|
| 190 | protected function parseLegacyBreadCrumb($breadCrumbList = array()) |
|
| 191 | { |
|
| 192 | $menu = $this->getHomeBreadCrumb(); |
|
| 193 | foreach ($breadCrumbList as $item) { |
|
| 194 | $menu->addChild( |
|
| 195 | $this->trans($item['title']), |
|
| 196 | array('uri' => $item['url']) |
|
| 197 | ); |
|
| 198 | } |
|
| 199 | ||
| 200 | $renderer = new ListRenderer(new \Knp\Menu\Matcher\Matcher()); |
|
| 201 | $result = $renderer->render($menu); |
|
| 202 | ||
| 203 | return $result; |
|
| 204 | } |
|
| 205 | ||
| 206 | /** |
|
| 207 | * Renders the current controller template |
|
| 208 | * @param string $name |
|
| 209 | * @param array $elements |
|
| 210 | * @return mixed |
|
| 211 | */ |
|
| 212 | public function renderTemplate($name, $elements = array()) |
|
| 213 | { |
|
| 214 | $name = $this->getTemplatePath().'/'.$name; |
|
| 215 | ||
| 216 | $renderer = new ListRenderer(new \Knp\Menu\Matcher\Matcher()); |
|
| 217 | $action = $this->getRequest()->get('_route'); |
|
| 218 | $result = $renderer->render($this->getBreadcrumbs($action)); |
|
| 219 | $elements['new_breadcrumb'] = $result; |
|
| 220 | ||
| 221 | return $this->getTemplate()->renderTemplate($name, $elements); |
|
| 222 | } |
|
| 223 | ||
| 224 | /** |
|
| 225 | * @inheritdoc |
|
| 226 | **/ |
|
| 227 | public function isGranted($attributes, $object = null) |
|
| 228 | { |
|
| 229 | return $this->get('security.authorization_checker')->isGranted($attributes, $object); |
|
| 230 | } |
|
| 231 | } |
|
| 232 | ||