<?php
namespace Moto\Application\Blocks; use Moto; class Service extends Moto\Service\AbstractStaticService { protected static $_resourceName = 'blocks'; protected static $_resourcePrivilegesMap = array( 'getList' => 'get', ); protected static $_themeBlocksDataFile = '@themeBlocksDataFile'; public static function getList($request = null) { if (null === $request) { $request = static::getRequest()->getParams(); } $result = array(); $file = Moto\System::getAbsolutePath(static::$_themeBlocksDataFile); if (file_exists($file)) { $data = file_get_contents($file); $result = json_decode($data, true); } return $result; } public static function saveToTheme($request = null) { if (null === $request) { $request = static::getRequest()->getParams(); } $isNew = empty($request['id']); if ($isNew) { $filter = new InputFilter\NewBlockTemplate(); } else { $filter = new InputFilter\SaveBlockTemplate(); } $filter->setData($request); if (!$filter->isValid()) { throw new Moto\System\Exception(Moto\System\Exception::ERROR_BAD_REQUEST_MESSAGE, Moto\System\Exception::ERROR_BAD_REQUEST_CODE, $filter->getMessagesKeys()); } $collection = new BlockTemplateCollection(array( 'themeBlocksDataFile' => static::$_themeBlocksDataFile, )); $values = $filter->getValues(true); if ($isNew) { $item = $collection->create($values); $collection->addItem($item); } else { $item = $collection->getById($values['id']); if (!$item) { throw new Moto\System\Exception(Moto\System\Exception::ERROR_NOT_FOUND_MESSAGE, Moto\System\Exception::ERROR_NOT_FOUND_CODE); } $item->fromArray($values); } $collection->save(); return $item->toArray(); } public static function deleteFromTheme($request = null) { if (null === $request) { $request = static::getRequest()->getParams(); } $id = Moto\Util::getFrom($request, 'id'); if (empty($id)) { throw new Moto\System\Exception(Moto\System\Exception::ERROR_BAD_REQUEST_MESSAGE, Moto\System\Exception::ERROR_BAD_REQUEST_CODE, array('id' => array('isEmpty'))); } $collection = new BlockTemplateCollection(array( 'themeBlocksDataFile' => static::$_themeBlocksDataFile, )); $item = $collection->getById($id); if (!$item) { throw new Moto\System\Exception(Moto\System\Exception::ERROR_NOT_FOUND_MESSAGE, Moto\System\Exception::ERROR_NOT_FOUND_CODE); } $item->delete(); $collection->save(); return true; } } 