/**
  * Iterates over all options and returns specific result
  * 
  * @param int $resultType 1 - values, 2 - definition
  * @return array
  */
 private function walkOptions($resultType)
 {
     $cacheKey = 'options' . $resultType;
     //todo optimize
     $app = App::$instance;
     if (!isset(self::$cache[$cacheKey])) {
         $currentThemeID = $this->getID();
         $result = [];
         $values = $app->bearCMS->data->themes->getOptions($currentThemeID);
         if ($app->bearCMS->currentUser->exists()) {
             $userOptions = $app->bearCMS->data->themes->getTempOptions($currentThemeID, $app->bearCMS->currentUser->getID());
             if (!empty($userOptions)) {
                 $values = array_merge($values, $userOptions);
             }
         }
         // todo optimize
         $themes = \BearCMS\Internal\Data\Themes::getList();
         foreach ($themes as $theme) {
             if ($theme['id'] === $currentThemeID) {
                 if (isset($theme['manifestFilename'])) {
                     $manifestData = \BearCMS\Internal\Data\Themes::getManifestData($theme['manifestFilename'], $theme['dir']);
                     if (isset($manifestData['options'])) {
                         $walkOptions = function ($options) use(&$result, $values, &$walkOptions, $resultType) {
                             foreach ($options as $option) {
                                 if (isset($option['id'])) {
                                     if (isset($values[$option['id']])) {
                                         $result[$option['id']] = $values[$option['id']];
                                     } else {
                                         $result[$option['id']] = isset($option['defaultValue']) ? is_array($option['defaultValue']) ? json_encode($option['defaultValue']) : $option['defaultValue'] : null;
                                     }
                                     if ($resultType === 2) {
                                         $result[$option['id']] = [$result[$option['id']], $option];
                                     }
                                 }
                                 if (isset($option['options'])) {
                                     $walkOptions($option['options']);
                                 }
                             }
                         };
                         $walkOptions($manifestData['options']);
                     }
                 }
                 break;
             }
         }
         self::$cache[$cacheKey] = new \BearCMS\CurrentThemeOptions($result);
     }
     return self::$cache[$cacheKey];
 }
 /**
  * 
  * @param array $data
  * @return array
  * @throws \Exception
  */
 static function theme($data)
 {
     $app = App::$instance;
     if (!isset($data['id'])) {
         throw new \Exception('');
     }
     if ($data['id'] === 'none') {
         return ['id' => 'none'];
     }
     $includeOptions = isset($data['includeOptions']) && !empty($data['includeOptions']);
     $themes = \BearCMS\Internal\Data\Themes::getList();
     foreach ($themes as $theme) {
         if ($theme['id'] === $data['id']) {
             if (isset($theme['manifestFilename'])) {
                 $manifestData = \BearCMS\Internal\Data\Themes::getManifestData($theme['manifestFilename'], $theme['dir']);
                 $manifestData['id'] = $theme['id'];
                 if (isset($manifestData['options'])) {
                     $manifestData['hasOptions'] = !empty($manifestData['options']);
                 } else {
                     $manifestData['hasOptions'] = false;
                 }
                 if ($includeOptions) {
                     $manifestData['options'] = ['definition' => isset($manifestData['options']) ? $manifestData['options'] : []];
                     $result = $app->data->get(['key' => 'bearcms/themes/theme/' . md5($theme['id']) . '.json', 'result' => ['key', 'body']]);
                     if (isset($result['body'])) {
                         $temp = json_decode($result['body'], true);
                         $optionsValues = isset($temp['options']) ? $temp['options'] : [];
                     } else {
                         $optionsValues = [];
                     }
                     $manifestData['options']['activeValues'] = $optionsValues;
                     $result = $app->data->get(['key' => '.temp/bearcms/userthemeoptions/' . md5($app->bearCMS->currentUser->getID()) . '/' . md5($data['id']) . '.json', 'result' => ['key', 'body']]);
                     if (isset($result['body'])) {
                         $temp = json_decode($result['body'], true);
                         $optionsValues = isset($temp['options']) ? $temp['options'] : [];
                     } else {
                         $optionsValues = null;
                     }
                     $manifestData['options']['currentUserValues'] = $optionsValues;
                 } else {
                     if (isset($manifestData['options'])) {
                         unset($manifestData['options']);
                     }
                 }
                 return $manifestData;
             }
         }
     }
     return null;
 }