| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * |
||
| 5 | * @package sitemaker |
||
| 6 | * @copyright (c) 2013 Daniel A. (blitze) |
||
| 7 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 |
||
| 8 | * |
||
| 9 | */ |
||
| 10 | |||
| 11 | namespace blitze\sitemaker\services\blocks\action; |
||
| 12 | |||
| 13 | class save_block extends base_action |
||
| 14 | { |
||
| 15 | /** @var \blitze\sitemaker\model\mapper\blocks */ |
||
| 16 | protected $block_mapper; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * {@inheritdoc} |
||
| 20 | * @throws \blitze\sitemaker\exception\out_of_bounds |
||
| 21 | */ |
||
| 22 | public function execute($style_id) |
||
| 23 | { |
||
| 24 | $block_id = $this->request->variable('id', 0); |
||
| 25 | $update_similar = $this->request->variable('similar', false); |
||
| 26 | |||
| 27 | $this->block_mapper = $this->mapper_factory->create('blocks'); |
||
| 28 | |||
| 29 | /** @var \blitze\sitemaker\model\entity\block $entity */ |
||
| 30 | if (($entity = $this->block_mapper->load(array('bid', '=', $block_id))) === null) |
||
| 31 | { |
||
| 32 | throw new \blitze\sitemaker\exception\out_of_bounds('bid'); |
||
| 33 | } |
||
| 34 | |||
| 35 | $old_hash = $entity->get_hash(); |
||
| 36 | $updated_blocks = array(); |
||
| 37 | |||
| 38 | /** @var \blitze\sitemaker\model\entity\block $entity */ |
||
| 39 | $entity = $this->get_updated_settings($entity); |
||
| 40 | $entity = $this->block_mapper->save($entity); |
||
| 41 | |||
| 42 | $new_hash = $entity->get_hash(); |
||
| 43 | $updated_blocks[$entity->get_bid()] = $this->render_block($entity); |
||
| 44 | |||
| 45 | if ($update_similar && $new_hash !== $old_hash) |
||
| 46 | { |
||
| 47 | $updated_blocks += $this->update_similar($old_hash, $new_hash, $entity->get_settings()); |
||
| 48 | } |
||
| 49 | |||
| 50 | return array( |
||
| 51 | 'list' => $updated_blocks, |
||
| 52 | ); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param \blitze\sitemaker\model\entity\block $entity |
||
| 57 | * @return array |
||
| 58 | */ |
||
| 59 | private function get_updated_settings(\blitze\sitemaker\model\entity\block $entity) |
||
| 60 | { |
||
| 61 | $block_instance = $this->block_factory->get_block($entity->get_name()); |
||
| 62 | $default_config = $block_instance->get_config(array()); |
||
| 63 | |||
| 64 | $cfg_handler = $this->phpbb_container->get('blitze.sitemaker.blocks.cfg_handler'); |
||
| 65 | $submitted_settings = $cfg_handler->get_submitted_settings($default_config); |
||
| 66 | |||
| 67 | $this->set_hidden_fields($submitted_settings, $default_config, $entity->get_settings()); |
||
| 68 | |||
| 69 | return $entity->set_permission($this->get_block_permissions()) |
||
| 70 | ->set_class($this->request->variable('class', '')) |
||
| 71 | ->set_hide_title($this->request->variable('hide_title', 0)) |
||
| 72 | ->set_status($this->request->variable('status', 0)) |
||
| 73 | ->set_type($this->request->variable('type', 0)) |
||
| 74 | ->set_view($this->request->variable('view', '')) |
||
| 75 | ->set_settings($submitted_settings); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Get submitted block permissions |
||
| 80 | * @return [] |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 81 | */ |
||
| 82 | private function get_block_permissions() |
||
| 83 | { |
||
| 84 | $groups = $this->request->variable('perm_groups', array(0)); |
||
| 85 | |||
| 86 | $permission = []; |
||
| 87 | $groups = array_filter($groups); |
||
| 88 | |||
| 89 | if (!empty($groups)) |
||
| 90 | { |
||
| 91 | $permission = array( |
||
| 92 | 'type' => $this->request->variable('perm_type', 0), |
||
| 93 | 'groups' => $groups, |
||
| 94 | ); |
||
| 95 | } |
||
| 96 | |||
| 97 | return $permission; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param array $submitted_settings |
||
| 102 | * @param array $default_settings |
||
| 103 | * @param array $db_settings |
||
| 104 | * @return void |
||
| 105 | */ |
||
| 106 | private function set_hidden_fields(array &$submitted_settings, array $default_settings, array $db_settings) |
||
| 107 | { |
||
| 108 | $not_submitted = array_diff_key($default_settings, $submitted_settings); |
||
| 109 | $hidden_settings = array_intersect_key($db_settings, $not_submitted); |
||
| 110 | |||
| 111 | foreach ($hidden_settings as $field => $value) |
||
| 112 | { |
||
| 113 | if ($default_settings[$field]['type'] === 'hidden') |
||
| 114 | { |
||
| 115 | $submitted_settings[$field] = $value; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param string $old_hash |
||
| 122 | * @param string $new_hash |
||
| 123 | * @param array $settings |
||
| 124 | * @return array |
||
| 125 | */ |
||
| 126 | private function update_similar($old_hash, $new_hash, array $settings) |
||
| 127 | { |
||
| 128 | // find all similar blocks |
||
| 129 | $blocks_collection = $this->block_mapper->find(array('hash', '=', $old_hash)); |
||
| 130 | |||
| 131 | $blocks = array(); |
||
| 132 | foreach ($blocks_collection as $entity) |
||
| 133 | { |
||
| 134 | $entity->set_hash($new_hash); |
||
| 135 | $entity->set_settings($settings); |
||
| 136 | $this->block_mapper->save($entity); |
||
| 137 | |||
| 138 | $blocks[$entity->get_bid()] = $this->render_block($entity); |
||
| 139 | } |
||
| 140 | |||
| 141 | return $blocks; |
||
| 142 | } |
||
| 143 | } |
||
| 144 |