| Conditions | 22 |
| Paths | 1268 |
| Total Lines | 179 |
| Code Lines | 120 |
| 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 |
||
| 36 | public function index(Application $app, Request $request, $id = 1, $origId = 1) |
||
| 37 | { |
||
| 38 | $DeviceType = $app['eccube.repository.master.device_type'] |
||
| 39 | ->find(\Eccube\Entity\Master\DeviceType::DEVICE_TYPE_PC); |
||
| 40 | |||
| 41 | $PreviewBlockPositions = $app['eccube.repository.block_position'] |
||
| 42 | ->findBy(array( |
||
| 43 | 'page_id' => 0, |
||
| 44 | )); |
||
| 45 | foreach ($PreviewBlockPositions as $BlockPosition) { |
||
| 46 | $app['orm.em']->remove($BlockPosition); |
||
| 47 | } |
||
| 48 | $app['orm.em']->flush(); |
||
| 49 | |||
| 50 | // 編集対象ページ |
||
| 51 | /* @var $TargetPageLayout \Eccube\Entity\PageLayout */ |
||
| 52 | $TargetPageLayout = $app['eccube.repository.page_layout']->get($DeviceType, $id); |
||
| 53 | $OrigTargetPageLayout = $app['eccube.repository.page_layout']->get($DeviceType, $origId); |
||
| 54 | $Blocks = $app['orm.em']->getRepository('Eccube\Entity\Block') |
||
| 55 | ->findBy(array( |
||
| 56 | 'DeviceType' => $DeviceType, |
||
| 57 | )); |
||
| 58 | $BlockPositions = $TargetPageLayout->getBlockPositions(); |
||
| 59 | |||
| 60 | |||
| 61 | $builderLayout = $app['form.factory'] |
||
| 62 | ->createBuilder('admin_page_layout'); |
||
| 63 | |||
| 64 | // 未使用ブロックの取得 |
||
| 65 | $unusedBlocks = $app['eccube.repository.page_layout']->findUnusedBlocks($DeviceType, $id); |
||
| 66 | foreach ($unusedBlocks as $unusedBlock) { |
||
| 67 | $UnusedBlockPosition = new \Eccube\Entity\BlockPosition(); |
||
| 68 | $UnusedBlockPosition |
||
| 69 | ->setPageId($id) |
||
| 70 | ->setTargetId(\Eccube\Entity\PageLayout::TARGET_ID_UNUSED) |
||
| 71 | ->setAnywhere(0) |
||
| 72 | ->setBlockRow(0) |
||
| 73 | ->setBlockId($unusedBlock->getId()) |
||
| 74 | ->setBlock($unusedBlock) |
||
| 75 | ->setPageLayout($TargetPageLayout); |
||
| 76 | $TargetPageLayout->addBlockPosition($UnusedBlockPosition); |
||
| 77 | } |
||
| 78 | |||
| 79 | $builder = $app['form.factory'] |
||
| 80 | ->createBuilder(); |
||
| 81 | |||
| 82 | $event = new EventArgs( |
||
| 83 | array( |
||
| 84 | 'builder' => $builder, |
||
| 85 | 'builderLayout' => $builderLayout, |
||
| 86 | 'DeviceType' => $DeviceType, |
||
| 87 | 'TargetPageLayout' => $TargetPageLayout, |
||
| 88 | 'OrigTargetPageLayout' => $OrigTargetPageLayout, |
||
| 89 | 'Blocks' => $Blocks, |
||
| 90 | 'BlockPositions' => $BlockPositions, |
||
| 91 | ), |
||
| 92 | $request |
||
| 93 | ); |
||
| 94 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_LAYOUT_INDEX_INITIALIZE, $event); |
||
| 95 | |||
| 96 | $listForm = $builderLayout->getForm(); |
||
| 97 | |||
| 98 | $listForm->get('layout')->setData($TargetPageLayout); |
||
| 99 | |||
| 100 | $form = $builder->getForm(); |
||
| 101 | |||
| 102 | if ('POST' === $request->getMethod()) { |
||
| 103 | $form->handleRequest($request); |
||
| 104 | |||
| 105 | if ($form->isValid()) { |
||
| 106 | // 消す |
||
| 107 | foreach ($BlockPositions as $BlockPosition) { |
||
| 108 | if ($BlockPosition->getPageId() == $id || $BlockPosition->getAnywhere() == 0) { |
||
| 109 | $TargetPageLayout->removeBlockPosition($BlockPosition); |
||
| 110 | $app['orm.em']->remove($BlockPosition); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | $app['orm.em']->flush(); |
||
| 114 | |||
| 115 | // TODO: collection を利用 |
||
| 116 | |||
| 117 | $data = $request->request->all(); |
||
| 118 | $max = count($Blocks); |
||
| 119 | for ($i = 0; $i < $max; $i++) { |
||
| 120 | // block_id が取得できない場合は INSERT しない |
||
| 121 | if (!isset($data['id_' . $i])) { |
||
| 122 | continue; |
||
| 123 | } |
||
| 124 | // 未使用は INSERT しない |
||
| 125 | if ($data['target_id_' . $i] == \Eccube\Entity\PageLayout::TARGET_ID_UNUSED) { |
||
| 126 | continue; |
||
| 127 | } |
||
| 128 | // 他のページに anywhere が存在する場合は INSERT しない |
||
| 129 | $anywhere = (isset($data['anywhere_' . $i]) && $data['anywhere_' . $i] == 1) ? 1 : 0; |
||
| 130 | |||
| 131 | if (isset($data['anywhere_' . $i]) && $data['anywhere_' . $i] == 1) { |
||
| 132 | $Other = $app['orm.em']->getRepository('Eccube\Entity\BlockPosition') |
||
| 133 | ->findBy(array( |
||
| 134 | 'anywhere' => 1, |
||
| 135 | 'block_id' => $data['id_' . $i], |
||
| 136 | )); |
||
| 137 | //exist and not preview model |
||
| 138 | if (( count($Other) > 0)&&($id)) { |
||
| 139 | continue; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | $BlockPosition = new \Eccube\Entity\BlockPosition(); |
||
| 144 | $Block = $app['orm.em']->getRepository('Eccube\Entity\Block') |
||
| 145 | ->findOneBy(array( |
||
| 146 | 'id' => $data['id_' . $i], |
||
| 147 | 'DeviceType' => $DeviceType, |
||
| 148 | )); |
||
| 149 | $BlockPosition |
||
| 150 | ->setPageId($id) |
||
| 151 | ->setBlockId($data['id_' . $i]) |
||
| 152 | ->setBlockRow($data['top_' . $i]) |
||
| 153 | ->setTargetId($data['target_id_' . $i]) |
||
| 154 | ->setBlock($Block) |
||
| 155 | ->setPageLayout($TargetPageLayout) |
||
| 156 | ->setAnywhere($anywhere); |
||
| 157 | |||
| 158 | $TargetPageLayout->addBlockPosition($BlockPosition); |
||
| 159 | $app['orm.em']->persist($BlockPosition); |
||
| 160 | } |
||
| 161 | |||
| 162 | $app['orm.em']->persist($TargetPageLayout); |
||
| 163 | $app['orm.em']->flush(); |
||
| 164 | |||
| 165 | $event = new EventArgs( |
||
| 166 | array( |
||
| 167 | 'form' => $form, |
||
| 168 | 'DeviceType' => $DeviceType, |
||
| 169 | 'TargetPageLayout' => $TargetPageLayout, |
||
| 170 | 'OrigTargetPageLayout' => $OrigTargetPageLayout, |
||
| 171 | 'Blocks' => $Blocks, |
||
| 172 | 'BlockPositions' => $BlockPositions, |
||
| 173 | ), |
||
| 174 | $request |
||
| 175 | ); |
||
| 176 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_LAYOUT_INDEX_COMPLETE, $event); |
||
| 177 | |||
| 178 | if ($this->isPreview) { |
||
| 179 | if ($OrigTargetPageLayout->getEditFlg()) { |
||
| 180 | if ($OrigTargetPageLayout->getUrl() === 'product_detail') { |
||
| 181 | $products = $app['eccube.repository.product']->createQueryBUilder('p') |
||
| 182 | ->where('p.Status = 1') |
||
| 183 | ->getQuery() |
||
| 184 | ->getResult(); |
||
| 185 | $product = null; |
||
| 186 | foreach ($products as $p) { |
||
| 187 | $product = $p; |
||
| 188 | break; |
||
| 189 | } |
||
| 190 | if (is_null($product)) { |
||
| 191 | return ''; |
||
| 192 | } |
||
| 193 | return $app->redirect($app->url($OrigTargetPageLayout->getUrl(), array('preview' => 1, 'id' => $product->getId()))); |
||
| 194 | } else { |
||
| 195 | return $app->redirect($app->url($OrigTargetPageLayout->getUrl(), array('preview' => 1))); |
||
| 196 | } |
||
| 197 | } else { |
||
| 198 | return $app->redirect($app->url('homepage').$app['config']['user_data_route']."/".$OrigTargetPageLayout->getUrl().'?preview=1'); |
||
| 199 | } |
||
| 200 | } else { |
||
| 201 | $app->addSuccess('admin.register.complete', 'admin'); |
||
| 202 | return $app->redirect($app->url('admin_content_layout_edit', array('id' => $id))); |
||
| 203 | } |
||
| 204 | |||
| 205 | } |
||
| 206 | |||
| 207 | } |
||
| 208 | |||
| 209 | return $app->render('Content/layout.twig', array( |
||
| 210 | 'form' => $form->createView(), |
||
| 211 | 'list_form' => $listForm->createView(), |
||
| 212 | 'TargetPageLayout' => $TargetPageLayout, |
||
| 213 | )); |
||
| 214 | } |
||
| 215 | |||
| 223 |