/**
* iTop User Portal main page
*
* @copyright Copyright (C) 2010-2013 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
require_once('../approot.inc.php');
require_once(APPROOT.'/application/application.inc.php');
require_once(APPROOT.'/application/nicewebpage.class.inc.php');
require_once(APPROOT.'/application/wizardhelper.class.inc.php');
/**
* Helper to determine the supported types of tickets
*/
function GetTicketClasses()
{
$aClasses = array();
foreach (explode(',', MetaModel::GetConfig()->Get('portal_tickets')) as $sRawClass)
{
$sRawClass = trim($sRawClass);
if (!MetaModel::IsValidClass($sRawClass))
{
throw new Exception("Class '$sRawClass' is not a valid class, please review your configuration (portal_tickets)");
}
if (!MetaModel::IsParentClass('Ticket', $sRawClass))
{
throw new Exception("Class '$sRawClass' does not inherit from Ticket, please review your configuration (portal_tickets)");
}
$aClasses[] = $sRawClass;
}
return $aClasses;
}
/**
* Helper to protect the portal against malicious usages
* Throws an exception if the current user is not allowed to view the object details
*/
function ValidateObject($oObject)
{
if (IsPowerUser())
{
$sValidationDefine = 'PORTAL_'.strtoupper(get_class($oObject)).'_DISPLAY_POWERUSER_QUERY';
}
else
{
$sValidationDefine = 'PORTAL_'.strtoupper(get_class($oObject)).'_DISPLAY_QUERY';
}
if (defined($sValidationDefine))
{
$sValidationOql = constant($sValidationDefine);
$oSearch = DBObjectSearch::FromOQL($sValidationOql);
$oSearch->AddCondition('id', $oObject->GetKey());
if ($iUser = UserRights::GetContactId())
{
$oContact = MetaModel::GetObject('Contact', $iUser);
$aArgs = $oContact->ToArgs('contact');
}
else
{
$aArgs = array();
}
$oSet = new DBObjectSet($oSearch, array(), $aArgs);
if ($oSet->Count() == 0)
{
throw new SecurityException('You are not allowed to access the object '.get_class($oObject).'::'.$oObject->GetKey());
}
}
}
/**
* Helper to get the relevant constant
*/
function GetConstant($sClass, $sName)
{
$sConstName = 'PORTAL_'.strtoupper($sClass).'_'.$sName;
if (defined($sConstName))
{
return constant($sConstName);
}
else
{
throw new Exception("Missing portal constant '$sConstName'");
}
}
/**
* Helper to determine the ticket class given the service subcategory
*/
function ComputeClass($iSubSvcId)
{
$aClasses = GetTicketClasses();
if ((PORTAL_SET_TYPE_FROM == '') || (PORTAL_TYPE_TO_CLASS == ''))
{
// return the first enabled class
$sClass = reset($aClasses);
}
else
{
$oServiceSubcat = MetaModel::GetObject('ServiceSubcategory', $iSubSvcId, true, true /* allow all data*/);
$sTicketType = $oServiceSubcat->Get(PORTAL_SET_TYPE_FROM);
$aMapping = json_decode(PORTAL_TYPE_TO_CLASS, true);
if (!array_key_exists($sTicketType, $aMapping))
{
throw new Exception("Ticket type '$sTicketType' not found in the mapping (".implode(', ', array_keys($aMapping))."). Please contact your administrator.");
}
$sClass = $aMapping[$sTicketType];
if (!in_array($sClass, $aClasses))
{
throw new Exception("Service subcategory #$iSubSvcId has a ticket type ($sClass) that is not known by the portal, please contact your administrator.");
}
}
return $sClass;
}
/**
* Helper to limit the service categories depending on the current settings
*/
function RestrictSubcategories(&$oSearch)
{
$aMapping = (PORTAL_TYPE_TO_CLASS == '') ? array() : json_decode(PORTAL_TYPE_TO_CLASS, true);
foreach($aMapping as $sTicketType => $sClass)
{
if (!in_array($sClass, GetTicketClasses()))
{
// Exclude this value for the result set
$oSearch->AddCondition(PORTAL_SET_TYPE_FROM, $sTicketType, '!=');
}
}
}
/**
* Displays the portal main menu
* @param WebPage $oP The current web page
* @return void
*/
function DisplayMainMenu(WebPage $oP)
{
$oP->AddMenuButton('showongoing', 'Portal:ShowOngoing', '../portal/index.php?operation=show_ongoing');
$oP->AddMenuButton('newrequest', 'Portal:CreateNewRequest', '../portal/index.php?operation=create_request');
$oP->AddMenuButton('showclosed', 'Portal:ShowClosed', '../portal/index.php?operation=show_closed');
if (UserRights::CanChangePassword())
{
$oP->AddMenuButton('change_pwd', 'Portal:ChangeMyPassword', '../portal/index.php?loginop=change_pwd');
}
}
/**
* Displays the current tickets
* @param WebPage $oP The current web page
* @return void
*/
function ShowOngoingTickets(WebPage $oP)
{
$oP->add("
\n");
$oP->add("
".Dict::S('Portal:OpenRequests')."
\n");
ListOpenRequests($oP);
$oP->add("
\n");
$oP->add("
\n");
$oP->add("
".Dict::S('Portal:ResolvedRequests')."
\n");
ListResolvedRequests($oP);
$oP->add("
\n");
}
/**
* Displays the closed tickets
* @param WebPage $oP The current web page
* @return void
*/
function ShowClosedTickets(WebPage $oP)
{
$oP->add("
\n");
//$oP->add("
".Dict::S('Portal:ListClosedTickets')."
\n");
ListClosedTickets($oP);
$oP->add("
\n");
}
/**
* Displays the form to select a Service Category Id (among the valid ones for the specified user Organization)
* @param WebPage $oP Web page for the form output
* @param Organization $oUserOrg The organization of the current user
* @return void
*/
function SelectServiceCategory($oP, $oUserOrg)
{
$aParameters = $oP->ReadAllParams(PORTAL_ALL_PARAMS.',template_id');
$oSearch = DBObjectSearch::FromOQL(PORTAL_SERVICECATEGORY_QUERY);
$oSearch->AllowAllData(); // In case the user has the rights on his org only
$oSet = new CMDBObjectSet($oSearch, array(), array('org_id' => $oUserOrg->GetKey()));
if ($oSet->Count() == 1)
{
$oService = $oSet->Fetch();
$iSvcCategory = $oService->GetKey();
// Only one Category, skip this step in the wizard
SelectServiceSubCategory($oP, $oUserOrg, $iSvcCategory);
}
else
{
$oP->add("
\n");
$oP->DumpHiddenParams($aParameters, array('service_id'));
$oP->add("");
$oP->WizardFormButtons(BUTTON_NEXT | BUTTON_CANCEL); // NO back button since it's the first step of the Wizard
$oP->WizardFormEnd();
$oP->WizardCheckSelectionOnSubmit(Dict::S('Portal:PleaseSelectOneService'));
$oP->add("
\n");
}
}
/**
* Displays the form to select a Service Subcategory Id (among the valid ones for the specified user Organization)
* and based on the page's parameter 'service_id'
* @param WebPage $oP Web page for the form output
* @param Organization $oUserOrg The organization of the current user
* @param $iSvcId Id of the selected service in case of pass-through (when there is only one service)
* @return void
*/
function SelectServiceSubCategory($oP, $oUserOrg, $iSvcId = null)
{
$aParameters = $oP->ReadAllParams(PORTAL_ALL_PARAMS.',template_id');
if ($iSvcId == null)
{
$iSvcId = $aParameters['service_id'];
}
else
{
$aParameters['service_id'] = $iSvcId;
}
$iDefaultSubSvcId = isset($aParameters['servicesubcategory_id']) ? $aParameters['servicesubcategory_id'] : 0;
$iDefaultWizNext = 2;
$oSearch = DBObjectSearch::FromOQL(PORTAL_SERVICE_SUBCATEGORY_QUERY);
RestrictSubcategories($oSearch);
$oSearch->AllowAllData(); // In case the user has the rights on his org only
$oSet = new CMDBObjectSet($oSearch, array(), array('svc_id' => $iSvcId, 'org_id' => $oUserOrg->GetKey()));
if ($oSet->Count() == 1)
{
// Only one sub service, skip this step of the wizard
$oSubService = $oSet->Fetch();
$iSubSvdId = $oSubService->GetKey();
SelectRequestTemplate($oP, $oUserOrg, $iSvcId, $iSubSvdId);
}
else
{
$oServiceCategory = MetaModel::GetObject('Service', $iSvcId, false, true /* allow all data*/);
if (is_object($oServiceCategory))
{
$oP->add("
\n");
$oP->DumpHiddenParams($aParameters, array('servicesubcategory_id'));
$oP->add("");
$oP->WizardFormButtons(BUTTON_BACK | BUTTON_NEXT | BUTTON_CANCEL); //Back button automatically discarded if on the first page
$oP->WizardFormEnd();
$oP->WizardCheckSelectionOnSubmit(Dict::S('Portal:PleaseSelectAServiceSubCategory'));
$oP->add("
\n");
}
else
{
$oP->p("Error: Invalid Service: id = $iSvcId");
}
}
}
/**
* Displays the form to select a Template
* @param WebPage $oP Web page for the form output
* @param Organization $oUserOrg The organization of the current user
* @param $iSvcId Id of the selected service in case of pass-through (when there is only one service)
* @param integer $iSubSvcId The identifier of the sub-service (fall through when there is only one sub-service)
* @return void
*/
function SelectRequestTemplate($oP, $oUserOrg, $iSvcId = null, $iSubSvcId = null)
{
$aParameters = $oP->ReadAllParams(PORTAL_ALL_PARAMS.',template_id');
if ($iSvcId != null)
{
$aParameters['service_id'] = $iSvcId;
}
if ($iSubSvcId != null)
{
$aParameters['servicesubcategory_id'] = $iSubSvcId;
}
$iDefaultTemplate = isset($aParameters['template_id']) ? $aParameters['template_id'] : 0;
if (MetaModel::IsValidClass('Template'))
{
$sClass = ComputeClass($aParameters['servicesubcategory_id']);
try
{
$sOql = GetConstant($sClass, 'TEMPLATE_QUERY');
}
catch(Exception $e)
{
// Backward compatibility
$sOql = REQUEST_TEMPLATE_QUERY;
}
$oSearch = DBObjectSearch::FromOQL($sOql);
$oSearch->AllowAllData();
$oSet = new CMDBObjectSet($oSearch, array(), array(
'service_id' => $aParameters['service_id'],
'servicesubcategory_id' => $aParameters['servicesubcategory_id']
));
if ($oSet->Count() == 0)
{
RequestCreationForm($oP, $oUserOrg, $aParameters['service_id'], $aParameters['servicesubcategory_id']);
return;
}
elseif ($oSet->Count() == 1)
{
$oTemplate = $oSet->Fetch();
$iTemplateId = $oTemplate->GetKey();
RequestCreationForm($oP, $oUserOrg, $aParameters['service_id'], $aParameters['servicesubcategory_id'], $iTemplateId);
return;
}
$oServiceSubCategory = MetaModel::GetObject('ServiceSubcategory', $aParameters['servicesubcategory_id'], false);
if (is_object($oServiceSubCategory))
{
$oP->add("
\n");
//ShowOngoingTickets($oP);
return;
}
// Validate the parameters
// 1) ServiceCategory
$oSearch = DBObjectSearch::FromOQL(PORTAL_VALIDATE_SERVICECATEGORY_QUERY);
$oSearch->AllowAllData(); // In case the user has the rights on his org only
$oSet = new CMDBObjectSet($oSearch, array(), array('id' => $aParameters['service_id'], 'org_id' => $oUserOrg->GetKey()));
if ($oSet->Count() != 1)
{
// Invalid service for the current user !
throw new Exception("Invalid Service Category: id={$aParameters['service_id']} - count: ".$oSet->Count());
}
$oServiceCategory = $oSet->Fetch();
// 2) Service Subcategory
$oSearch = DBObjectSearch::FromOQL(PORTAL_VALIDATE_SERVICESUBCATEGORY_QUERY);
RestrictSubcategories($oSearch);
$oSearch->AllowAllData(); // In case the user has the rights on his org only
$oSet = new CMDBObjectSet($oSearch, array(), array('service_id' => $aParameters['service_id'], 'id' =>$aParameters['servicesubcategory_id'],'org_id' => $oUserOrg->GetKey() ));
if ($oSet->Count() != 1)
{
// Invalid subcategory
throw new Exception("Invalid ServiceSubcategory: id={$aParameters['servicesubcategory_id']} for service category ".$oServiceCategory->GetName()."({$aParameters['service_id']}) - count: ".$oSet->Count());
}
$oServiceSubCategory = $oSet->Fetch();
$sClass = ComputeClass($oServiceSubCategory->GetKey());
$oRequest = MetaModel::NewObject($sClass);
$aAttList = array_merge(explode(',', GetConstant($sClass, 'FORM_ATTRIBUTES')), array('service_id', 'servicesubcategory_id'));
$oRequest->UpdateObjectFromPostedForm('' /* form prefix */, $aAttList);
$oRequest->Set('org_id', $oUserOrg->GetKey());
$oRequest->Set('caller_id', UserRights::GetContactId());
if (isset($aParameters['moreinfo']))
{
// There is a template, insert it into the description
$sLogAttCode = GetConstant($sClass, 'PUBLIC_LOG');
$oRequest->Set($sLogAttCode, $aParameters['moreinfo']);
}
$sTypeAttCode = GetConstant($sClass, 'TYPE');
if (($sTypeAttCode != '') && (PORTAL_SET_TYPE_FROM != ''))
{
$oRequest->Set($sTypeAttCode, $oServiceSubCategory->Get(PORTAL_SET_TYPE_FROM));
}
if (MetaModel::IsValidAttCode($sClass, 'origin'))
{
$oRequest->Set('origin', 'portal');
}
$oAttPlugin = new AttachmentPlugIn();
$oAttPlugin->OnFormSubmit($oRequest);
list($bRes, $aIssues) = $oRequest->CheckToWrite();
if ($bRes)
{
if (isset($aParameters['template_id']))
{
$oTemplate = MetaModel::GetObject('Template', $aParameters['template_id']);
$sLogAttCode = GetConstant($sClass, 'PUBLIC_LOG');
$oRequest->Set($sLogAttCode, $oTemplate->GetPostedValuesAsText($oRequest)."\n");
$oRequest->DBInsertNoReload();
$oTemplate->RecordExtraDataFromPostedForm($oRequest);
}
else
{
$oRequest->DBInsertNoReload();
}
$oP->add("
\n");
//DisplayObject($oP, $oRequest, $oUserOrg);
ShowOngoingTickets($oP);
}
else
{
RequestCreationForm($oP, $oUserOrg);
$sIssueDesc = Dict::Format('UI:ObjectCouldNotBeWritten', implode(', ', $aIssues));
$oP->add_ready_script("alert('".addslashes($sIssueDesc)."');");
}
}
/**
* Prompts the user for creating a new request
* @param WebPage $oP The current web page
* @return void
*/
function CreateRequest(WebPage $oP, Organization $oUserOrg)
{
switch($oP->GetWizardStep())
{
case 0:
default:
SelectServiceCategory($oP, $oUserOrg);
break;
case 1:
SelectServiceSubCategory($oP, $oUserOrg);
break;
case 2:
SelectRequestTemplate($oP, $oUserOrg);
break;
case 3:
RequestCreationForm($oP, $oUserOrg);
break;
case 4:
DoCreateRequest($oP, $oUserOrg);
break;
}
}
/**
* Helper to display lists (UserRequest, Incident, etc.)
* Adjust the presentation depending on the following cases:
* - no item at all
* - items of one class only
* - items of several classes
*/
function DisplayRequestLists(WebPage $oP, $aClassToSet)
{
$iNotEmpty = 0; // Count of types for which there are some items to display
foreach ($aClassToSet as $sClass => $oSet)
{
if ($oSet->Count() > 0)
{
$iNotEmpty++;
}
}
if ($iNotEmpty == 0)
{
$oP->p(Dict::S('Portal:NoOpenRequest'));
}
else
{
foreach ($aClassToSet as $sClass => $oSet)
{
if ($iNotEmpty > 1)
{
// Differentiate the sublists
$oP->add("
".MetaModel::GetName($sClass)."
\n");
}
if ($oSet->Count() > 0)
{
$sZList = GetConstant($sClass, 'LIST_ZLIST');
$aZList = explode(',', $sZList);
$oP->DisplaySet($oSet, $aZList, Dict::S('Portal:NoOpenRequest'));
}
}
}
}
/**
* Lists all the currently opened User Requests for the current user
* @param WebPage $oP The current web page
* @return void
*/
function ListOpenRequests(WebPage $oP)
{
$oUserOrg = GetUserOrg();
$aClassToSet = array();
foreach (GetTicketClasses() as $sClass)
{
$sOQL = "SELECT $sClass WHERE org_id = :org_id AND status NOT IN ('closed', 'resolved')";
$oSearch = DBObjectSearch::FromOQL($sOQL);
$iUser = UserRights::GetContactId();
if ($iUser > 0 && !IsPowerUser())
{
$oSearch->AddCondition('caller_id', $iUser);
}
$aClassToSet[$sClass] = new CMDBObjectSet($oSearch, array(), array('org_id' => $oUserOrg->GetKey()));
}
DisplayRequestLists($oP, $aClassToSet);
}
/**
* Lists all the currently resolved (not yet closed) User Requests for the current user
* @param WebPage $oP The current web page
* @return void
*/
function ListResolvedRequests(WebPage $oP)
{
$oUserOrg = GetUserOrg();
$aClassToSet = array();
foreach (GetTicketClasses() as $sClass)
{
$sOQL = "SELECT $sClass WHERE org_id = :org_id AND status = 'resolved'";
$oSearch = DBObjectSearch::FromOQL($sOQL);
$iUser = UserRights::GetContactId();
if ($iUser > 0 && !IsPowerUser())
{
$oSearch->AddCondition('caller_id', $iUser);
}
$aClassToSet[$sClass] = new CMDBObjectSet($oSearch, array(), array('org_id' => $oUserOrg->GetKey()));
}
DisplayRequestLists($oP, $aClassToSet);
}
/**
* Lists all the currently closed tickets
* @param WebPage $oP The current web page
* @return void
*/
function ListClosedTickets(WebPage $oP)
{
$aAttSpecs = explode(',', PORTAL_TICKETS_SEARCH_CRITERIA);
// Remove the caller_id form the search criteria if the user is not a Portal Power User
// since the user is only allowed to see her/his own tickets
foreach($aAttSpecs as $idx => $sAttCode)
{
if (($sAttCode == 'caller_id') && !IsPowerUser())
{
unset($aAttSpecs[$idx]);
}
}
$aClasses = GetTicketClasses();
$sMainClass = reset($aClasses);
$oP->DisplaySearchForm($sMainClass, $aAttSpecs, array('operation' => 'show_closed'), 'search_', false /* => not closed */);
$oUserOrg = GetUserOrg();
$oP->add("
".Dict::S('Portal:ClosedRequests')."
\n");
$aClassToSet = array();
foreach (GetTicketClasses() as $sClass)
{
$oSearch = $oP->PostedParamsToFilter($sClass, $aAttSpecs, 'search_');
if(is_null($oSearch))
{
$oSearch = new DBObjectSearch($sClass);
}
$oSearch->AddCondition('org_id', $oUserOrg->GetKey());
$oSearch->AddCondition('status', 'closed');
$iUser = UserRights::GetContactId();
if ($iUser > 0 && !IsPowerUser())
{
$oSearch->AddCondition('caller_id', $iUser);
}
$aClassToSet[$sClass] = new CMDBObjectSet($oSearch);
}
DisplayRequestLists($oP, $aClassToSet);
}
/**
* Display an object - to be customized
* @param WebPage $oP The current web page
* @param Object $oObj Any kind of object
* @param Object $oUserOrg The organization of the logged in user
* @return void
*/
function DisplayObject($oP, $oObj, $oUserOrg)
{
if (in_array(get_class($oObj), GetTicketClasses()))
{
ShowDetailsRequest($oP, $oObj);
}
else
{
throw new Exception("The class ".get_class($oObj)." is not handled through the portal");
}
}
/**
* Displays the details of a request
* @param WebPage $oP The current web page
* @param Object $oObj The target object
* @return void
*/
function ShowDetailsRequest(WebPage $oP, $oObj)
{
$sClass = get_class($oObj);
$sLogAttCode = GetConstant($sClass, 'PUBLIC_LOG');
$sUserCommentAttCode = GetConstant($sClass, 'USER_COMMENT');
$bIsEscalateButton = false;
$bIsReopenButton = false;
$bIsCloseButton = false;
$bEditAttachments = false;
$aEditAtt = array(); // List of attributes editable in the main form
if (!MetaModel::DBIsReadOnly())
{
switch($oObj->GetState())
{
case 'new':
case 'assigned':
case 'frozen':
case 'pending':
$aEditAtt = array(
$sLogAttCode => '????'
);
$bEditAttachments = true;
// disabled - $bIsEscalateButton = true;
break;
case 'escalated_tto':
case 'escalated_ttr':
$aEditAtt = array(
$sLogAttCode => '????'
);
$bEditAttachments = true;
break;
case 'resolved':
$aEditAtt = array();
if (array_key_exists('ev_reopen', MetaModel::EnumStimuli($sClass)))
{
$bIsReopenButton = true;
MakeStimulusForm($oP, $oObj, 'ev_reopen', array($sLogAttCode));
}
$bIsCloseButton = true;
MakeStimulusForm($oP, $oObj, 'ev_close', array('user_satisfaction', $sUserCommentAttCode));
break;
case 'closed':
case 'closure_requested':
default:
break;
}
}
// REFACTORISER LA MISE EN FORME
$oP->add("