| Conditions | 21 |
| Paths | 126 |
| Total Lines | 190 |
| Code Lines | 123 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 74 | public function createPage($pageSlug, $redirect, Request $request) |
||
| 75 | { |
||
| 76 | $container = $this->container; |
||
| 77 | //$siteSelector = $container->get('sonata.page.site.selector'); |
||
| 78 | |||
| 79 | $siteManager = $container->get('sonata.page.manager.site'); |
||
| 80 | $host = $request->getHost(); |
||
| 81 | $criteria = [ |
||
| 82 | 'locale' => $request->getLocale(), |
||
| 83 | 'host' => $host, |
||
| 84 | ]; |
||
| 85 | $site = $siteManager->findOneBy($criteria); |
||
| 86 | //$site = $siteSelector->retrieve(); |
||
| 87 | |||
| 88 | if ($request->getLocale() !== $site->getLocale()) { |
||
| 89 | // Check if there's site for this locale |
||
| 90 | $siteManager = $container->get('sonata.page.manager.site'); |
||
| 91 | $host = $request->getHost(); |
||
| 92 | $criteria = [ |
||
| 93 | 'locale' => $request->getLocale(), |
||
| 94 | 'host' => $host, |
||
| 95 | ]; |
||
| 96 | $site = $siteManager->findOneBy($criteria); |
||
| 97 | if (!$site) { |
||
| 98 | // Create new site for this host and language |
||
| 99 | $site = $siteManager->create(); |
||
| 100 | $site->setHost($host); |
||
| 101 | $site->setEnabled(true); |
||
| 102 | $site->setName($host.' in language '.$request->getLocale()); |
||
| 103 | $site->setEnabledFrom(new \DateTime('now')); |
||
| 104 | $site->setEnabledTo(new \DateTime('+20 years')); |
||
| 105 | $site->setRelativePath(''); |
||
| 106 | $site->setIsDefault(false); |
||
| 107 | $site->setLocale($request->getLocale()); |
||
| 108 | $site = $siteManager->save($site); |
||
| 109 | |||
| 110 | // Create first root page |
||
| 111 | |||
| 112 | /** @var PageManager $pageManager */ |
||
| 113 | $pageManager = $container->get('sonata.page.manager.page'); |
||
| 114 | /** @var \Sonata\PageBundle\Model\Page $page */ |
||
| 115 | $page = $pageManager->create(); |
||
| 116 | $page->setSlug('homepage'); |
||
| 117 | $page->setUrl('/'); |
||
| 118 | $page->setName('homepage'); |
||
| 119 | $page->setTitle('home'); |
||
| 120 | $page->setEnabled(true); |
||
| 121 | $page->setDecorate(1); |
||
| 122 | $page->setRequestMethod('GET|POST|HEAD|DELETE|PUT'); |
||
| 123 | $page->setTemplateCode('default'); |
||
| 124 | $page->setRouteName('homepage'); |
||
| 125 | //$page->setParent($this->getReference('page-homepage')); |
||
| 126 | $page->setSite($site); |
||
| 127 | $pageManager->save($page); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | $em = $this->getDoctrine()->getManager(); |
||
| 132 | $page = null; |
||
| 133 | |||
| 134 | $form = $this->createFormBuilder() |
||
| 135 | ->add('content', CKEditorType::class) |
||
| 136 | ->add('save', SubmitType::class, ['label' => 'Update']) |
||
| 137 | ->getForm(); |
||
| 138 | |||
| 139 | $blockToEdit = null; |
||
| 140 | if ($site) { |
||
| 141 | $pageManager = $this->get('sonata.page.manager.page'); |
||
| 142 | |||
| 143 | // Getting parent |
||
| 144 | $criteria = ['site' => $site, 'enabled' => true, 'parent' => null]; |
||
| 145 | /** @var Page $page */ |
||
| 146 | $parent = $pageManager->findOneBy($criteria); |
||
| 147 | |||
| 148 | // Check if a page exists for this site |
||
| 149 | $criteria = ['site' => $site, 'enabled' => true, 'parent' => $parent, 'slug' => $pageSlug]; |
||
| 150 | |||
| 151 | /** @var Page $page */ |
||
| 152 | $page = $pageManager->findOneBy($criteria); |
||
| 153 | if ($page) { |
||
|
|
|||
| 154 | $blocks = $page->getBlocks(); |
||
| 155 | /** @var Block $block */ |
||
| 156 | foreach ($blocks as $block) { |
||
| 157 | if ($block->getName() == 'Main content') { |
||
| 158 | $code = $block->getSetting('code'); |
||
| 159 | if ($code == 'content') { |
||
| 160 | $children = $block->getChildren(); |
||
| 161 | /** @var Block $child */ |
||
| 162 | foreach ($children as $child) { |
||
| 163 | if ($child->getType() == 'sonata.formatter.block.formatter') { |
||
| 164 | $blockToEdit = $child; |
||
| 165 | break 2; |
||
| 166 | } |
||
| 167 | } |
||
| 168 | } |
||
| 169 | } |
||
| 170 | } |
||
| 171 | } else { |
||
| 172 | $pageManager = $this->get('sonata.page.manager.page'); |
||
| 173 | |||
| 174 | $page = $pageManager->create(); |
||
| 175 | $page->setSlug($pageSlug); |
||
| 176 | $page->setUrl('/'.$pageSlug); |
||
| 177 | $page->setName($pageSlug); |
||
| 178 | $page->setTitle($pageSlug); |
||
| 179 | $page->setEnabled(true); |
||
| 180 | $page->setDecorate(1); |
||
| 181 | $page->setRequestMethod('GET'); |
||
| 182 | $page->setTemplateCode('default'); |
||
| 183 | $page->setRouteName($pageSlug); |
||
| 184 | $page->setParent($parent); |
||
| 185 | $page->setSite($site); |
||
| 186 | |||
| 187 | $pageManager->save($page); |
||
| 188 | |||
| 189 | $templateManager = $this->get('sonata.page.template_manager'); |
||
| 190 | $template = $templateManager->get('default'); |
||
| 191 | $templateContainers = $template->getContainers(); |
||
| 192 | |||
| 193 | $containers = []; |
||
| 194 | foreach ($templateContainers as $id => $area) { |
||
| 195 | $containers[$id] = [ |
||
| 196 | 'area' => $area, |
||
| 197 | 'block' => false, |
||
| 198 | ]; |
||
| 199 | } |
||
| 200 | |||
| 201 | // Create blocks for this page |
||
| 202 | $blockInteractor = $this->get('sonata.page.block_interactor'); |
||
| 203 | $parentBlock = null; |
||
| 204 | foreach ($containers as $id => $area) { |
||
| 205 | if (false === $area['block'] && $templateContainers[$id]['shared'] === false) { |
||
| 206 | $block = $blockInteractor->createNewContainer( |
||
| 207 | [ |
||
| 208 | 'page' => $page, |
||
| 209 | 'name' => $templateContainers[$id]['name'], |
||
| 210 | 'code' => $id, |
||
| 211 | ] |
||
| 212 | ); |
||
| 213 | |||
| 214 | if ($id === 'content' && $templateContainers[$id]['name'] == 'Main content') { |
||
| 215 | $parentBlock = $block; |
||
| 216 | } |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 220 | // Create block in main content |
||
| 221 | $block = $this->get('sonata.page.manager.block'); |
||
| 222 | /** @var \Sonata\BlockBundle\Model\Block $myBlock */ |
||
| 223 | $myBlock = $block->create(); |
||
| 224 | $myBlock->setType('sonata.formatter.block.formatter'); |
||
| 225 | $myBlock->setSetting('format', 'richhtml'); |
||
| 226 | $myBlock->setSetting('content', ''); |
||
| 227 | $myBlock->setSetting('rawContent', ''); |
||
| 228 | $myBlock->setSetting('template', '@SonataFormatter/Block/block_formatter.html.twig'); |
||
| 229 | $myBlock->setParent($parentBlock); |
||
| 230 | $page->addBlocks($myBlock); |
||
| 231 | $pageManager->save($page); |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | if ($blockToEdit) { |
||
| 236 | $form->setData(['content' => $blockToEdit->getSetting('content')]); |
||
| 237 | } |
||
| 238 | |||
| 239 | $form->handleRequest($request); |
||
| 240 | |||
| 241 | if ($form->isSubmitted() && $form->isValid() && $blockToEdit) { |
||
| 242 | $data = $form->getData(); |
||
| 243 | $content = $data['content']; |
||
| 244 | /** @var Block $blockToEdit */ |
||
| 245 | $blockToEdit->setSetting('rawContent', $content); |
||
| 246 | $blockToEdit->setSetting('content', $content); |
||
| 247 | $em->merge($blockToEdit); |
||
| 248 | $em->flush(); |
||
| 249 | |||
| 250 | $this->addFlash('success', $this->trans('Updated')); |
||
| 251 | |||
| 252 | if (!empty($redirect)) { |
||
| 253 | return $this->redirect($redirect); |
||
| 254 | } |
||
| 255 | |||
| 256 | return $this->redirectToRoute('home'); |
||
| 257 | } |
||
| 258 | |||
| 259 | return $this->render( |
||
| 260 | '@ChamiloCore/Index/page_edit.html.twig', |
||
| 261 | [ |
||
| 262 | 'page' => $page, |
||
| 263 | 'form' => $form->createView(), |
||
| 264 | ] |
||
| 328 |