application = $application; $config = $this->getContainer()->get('config'); $this->passwordHash = new \Espo\Core\Utils\PasswordHash($config); } protected function getApplication() { return $this->application; } protected function getContainer() { return $this->application->getContainer(); } protected function getPasswordHash() { return $this->passwordHash; } public function loadData($dataFile) { if (!file_exists($dataFile)) { return; } $data = include($dataFile); $this->handleData($data); } public function setData(array $data) { $this->handleData($data); } protected function handleData(array $fullData) { foreach ($fullData as $type => $data) { $methodName = 'load' . ucfirst($type); if (!method_exists($this, $methodName)) { throw new \Exception('DataLoader: Data type is not supported in dataFile ['.$dataFile.'].'); } $this->$methodName($data); } } public function loadFiles($path) { try { $fileManager = $this->getContainer()->get('fileManager'); $fileManager->copy($path, '.', true); } catch (Exception $e) { throw new \Exception('Error loadFiles: ' . $e->getMessage()); } } protected function loadEntities(array $data) { $entityManager = $this->getContainer()->get('entityManager'); foreach($data as $entityName => $entities) { foreach($entities as $entityData) { $entity = $entityManager->getEntity($entityName, $entityData['id']); if (empty($entity)) { $entity = $entityManager->getEntity($entityName); } foreach($entityData as $field => $value) { if ($field == "password" && $entityName == "User") { $value = $this->getPasswordHash()->hash($value); } $entity->set($field, $value); } try { $entityManager->saveEntity($entity); } catch (\Exception $e) { throw new \Exception('Error loadEntities: ' . $e->getMessage() . ', ' . print_r($entityData, true)); } } } } protected function loadConfig(array $data) { if (empty($data)) { return; } $config = $this->getContainer()->get('config'); $config->set($data); try { $config->save(); } catch (\Exception $e) { throw new \Exception('Erro loadConfig: ' . $e->getMessage()); } } protected function loadPreferences(array $data) { $entityManager = $this->getContainer()->get('entityManager'); foreach ($data as $userId => $params) { $entityManager->getRepository('Preferences')->resetToDefaults($userId); $preferences = $entityManager->getEntity('Preferences', $userId); $preferences->set($params); try { $entityManager->saveEntity($preferences); } catch (\Exception $e) { throw new \Exception('Error loadPreferences: ' . $e->getMessage()); } } } protected function loadSql(array $data) { if (empty($data)) { return; } $pdo = $this->getContainer()->get('entityManager')->getPDO(); foreach ($data as $sql) { try { $pdo->query($sql); } catch (Exception $e) { throw new \Exception('Error loadSql: ' . $e->getMessage() . ', sql: ' . $sql); } } } }