public function prepareData()
 {
     parent::prepareData();
     $this->messageManager = new CoreFormValidationMessageContainer();
     if (!$this->isUserPermitted()) {
         $this->messageManager->addMessage('fileDeleteErrorNoPermission');
         return;
     }
     $this->fileDAO = new FileDAO();
     CoreServices2::getDB()->transactionStart();
     $this->initFileRecord();
     if (!empty($this->fileRecord['id'])) {
         $this->initBaseRecordDAO();
         $this->initBaseRecord();
         if (!$this->hasUserPermissionsForRecord()) {
             $this->messageManager->addMessage('fileDeleteErrorNoPermission');
             return;
         }
         $this->checkDataConsistency();
         if (!$this->messageManager->isAnyErrorMessage()) {
             $this->deleteFileRecord();
         }
     } else {
         $this->messageManager->addMessage('fileDeleteErrorNoSuchFile');
     }
     CoreServices2::getDB()->transactionCommit();
 }
 protected function checkUserPermissionsForRecord()
 {
     if ($this->currentUser['adminRole'] < $this->adminRoles['adminRoleSuperadmin'] && $this->currentUser['id'] != $this->record['id']) {
         CoreServices2::getDB()->transactionCommit();
         CoreUtils::redirect($this->getListPageAddress());
     }
 }
    public function getOldRecords($time, $limit)
    {
        $db = CoreServices2::getDB();
        $sql = '
			SELECT *
			FROM _tmpRecord
			WHERE _tmpRecordCreateTime < ' . $db->prepareInputValue($time) . '
			ORDER BY _tmpRecordCreateTime ASC
			LIMIT 0, ' . $db->prepareInputValue($limit);
        return $db->getRows($sql);
    }
    public function getRecordPath($recordId)
    {
        $db = CoreServices2::getDB();
        $sql = '
			SELECT
				company.id as companyId, companyName
			FROM
                                company
			WHERE
				company.id = ' . $db->prepareInputValue($recordId) . '
                        LIMIT 0,1';
        return $db->getRow($sql);
    }
    public function getWellPointsByWellId($wellId)
    {
        $db = CoreServices2::getDB();
        $sql = '
			SELECT
				*
			FROM
				well3dpoint 
			WHERE
				wellId = ' . $db->prepareInputValue($wellId) . '
                        Order By number';
        return $db->getRows($sql);
    }
 public function prepareData()
 {
     parent::prepareData();
     $this->wellId = CoreServices2::getRequest()->getFromPost('wellId');
     $this->wellType = CoreServices2::getRequest()->getFromPost('wellType');
     $this->wellPoints = CoreServices2::getRequest()->getFromPost('wellPoints');
     $this->messageManager = new CoreFormValidationMessageContainer();
     CoreServices2::getDB()->transactionStart();
     if (!$this->hasUserPermissionsForRecord()) {
         $this->messageManager->addMessage('fileDeleteErrorNoPermission');
         return;
     }
     $pointsData = json_decode($this->wellPoints);
     foreach ($pointsData as $key => $pointData) {
         if ($pointData == "") {
             return false;
         }
         $keyValue = explode("_", $key);
         if ($keyValue[1] != "rowOrder") {
             $wellPoints[$keyValue[2]][$keyValue[1]] = $pointData;
         } else {
             $pointsOrder = explode(',', $pointData);
         }
     }
     // find old points connectet with welll
     $well3dDAO = new Well3DPointDAO();
     $oldPoints = $well3dDAO->getWellPointsByWellId($this->wellId);
     // delete old points
     if (!empty($oldPoints)) {
         foreach ($oldPoints as $point) {
             $well3dDAO->delete($point);
         }
     }
     $pointsCounter = 1;
     foreach ($pointsOrder as $order) {
         // save new points into table
         $recordTemplate = $well3dDAO->getRecordTemplate();
         $recordTemplate['wellId'] = $this->wellId;
         $recordTemplate['number'] = $pointsCounter;
         $recordTemplate['X'] = $wellPoints[$order]['X'];
         $recordTemplate['Y'] = $wellPoints[$order]['Y'];
         $recordTemplate['Z'] = $wellPoints[$order]['Z'];
         $recordTemplate['LP'] = $wellPoints[$order]['LP'];
         $recordTemplate['alfa'] = $wellPoints[$order]['alfa'];
         $recordTemplate['beta'] = $wellPoints[$order]['beta'];
         $well3dDAO->save($recordTemplate);
         $pointsCounter++;
     }
     CoreServices2::getDB()->transactionCommit();
 }
 protected function handleDeleteRequest()
 {
     $ids = $this->deletionForm->getField('_delete')->getValue();
     if (empty($ids)) {
         return;
     }
     CoreServices2::getDB()->transactionStart();
     foreach ($this->recordList as $record) {
         if (in_array($record['id'], $ids)) {
             $this->dao->delete($record);
         }
     }
     CoreServices2::getDB()->transactionCommit();
 }
    public function getRecordByAbbr($abbr)
    {
        $db = CoreServices2::getDB();
        $sql = '
			SELECT *
			FROM country
			WHERE countryAbbr = ' . $db->prepareInputValue(strtoupper($abbr)) . '
			ORDER BY id
			LIMIT 0, 1';
        $rows = $db->getRows($sql);
        if (sizeof($rows) == 1) {
            return $rows[0];
        }
        return $this->getRecordTemplate();
    }
    public function getRecordPath($recordId)
    {
        $db = CoreServices2::getDB();
        $sql = '
			SELECT
				companyId, companyName, project.id as projectId, projectName
			FROM
				project 
                        Left join 
                                company ON (companyId = company.id)
			WHERE
				project.id = ' . $db->prepareInputValue($recordId) . '
                        LIMIT 0,1';
        return $db->getRow($sql);
    }
 protected function initTmpRecord()
 {
     $tmpId = CoreServices::get('request')->getFromRequest('_tmpId');
     if (!empty($tmpId)) {
         $this->tmpRecord = $this->tmpRecordDAO->getRecordById($tmpId);
         if (empty($this->tmpRecord['id'])) {
             CoreServices2::getDB()->transactionCommit();
             CoreUtils::redirect($this->getListPageAddress());
         }
     } else {
         $this->tmpRecord = $this->tmpRecordDAO->getRecordTemplate();
         $this->tmpRecord['recordType'] = $this->getRecordType();
         $this->tmpRecord['_tmpRecordCreateTime'] = CoreUtils::getDateTime();
         $this->tmpRecord['_tmpRecordSessionId'] = CoreServices2::getRequest()->getSessionId();
         $this->tmpRecordDAO->save($this->tmpRecord);
     }
 }
    public function getRecordPath($recordId)
    {
        $db = CoreServices2::getDB();
        $sql = '
			SELECT
				companyName, projectName, projectId, companyId, site.id as siteId, siteName
			FROM
				site
                        LEFT JOIN 
                                project on (projectId = project.id)
                        LEFT JOIN 
                                company ON (companyId = company.id)
			WHERE
				site.id = ' . $db->prepareInputValue($recordId) . '
                        LIMIT 0,1';
        return $db->getRow($sql);
    }
 public function prepareData()
 {
     parent::prepareData();
     if (!$this->isUserPermitted()) {
         $this->messageManager = new CoreFormValidationMessageContainer();
         $this->messageManager->addMessage('fileUploadErrorNoPermission');
         return;
     }
     $this->initForm();
     $this->createFormFields();
     $this->setFieldValuesFromRequest();
     $this->fileDAO = new FileDAO();
     CoreServices2::getDB()->transactionStart();
     $this->validateFields();
     if (!$this->messageManager->isAnyErrorMessage()) {
         $this->saveFileRecord();
     }
     CoreServices2::getDB()->transactionCommit();
     if (!empty($this->fileRecord['id'])) {
         $this->initFileLinkHTML();
     }
 }
    public function getList($recordType, $statsSimpleName, &$recordColumns, $pagination, $order)
    {
        $recordColumnsModified = $recordColumns;
        // kopia! to ważne!
        $this->prepareRecordColumnList($recordColumnsModified, $recordType);
        $columnsSQL = $this->adaptColumnsSQL(implode(', ', array_keys($recordColumnsModified)) . ', statsSimpleValue');
        $orderBySQL = $this->orderSQL($columns, $order);
        $offset = null;
        $limit = null;
        if ($pagination->getType() == 'Standard') {
            $offset = $pagination->getCurrentOffset();
            $limit = $pagination->getMaxRecordsOnPage();
        }
        $db = CoreServices2::getDB();
        $sql = '
			SELECT
				' . $recordType . '.id AS ' . $recordType . 'Id,
				' . $columnsSQL . '
			FROM statsSimple, ' . $recordType . '
			WHERE
				statsSimple.recordType = ' . $db->prepareInputValue($recordType) . '
				AND statsSimpleName = ' . $db->prepareInputValue($statsSimpleName) . '
				AND statsSimple.recordId = ' . $recordType . '.id
			ORDER BY ' . $orderBySQL . '
			' . $this->getLimitSQLClause($offset, $limit);
        return $db->getRows($sql);
    }
    public function getAllIds()
    {
        $sql = '
			SELECT id
			FROM ' . $this->getTableName() . '
			WHERE 1
			ORDER BY id';
        $rows = CoreServices2::getDB()->getRows($sql);
        $result = array();
        foreach ($rows as $row) {
            $result[] = $row['id'];
        }
        return $result;
    }
    /**
     * It is enough to call this function only once after the whole subtree was removed;
     * of course in such case the parameter is the subtree root.
     * This must be triggered by a controller.
     * Keeping consistency of the tree structure is controllers' responsibility.
     */
    public function updateTreeOnDelete(&$record)
    {
        $db = CoreServices2::getDB();
        $difference = intval($record['subpageRight']) - intval($record['subpageLeft']) + 1;
        $sql = '
			UPDATE subpage
			SET subpageLeft = subpageLeft - ' . $difference . '
			WHERE
				subpageLeft > ' . $db->prepareInputValue($record['subpageRight']);
        $db->change($sql);
        $sql = '
			UPDATE subpage
			SET subpageRight = subpageRight - ' . $difference . '
			WHERE
				subpageRight > ' . $db->prepareInputValue($record['subpageRight']);
        $db->change($sql);
    }
    /**
     * Powinno byc raczej getFirstImageListByRecordIds
     * 
     * @param string $recordType
     * @param array $recordIds
     * @param string $filePosition
     * @return array
     */
    public function getFirstImageListByRecordList($recordType, &$recordIds, $filePosition)
    {
        if (empty($recordIds)) {
            return array();
        }
        $db = CoreServices2::getDB();
        $preparedIds = array();
        foreach ($recordIds as $id) {
            $preparedIds[] = $db->prepareInputValue($id);
        }
        $idsSQL = implode(', ', $preparedIds);
        $recordTypeSQL = $db->prepareInputValue($recordType);
        $fileCategorySQL = $db->prepareInputValue('image');
        $filePositionSQL = $db->prepareInputValue($filePosition);
        $sql = '
			SELECT
				`file`.*
			FROM
				`file`, (
					SELECT recordId, MIN(fileOrder) AS _firstFileOrder
					FROM `file` AS _file
					WHERE
						recordType = ' . $recordTypeSQL . '
						AND recordId IN (' . $idsSQL . ')
						AND fileCategory = ' . $fileCategorySQL . '
						AND filePosition = ' . $filePositionSQL . '
					GROUP BY
						recordId
				) AS _helper
			WHERE
				recordType = ' . $recordTypeSQL . '
				AND _helper.recordId = `file`.recordId
				AND `file`.fileOrder = _firstFileOrder
				AND `file`.filePosition = ' . $filePositionSQL;
        $rows = $db->getRows($sql);
        $res = array();
        foreach ($rows as $row) {
            $res[$row['recordId']] = $row;
        }
        return $res;
    }
    public function getDeletedAccountsCount($dateFrom = null, $dateTo = null)
    {
        $db = CoreServices2::getDB();
        $sql = '
			SELECT
				count(id) as num
			FROM
				user
			WHERE
				userState = \'deleted\'';
        // 'OR userState = \'forDeletion\''
        if (!empty($dateFrom)) {
            $sql .= '
				AND userEraseTime >= ' . $db->prepareInputValue($dateFrom . ' 00:00:00');
        }
        if (!empty($dateTo)) {
            $sql .= '
				AND userEraseTime <= ' . $db->prepareInputValue($dateTo . ' 23:59:59');
        }
        $row = $db->getRow($sql);
        return $row['num'];
    }
    public function getWellOwnerUserId($wellId)
    {
        $db = CoreServices2::getDB();
        $sql = '
			SELECT
				userId
			FROM
				well   
                        LEFT JOIN 
                                site on (well.siteId = site.id)
                        LEFT JOIN 
                                project on (projectId = project.id)
                        LEFT JOIN 
                                company ON (companyId = company.id)
			WHERE
				well.id = ' . $db->prepareInputValue($wellId) . '
                        LIMIT 0,1';
        return $db->getRow($sql);
    }