public static function GetCurrentUserName()
 {
     if (UserRights::IsImpersonated()) {
         $sUserString = Dict::Format('UI:Archive_User_OnBehalfOf_User', UserRights::GetRealUserFriendlyName(), UserRights::GetUserFriendlyName());
     } else {
         $sUserString = UserRights::GetUserFriendlyName();
     }
     return $sUserString;
 }
Esempio n. 2
0
 $oSearch = new DBObjectSearch($sClassName);
 $oSearch->AddCondition('id', 0, '=');
 // Make sure we create an empty set
 $oSet = new CMDBObjectSet($oSearch);
 $sResult = cmdbAbstractObject::GetSetAsCSV($oSet, array('showMandatoryFields' => true));
 $sClassDisplayName = MetaModel::GetName($sClassName);
 $sDisposition = utils::ReadParam('disposition', 'inline');
 if ($sDisposition == 'attachment') {
     switch ($sFormat) {
         case 'xlsx':
             $oPage = new ajax_page("");
             $oPage->SetContentType('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
             $oPage->SetContentDisposition('attachment', $sClassDisplayName . '.xlsx');
             require_once APPROOT . '/application/excelexporter.class.inc.php';
             $writer = new XLSXWriter();
             $writer->setAuthor(UserRights::GetUserFriendlyName());
             $aHeaders = array(0 => explode(',', $sResult));
             // comma is the default separator
             $writer->writeSheet($aHeaders, $sClassDisplayName, array());
             $oPage->add($writer->writeToString());
             break;
         case 'csv':
         default:
             $oPage = new CSVPage("");
             $oPage->add_header("Content-type: text/csv; charset=utf-8");
             $oPage->add_header("Content-disposition: attachment; filename=\"{$sClassDisplayName}.csv\"");
             $oPage->no_cache();
             $oPage->add($sResult);
     }
 } else {
     $oPage = new ajax_page("");
Esempio n. 3
0
 public function AddLogEntryFromJSON($oJson, $bCheckUserId = true)
 {
     $sText = isset($oJson->message) ? $oJson->message : '';
     if (isset($oJson->user_id)) {
         if (!UserRights::IsAdministrator()) {
             throw new Exception("Only administrators can set the user id", RestResult::UNAUTHORIZED);
         }
         if ($bCheckUserId && $oJson->user_id != 0) {
             try {
                 $oUser = RestUtils::FindObjectFromKey('User', $oJson->user_id);
             } catch (Exception $e) {
                 throw new Exception('user_id: ' . $e->getMessage(), $e->getCode());
             }
             $iUserId = $oUser->GetKey();
             $sOnBehalfOf = $oUser->GetFriendlyName();
         } else {
             $iUserId = $oJson->user_id;
             $sOnBehalfOf = $oJson->user_login;
         }
     } else {
         $iUserId = UserRights::GetUserId();
         $sOnBehalfOf = UserRights::GetUserFriendlyName();
     }
     if (isset($oJson->date)) {
         $oDate = new DateTime($oJson->date);
         $iDate = (int) $oDate->format('U');
     } else {
         $iDate = time();
     }
     $sDate = date(Dict::S('UI:CaseLog:DateFormat'), $iDate);
     $sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId);
     $iSepLength = strlen($sSeparator);
     $iTextlength = strlen($sText);
     $this->m_sLog = $sSeparator . $sText . $this->m_sLog;
     // Latest entry printed first
     $this->m_aIndex[] = array('user_name' => $sOnBehalfOf, 'user_id' => $iUserId, 'date' => $iDate, 'text_length' => $iTextlength, 'separator_length' => $iSepLength);
     $this->m_bModified = true;
 }
Esempio n. 4
0
 ApplicationContext::SetUrlMakerClass('MyPortalURLMaker');
 $aClasses = explode(',', MetaModel::GetConfig()->Get('portal_tickets'));
 $sMainClass = trim(reset($aClasses));
 if (!class_exists($sMainClass)) {
     $oP = new WebPage(Dict::S('Portal:Title'));
     $oP->p(dict::Format('Portal:NoRequestMgmt', UserRights::GetUserFriendlyName()));
 } else {
     $oUserOrg = GetUserOrg();
     $sCode = $oUserOrg->Get('code');
     $sAlternateStylesheet = '';
     if (@file_exists("./{$sCode}/portal.css")) {
         $sAlternateStylesheet = "{$sCode}";
     }
     $oP = new PortalWebPage(Dict::S('Portal:Title'), $sAlternateStylesheet);
     $oP->EnableDisconnectButton(utils::CanLogOff());
     $oP->SetWelcomeMessage(Dict::Format('Portal:WelcomeUserOrg', UserRights::GetUserFriendlyName(), $oUserOrg->GetName()));
     if (is_object($oUserOrg)) {
         switch ($sOperation) {
             case 'show_closed':
                 $oP->set_title(Dict::S('Portal:ShowClosed'));
                 DisplayMainMenu($oP);
                 ShowClosedTickets($oP);
                 break;
             case 'create_request':
                 $oP->set_title(Dict::S('Portal:CreateNewRequest'));
                 DisplayMainMenu($oP);
                 if (!MetaModel::DBIsReadOnly()) {
                     CreateRequest($oP, $oUserOrg);
                 }
                 break;
             case 'details':
 public function GetFooter()
 {
     $hFile = @fopen($this->aStatusInfo['tmp_file'], 'rb');
     if ($hFile === false) {
         throw new Exception('ExcelBulkExport: Failed to open temporary data file: "' . $this->aStatusInfo['tmp_file'] . '" for reading.');
     }
     $sHeaders = fgets($hFile);
     $aHeaders = json_decode($sHeaders, true);
     $aData = array();
     while ($sLine = fgets($hFile)) {
         $aRow = json_decode($sLine);
         $aData[] = $aRow;
     }
     fclose($hFile);
     $fStartExcel = microtime(true);
     $writer = new XLSXWriter();
     $writer->setAuthor(UserRights::GetUserFriendlyName());
     $aHeaderTypes = array();
     $aHeaderNames = array();
     foreach ($aHeaders as $Header) {
         $aHeaderNames[] = $Header['label'];
         $aHeaderTypes[] = $Header['type'];
     }
     $writer->writeSheet($aData, 'Sheet1', $aHeaderTypes, $aHeaderNames);
     $fExcelTime = microtime(true) - $fStartExcel;
     //$this->aStatistics['excel_build_duration'] = $fExcelTime;
     $fTime = microtime(true);
     $data = $writer->writeToString();
     $fExcelSaveTime = microtime(true) - $fTime;
     //$this->aStatistics['excel_write_duration'] = $fExcelSaveTime;
     @unlink($this->aStatusInfo['tmp_file']);
     return $data;
 }
Esempio n. 6
0
 /**
  * Lifecycle action: Set the current logged in user for the given attribute
  */
 public function SetCurrentUser($sAttCode)
 {
     $oAttDef = MetaModel::GetAttributeDef(get_class($this), $sAttCode);
     if ($oAttDef instanceof AttributeString) {
         // Note: the user friendly name is the contact friendly name if a contact is attached to the logged in user
         $this->Set($sAttCode, UserRights::GetUserFriendlyName());
     } else {
         if ($oAttDef->IsExternalKey()) {
             if ($oAttDef->GetTargetClass() != 'User') {
                 throw new Exception("SetCurrentUser: the attribute {$sAttCode} must be an external key to 'User', found '" . $oAttDef->GetTargetClass() . "'");
             }
         }
         $this->Set($sAttCode, UserRights::GetUserId());
     }
     return true;
 }
 public function Run()
 {
     $sCode = 'error';
     $iPercentage = 100;
     $sMessage = Dict::Format('ExcelExporter:ErrorUnexpected_State', $this->sState);
     $fTime = microtime(true);
     try {
         switch ($this->sState) {
             case 'new':
                 $oIDSet = new DBObjectSet($this->oSearch);
                 $oIDSet->OptimizeColumnLoad(array('id'));
                 $this->aObjectsIDs = array();
                 while ($oObj = $oIDSet->Fetch()) {
                     $this->aObjectsIDs[] = $oObj->GetKey();
                 }
                 $sCode = 'retrieving-data';
                 $iPercentage = 5;
                 $sMessage = Dict::S('ExcelExporter:RetrievingData');
                 $this->iPosition = 0;
                 $this->aStatistics['objects_count'] = count($this->aObjectsIDs);
                 $this->aStatistics['data_retrieval_duration'] += microtime(true) - $fTime;
                 // The first line of the file is the "headers" specifying the label and the type of each column
                 $this->GetFieldsList($oIDSet, $this->bAdvancedMode);
                 $sRow = json_encode($this->aTableHeaders);
                 $hFile = @fopen($this->GetDataFile(), 'ab');
                 if ($hFile === false) {
                     throw new Exception('ExcelExporter: Failed to open temporary data file: "' . $this->GetDataFile() . '" for writing.');
                 }
                 fwrite($hFile, $sRow . "\n");
                 fclose($hFile);
                 // Next state
                 $this->sState = 'retrieving-data';
                 break;
             case 'retrieving-data':
                 $oCurrentSearch = clone $this->oSearch;
                 $aIDs = array_slice($this->aObjectsIDs, $this->iPosition, $this->iChunkSize);
                 $oCurrentSearch->AddCondition('id', $aIDs, 'IN');
                 $hFile = @fopen($this->GetDataFile(), 'ab');
                 if ($hFile === false) {
                     throw new Exception('ExcelExporter: Failed to open temporary data file: "' . $this->GetDataFile() . '" for writing.');
                 }
                 $oSet = new DBObjectSet($oCurrentSearch);
                 $this->GetFieldsList($oSet, $this->bAdvancedMode);
                 while ($aObjects = $oSet->FetchAssoc()) {
                     $aRow = array();
                     foreach ($this->aAuthorizedClasses as $sAlias => $sClassName) {
                         $oObj = $aObjects[$sAlias];
                         if ($this->bAdvancedMode) {
                             $aRow[] = $oObj->GetKey();
                         }
                         foreach ($this->aFieldsList[$sAlias] as $sAttCodeEx => $oAttDef) {
                             $value = $oObj->Get($sAttCodeEx);
                             if ($value instanceof ormCaseLog) {
                                 // Extract the case log as text and remove the "===" which make Excel think that the cell contains a formula the next time you edit it!
                                 $sExcelVal = trim(preg_replace('/========== ([^=]+) ============/', '********** $1 ************', $value->GetText()));
                             } else {
                                 $sExcelVal = $oAttDef->GetEditValue($value, $oObj);
                             }
                             $aRow[] = $sExcelVal;
                         }
                     }
                     $sRow = json_encode($aRow);
                     fwrite($hFile, $sRow . "\n");
                 }
                 fclose($hFile);
                 if ($this->iPosition + $this->iChunkSize > count($this->aObjectsIDs)) {
                     // Next state
                     $this->sState = 'building-excel';
                     $sCode = 'building-excel';
                     $iPercentage = 80;
                     $sMessage = Dict::S('ExcelExporter:BuildingExcelFile');
                 } else {
                     $sCode = 'retrieving-data';
                     $this->iPosition += $this->iChunkSize;
                     $iPercentage = 5 + round(75 * ($this->iPosition / count($this->aObjectsIDs)));
                     $sMessage = Dict::S('ExcelExporter:RetrievingData');
                 }
                 break;
             case 'building-excel':
                 $hFile = @fopen($this->GetDataFile(), 'rb');
                 if ($hFile === false) {
                     throw new Exception('ExcelExporter: Failed to open temporary data file: "' . $this->GetDataFile() . '" for reading.');
                 }
                 $sHeaders = fgets($hFile);
                 $aHeaders = json_decode($sHeaders, true);
                 $aData = array();
                 while ($sLine = fgets($hFile)) {
                     $aRow = json_decode($sLine);
                     $aData[] = $aRow;
                 }
                 fclose($hFile);
                 @unlink($this->GetDataFile());
                 $fStartExcel = microtime(true);
                 $writer = new XLSXWriter();
                 $writer->setAuthor(UserRights::GetUserFriendlyName());
                 $writer->writeSheet($aData, 'Sheet1', $aHeaders);
                 $fExcelTime = microtime(true) - $fStartExcel;
                 $this->aStatistics['excel_build_duration'] = $fExcelTime;
                 $fTime = microtime(true);
                 $writer->writeToFile($this->GetExcelFilePath());
                 $fExcelSaveTime = microtime(true) - $fTime;
                 $this->aStatistics['excel_write_duration'] = $fExcelSaveTime;
                 // Next state
                 $this->sState = 'done';
                 $sCode = 'done';
                 $iPercentage = 100;
                 $sMessage = Dict::S('ExcelExporter:Done');
                 break;
             case 'done':
                 $this->sState = 'done';
                 $sCode = 'done';
                 $iPercentage = 100;
                 $sMessage = Dict::S('ExcelExporter:Done');
                 break;
         }
     } catch (Exception $e) {
         $sCode = 'error';
         $sMessage = $e->getMessage();
     }
     $this->aStatistics['total_duration'] += microtime(true) - $fTime;
     $peak_memory = memory_get_peak_usage(true);
     if ($peak_memory > $this->aStatistics['peak_memory_usage']) {
         $this->aStatistics['peak_memory_usage'] = $peak_memory;
     }
     return array('code' => $sCode, 'message' => $sMessage, 'percentage' => $iPercentage);
 }