diff --git a/lib/SP/DataModel/AccountSearchData.php b/lib/SP/DataModel/AccountSearchData.php deleted file mode 100644 index 067464b2..00000000 --- a/lib/SP/DataModel/AccountSearchData.php +++ /dev/null @@ -1,54 +0,0 @@ -. - */ - -namespace SP\DataModel; - -/** - * Class AccountSearchData - * - * @package SP\DataModel - */ -class AccountSearchData extends AccountExtData -{ - /** - * @var int - */ - public $num_files = 0; - - /** - * @return int - */ - public function getNumFiles() - { - return (int)$this->num_files; - } - - /** - * @param int $num_files - */ - public function setNumFiles($num_files) - { - $this->num_files = (int)$num_files; - } -} \ No newline at end of file diff --git a/lib/SP/Services/Account/AccountFileService.php b/lib/SP/Services/Account/AccountFileService.php index 2e5b36bb..49462a45 100644 --- a/lib/SP/Services/Account/AccountFileService.php +++ b/lib/SP/Services/Account/AccountFileService.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link http://syspass.org - * @copyright 2012-2017, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -25,25 +25,43 @@ namespace SP\Services\Account; use SP\Core\Exceptions\SPException; +use SP\Core\Traits\InjectableTrait; use SP\DataModel\FileData; use SP\DataModel\FileExtData; use SP\DataModel\ItemSearchData; -use SP\Mgmt\Files\FileUtil; -use SP\Services\Service; -use SP\Services\ServiceItemInterface; -use SP\Services\ServiceItemTrait; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; -use SP\Util\ImageUtil; +use SP\Repositories\Account\AccountFileRepository; /** * Class AccountFileService * * @package SP\Services\Account */ -class AccountFileService extends Service implements ServiceItemInterface +class AccountFileService { - use ServiceItemTrait; + use InjectableTrait; + + /** + * @var AccountFileRepository + */ + protected $accountFileRepository; + + /** + * AccountFileService constructor. + * + * @throws \SP\Core\Dic\ContainerException + */ + public function __construct() + { + $this->injectDependencies(); + } + + /** + * @param AccountFileRepository $accountFileRepository + */ + public function inject(AccountFileRepository $accountFileRepository) + { + $this->accountFileRepository = $accountFileRepository; + } /** * Creates an item @@ -55,63 +73,7 @@ class AccountFileService extends Service implements ServiceItemInterface */ public function create($itemData) { - $query = /** @lang SQL */ - 'INSERT INTO accFiles - SET accountId = ?, - name = ?, - type = ?, - size = ?, - content = ?, - extension = ?, - thumb = ?'; - - $Data = new QueryData(); - $Data->setQuery($query); - $Data->addParam($itemData->getAccountId()); - $Data->addParam($itemData->getName()); - $Data->addParam($itemData->getType()); - $Data->addParam($itemData->getSize()); - $Data->addParam($itemData->getContent()); - $Data->addParam($itemData->getExtension()); - $Data->setOnErrorMessage(__u('No se pudo guardar el archivo')); - - if (FileUtil::isImage($itemData)) { - $thumbnail = ImageUtil::createThumbnail($itemData->getContent()); - - $Data->addParam($thumbnail ?: 'no_thumb'); - } else { - $Data->addParam('no_thumb'); - } - -// $Log = new Log(); -// $LogMessage = $Log->getLogMessage(); -// $LogMessage->setAction(__('Subir Archivo', false)); -// $LogMessage->addDetails(__('Cuenta', false), AccountUtil::getAccountNameById($this->itemData->getAccfileAccountId())); -// $LogMessage->addDetails(__('Archivo', false), $this->itemData->getAccfileName()); -// $LogMessage->addDetails(__('Tipo', false), $this->itemData->getAccfileType()); -// $LogMessage->addDetails(__('Tamaño', false), $this->itemData->getRoundSize() . 'KB'); -// -// DbWrapper::getQuery($Data); -// -// $LogMessage->addDescription(__('Archivo subido', false)); -// $Log->writeLog(); -// -// Email::sendEmail($LogMessage); - - DbWrapper::getQuery($Data, $this->db); - - return $this->db->getLastId(); - } - - /** - * Updates an item - * - * @param mixed $itemData - * @return mixed - */ - public function update($itemData) - { - throw new \RuntimeException('Not implemented'); + return $this->accountFileRepository->create($itemData); } /** @@ -120,25 +82,7 @@ class AccountFileService extends Service implements ServiceItemInterface */ public function getInfoById($id) { - $query = /** @lang SQL */ - 'SELECT name, - size, - type, - accountId, - extension, - account_name, - customer_name - FROM accFiles - INNER JOIN accounts ON account_id = accountId - INNER JOIN customers ON customer_id = account_customerId - WHERE id = ? LIMIT 1'; - - $Data = new QueryData(); - $Data->setMapClassName(FileExtData::class); - $Data->setQuery($query); - $Data->addParam($id); - - return DbWrapper::getResults($Data, $this->db); + return $this->accountFileRepository->getInfoById($id); } /** @@ -149,27 +93,7 @@ class AccountFileService extends Service implements ServiceItemInterface */ public function getById($id) { - $query = /** @lang SQL */ - 'SELECT name, - size, - type, - accountId, - content, - thumb, - extension, - account_name, - customer_name - FROM accFiles - INNER JOIN accounts ON account_id = accountId - INNER JOIN customers ON customer_id = account_customerId - WHERE id = ? LIMIT 1'; - - $Data = new QueryData(); - $Data->setMapClassName(FileExtData::class); - $Data->setQuery($query); - $Data->addParam($id); - - return DbWrapper::getResults($Data, $this->db); + return $this->accountFileRepository->getById($id); } /** @@ -179,25 +103,7 @@ class AccountFileService extends Service implements ServiceItemInterface */ public function getAll() { - $query = /** @lang SQL */ - 'SELECT name, - size, - type, - accountId, - content, - thumb, - extension, - account_name, - customer_name - FROM accFiles - INNER JOIN accounts ON account_id = accountId - INNER JOIN customers ON customer_id = account_customerId'; - - $Data = new QueryData(); - $Data->setMapClassName(FileExtData::class); - $Data->setQuery($query); - - return DbWrapper::getResultsArray($Data, $this->db); + return $this->accountFileRepository->getAll(); } /** @@ -208,27 +114,7 @@ class AccountFileService extends Service implements ServiceItemInterface */ public function getByIdBatch(array $ids) { - $query = /** @lang SQL */ - 'SELECT name, - size, - type, - accountId, - content, - thumb, - extension, - account_name, - customer_name - FROM accFiles - LEFT JOIN accounts ON account_id = accountId - LEFT JOIN customers ON customer_id = account_customerId - WHERE id IN (' . $this->getParamsFromArray($ids) . ')'; - - $Data = new QueryData(); - $Data->setMapClassName(FileExtData::class); - $Data->setQuery($query); - $Data->setParams($ids); - - return DbWrapper::getResultsArray($Data, $this->db); + return $this->accountFileRepository->getByIdBatch($ids); } /** @@ -249,93 +135,33 @@ class AccountFileService extends Service implements ServiceItemInterface * Deletes an item * * @param $id - * @return AccountFileService + * @return AccountFileRepository * @throws SPException */ public function delete($id) { - // Eliminamos el archivo de la BBDD - $query = /** @lang SQL */ - 'DELETE FROM accFiles WHERE id = ? LIMIT 1'; - - $Data = new QueryData(); - $Data->setQuery($query); - $Data->addParam($id); - $Data->setOnErrorMessage(__u('Error al eliminar el archivo')); - - DbWrapper::getQuery($Data, $this->db); - - if ($Data->getQueryNumRows() === 0) { - throw new SPException(SPException::SP_INFO, __u('Archivo no encontrado')); - } - - return $this; - } - - /** - * Checks whether the item is in use or not - * - * @param $id int - */ - public function checkInUse($id) - { - throw new \RuntimeException('Not implemented'); - } - - /** - * Checks whether the item is duplicated on updating - * - * @param mixed $itemData - */ - public function checkDuplicatedOnUpdate($itemData) - { - throw new \RuntimeException('Not implemented'); - } - - /** - * Checks whether the item is duplicated on adding - * - * @param mixed $itemData - */ - public function checkDuplicatedOnAdd($itemData) - { - throw new \RuntimeException('Not implemented'); + return $this->accountFileRepository->delete($id); } /** * Searches for items by a given filter * - * @param ItemSearchData $SearchData + * @param ItemSearchData $searchData * @return mixed */ - public function search(ItemSearchData $SearchData) + public function search(ItemSearchData $searchData) { - $Data = new QueryData(); - $Data->setMapClassName(FileExtData::class); - $Data->setSelect('id, name, CONCAT(ROUND(size/1000, 2), "KB") AS size, thumb, type, account_name, customer_name'); - $Data->setFrom('accFiles INNER JOIN accounts ON accountId = account_id INNER JOIN customers ON customer_id = account_customerId'); - $Data->setOrder('name'); + return $this->accountFileRepository->search($searchData); + } - if ($SearchData->getSeachString() !== '') { - $Data->setWhere('name LIKE ? OR type LIKE ? OR account_name LIKE ? OR customer_name LIKE ?'); - - $search = '%' . $SearchData->getSeachString() . '%'; - $Data->addParam($search); - $Data->addParam($search); - $Data->addParam($search); - $Data->addParam($search); - } - - $Data->setLimit('?,?'); - $Data->addParam($SearchData->getLimitStart()); - $Data->addParam($SearchData->getLimitCount()); - - DbWrapper::setFullRowCount(); - - $queryRes = DbWrapper::getResultsArray($Data, $this->db); - - $queryRes['count'] = $Data->getQueryNumRows(); - - return $queryRes; + /** + * Returns the item for given id + * + * @param int $id + * @return mixed + */ + public function getByAccountId($id) + { + return $this->accountFileRepository->getByAccountId($id); } } \ No newline at end of file