Example #1
0
 /**
  * Load data from a file
  *
  * @param  string $filePath
  * @param  array  $returns
  * @return array
  */
 protected function loadData($filePath, $returns = array())
 {
     if (file_exists($filePath)) {
         $content = $this->getFileManager()->getContents($filePath);
         $data = Json::getArrayData($content);
         if (empty($data)) {
             $GLOBALS['log']->warning('FileUnifier::unify() - Empty file or syntax error - [' . $filePath . ']');
             return $returns;
         }
         return $data;
     }
     return $returns;
 }
Example #2
0
 public function process($controllerName, $actionName, $params, $data, $request)
 {
     $customeClassName = '\\Fox\\Custom\\Controllers\\' . Util::normilizeClassName($controllerName);
     if (class_exists($customeClassName)) {
         $controllerClassName = $customeClassName;
     } else {
         $moduleName = $this->metadata->getScopeModuleName($controllerName);
         if ($moduleName) {
             $controllerClassName = '\\Fox\\Modules\\' . $moduleName . '\\Controllers\\' . Util::normilizeClassName($controllerName);
         } else {
             $controllerClassName = '\\Fox\\Controllers\\' . Util::normilizeClassName($controllerName);
         }
     }
     if ($data && stristr($request->getContentType(), 'application/json')) {
         $data = json_decode($data);
     }
     if ($data instanceof \stdClass) {
         $data = get_object_vars($data);
     }
     if (!class_exists($controllerClassName)) {
         throw new NotFound("Controller '{$controllerName}' is not found");
     }
     $controller = new $controllerClassName($this->container, $request->getMethod());
     if ($actionName == 'index') {
         $actionName = $controllerClassName::$defaultAction;
     }
     $actionNameUcfirst = ucfirst($actionName);
     $beforeMethodName = 'before' . $actionNameUcfirst;
     $actionMethodName = 'action' . $actionNameUcfirst;
     $afterMethodName = 'after' . $actionNameUcfirst;
     $fullActionMethodName = strtolower($request->getMethod()) . ucfirst($actionMethodName);
     if (method_exists($controller, $fullActionMethodName)) {
         $primaryActionMethodName = $fullActionMethodName;
     } else {
         $primaryActionMethodName = $actionMethodName;
     }
     if (!method_exists($controller, $primaryActionMethodName)) {
         throw new NotFound("Action '{$actionName}' (" . $request->getMethod() . ") does not exist in controller '{$controllerName}'");
     }
     if (method_exists($controller, $beforeMethodName)) {
         $controller->{$beforeMethodName}($params, $data, $request);
     }
     $result = $controller->{$primaryActionMethodName}($params, $data, $request);
     if (method_exists($controller, $afterMethodName)) {
         $controller->{$afterMethodName}($params, $data, $request);
     }
     if (is_array($result) || is_bool($result)) {
         return \Fox\Core\Utils\Json::encode($result);
     }
     return $result;
 }
Example #3
0
 protected function restoreFiles()
 {
     $packagePath = $this->getPath('packagePath');
     $filesPath = Util::concatPath($packagePath, self::FILES);
     if (!file_exists($filesPath)) {
         $this->unzipArchive($packagePath);
     }
     $res = $this->copy($filesPath, '', true);
     $manifestJson = $this->getFileManager()->getContents(array($packagePath, $this->manifestName));
     $manifest = Json::decode($manifestJson, true);
     if (!empty($manifest['delete'])) {
         $res &= $this->getFileManager()->remove($manifest['delete'], null, true);
     }
     $res &= $this->getFileManager()->removeInDir($packagePath, true);
     return $res;
 }
Example #4
0
 public function notifyAboutNote(array $userIds, \Fox\Entities\Note $note)
 {
     $data = array('noteId' => $note->id);
     $encodedData = Json::encode($data);
     $now = date('Y-m-d H:i:s');
     $pdo = $this->getEntityManager()->getPDO();
     $sql = "INSERT INTO `notification` (`id`, `data`, `type`, `user_id`, `created_at`) VALUES ";
     $arr = [];
     foreach ($userIds as $userId) {
         $id = uniqid();
         $arr[] = "(" . $pdo->quote($id) . ", " . $pdo->quote($encodedData) . ", " . $pdo->quote('Note') . ", " . $pdo->quote($userId) . ", " . $pdo->quote($now) . ")";
     }
     if (empty($arr)) {
         return;
     }
     $sql .= implode(", ", $arr);
     $pdo->query($sql);
 }
Example #5
0
 public function getManifest()
 {
     if (!isset($this->data['manifest'])) {
         $packagePath = $this->getPackagePath();
         $manifestPath = Util::concatPath($packagePath, $this->manifestName);
         if (!file_exists($manifestPath)) {
             $this->throwErrorAndRemovePackage('It\'s not an Installation package.');
         }
         $manifestJson = $this->getFileManager()->getContents($manifestPath);
         $this->data['manifest'] = Json::decode($manifestJson, true);
         if (!$this->data['manifest']) {
             $this->throwErrorAndRemovePackage('Syntax error in manifest.json.');
         }
         if (!$this->checkManifest($this->data['manifest'])) {
             $this->throwErrorAndRemovePackage('Unsupported package.');
         }
     }
     return $this->data['manifest'];
 }
Example #6
0
 public function create($name, $type, $params = array())
 {
     if ($this->getMetadata()->get('scopes.' . $name)) {
         throw new Conflict('Entity [' . $name . '] already exists.');
     }
     if (empty($name) || empty($type)) {
         throw new Error();
     }
     $normalizedName = Util::normilizeClassName($name);
     $contents = "<" . "?" . "php\n\n" . "namespace Fox\\Custom\\Entities;\n\n" . "class {$normalizedName} extends \\Fox\\Core\\Templates\\Entities\\{$type}\n" . "{\n" . "    protected \$entityType = \"{$name}\";\n" . "}\n";
     $filePath = "custom/Fox/Custom/Entities/{$normalizedName}.php";
     $this->getFileManager()->putContents($filePath, $contents);
     $contents = "<" . "?" . "php\n\n" . "namespace Fox\\Custom\\Controllers;\n\n" . "class {$normalizedName} extends \\Fox\\Core\\Templates\\Controllers\\{$type}\n" . "{\n" . "}\n";
     $filePath = "custom/Fox/Custom/Controllers/{$normalizedName}.php";
     $this->getFileManager()->putContents($filePath, $contents);
     $contents = "<" . "?" . "php\n\n" . "namespace Fox\\Custom\\Services;\n\n" . "class {$normalizedName} extends \\Fox\\Core\\Templates\\Services\\{$type}\n" . "{\n" . "}\n";
     $filePath = "custom/Fox/Custom/Services/{$normalizedName}.php";
     $this->getFileManager()->putContents($filePath, $contents);
     $contents = "<" . "?" . "php\n\n" . "namespace Fox\\Custom\\Repositories;\n\n" . "class {$normalizedName} extends \\Fox\\Core\\Templates\\Repositories\\{$type}\n" . "{\n" . "}\n";
     $filePath = "custom/Fox/Custom/Repositories/{$normalizedName}.php";
     $this->getFileManager()->putContents($filePath, $contents);
     $stream = false;
     if (!empty($params['stream'])) {
         $stream = $params['stream'];
     }
     $labelSingular = $name;
     if (!empty($params['labelSingular'])) {
         $labelSingular = $params['labelSingular'];
     }
     $labelPlural = $name;
     if (!empty($params['labelPlural'])) {
         $labelPlural = $params['labelPlural'];
     }
     $labelCreate = $this->getLanguage()->translate('Create') . ' ' . $labelSingular;
     $filePath = "application/Fox/Core/Templates/Metadata/{$type}/scopes.json";
     $scopesDataContents = $this->getFileManager()->getContents($filePath);
     $scopesDataContents = str_replace('{entityType}', $name, $scopesDataContents);
     $scopesData = Json::decode($scopesDataContents, true);
     $scopesData['stream'] = $stream;
     $scopesData['type'] = $type;
     $scopesData['module'] = 'Custom';
     $scopesData['object'] = true;
     $scopesData['isCustom'] = true;
     $this->getMetadata()->set('scopes', $name, $scopesData);
     $filePath = "application/Fox/Core/Templates/Metadata/{$type}/entityDefs.json";
     $entityDefsDataContents = $this->getFileManager()->getContents($filePath);
     $entityDefsDataContents = str_replace('{entityType}', $name, $entityDefsDataContents);
     $entityDefsData = Json::decode($entityDefsDataContents, true);
     $this->getMetadata()->set('entityDefs', $name, $entityDefsData);
     $filePath = "application/Fox/Core/Templates/Metadata/{$type}/clientDefs.json";
     $clientDefsContents = $this->getFileManager()->getContents($filePath);
     $clientDefsContents = str_replace('{entityType}', $name, $clientDefsContents);
     $clientDefsData = Json::decode($clientDefsContents, true);
     $this->getMetadata()->set('clientDefs', $name, $clientDefsData);
     $this->getLanguage()->set('Global', 'scopeNames', $name, $labelSingular);
     $this->getLanguage()->set('Global', 'scopeNamesPlural', $name, $labelPlural);
     $this->getLanguage()->set($name, 'labels', 'Create ' . $name, $labelCreate);
     $this->getMetadata()->save();
     $this->getLanguage()->save();
     $layoutsPath = "application/Fox/Core/Templates/Layouts/{$type}";
     if ($this->getFileManager()->isDir($layoutsPath)) {
         $this->getFileManager()->copy($layoutsPath, 'custom/Fox/Custom/Resources/layouts/' . $name);
     }
     return true;
 }
Example #7
0
 /**
  * Run Service
  *
  * @param  array  $job
  *
  * @return void
  */
 protected function runService(array $job)
 {
     $serviceName = $job['service_name'];
     if (!$this->getServiceFactory()->checkExists($serviceName)) {
         throw new NotFound();
     }
     $service = $this->getServiceFactory()->create($serviceName);
     $serviceMethod = $job['method'];
     if (!method_exists($service, $serviceMethod)) {
         throw new NotFound();
     }
     $data = $job['data'];
     if (Json::isJSON($data)) {
         $data = Json::decode($data, true);
     }
     $service->{$serviceMethod}($data);
 }
Example #8
0
 public function save(Entity $entity)
 {
     if ($entity->id) {
         $this->data[$entity->id] = $entity->toArray();
         $fields = $fields = $this->getMetadata()->get('entityDefs.Preferences.fields');
         $data = array();
         foreach ($this->data[$entity->id] as $field => $value) {
             if (empty($fields[$field]['notStorable'])) {
                 $data[$field] = $value;
             }
         }
         $fileName = $this->getFilePath($entity->id);
         $this->getFileManager()->putContents($fileName, Json::encode($data, \JSON_PRETTY_PRINT));
         $this->storeAutoFollowEntityTypeList($entity);
         return $entity;
     }
 }
Example #9
0
 /**
  * Get All Metadata context
  *
  * @param $isJSON
  * @param bool $reload
  *
  * @return json | array
  */
 public function getAll($isJSON = false, $reload = false)
 {
     if ($reload) {
         $this->init(true);
     }
     if ($isJSON) {
         return Json::encode($this->meta);
     }
     return $this->meta;
 }
Example #10
0
 /**
  * Unset some element of content data
  *
  * @param  string | array $path
  * @param  array | string $unsets
  * @return bool
  */
 public function unsetContents($path, $unsets, $isJSON = true)
 {
     $currentData = $this->getContents($path);
     if ($currentData == false) {
         $GLOBALS['log']->notice('FileManager::unsetContents: File [' . $this->concatPaths($path) . '] does not exist.');
         return false;
     }
     $currentDataArray = Utils\Json::getArrayData($currentData);
     $unsettedData = Utils\Util::unsetInArray($currentDataArray, $unsets, true);
     if (is_null($unsettedData) || is_array($unsettedData) && empty($unsettedData)) {
         $fullPath = $this->concatPaths($path);
         return $this->unlink($fullPath);
     }
     if ($isJSON) {
         return $this->putContentsJson($path, $unsettedData);
     }
     return $this->putContents($path, $unsettedData);
 }
Example #11
0
 /**
  * Load default values for selected type [metadata, layouts]
  *
  * @param string $name
  * @param string $type - [metadata, layouts]
  *
  * @return array
  */
 protected function loadDefaultValues($name, $type = 'metadata')
 {
     $defaultPath = $this->params['defaultsPath'];
     $defaultValue = $this->getFileManager()->getContents(array($defaultPath, $type, $name . '.json'));
     if ($defaultValue !== false) {
         //return default array
         return Utils\Json::decode($defaultValue, true);
     }
     return array();
 }