Ejemplo n.º 1
0
 /**
  * Designed as an action to be called when a stop watch threshold times out
  * or from within the framework	
  */
 public function ApplyStimulus($sStimulusCode, $bDoNotWrite = false)
 {
     $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($this));
     if (empty($sStateAttCode)) {
         return false;
     }
     MyHelpers::CheckKeyInArray('object lifecycle stimulus', $sStimulusCode, MetaModel::EnumStimuli(get_class($this)));
     $aStateTransitions = $this->EnumTransitions();
     if (!array_key_exists($sStimulusCode, $aStateTransitions)) {
         // This simulus has no effect in the current state... do nothing
         return;
     }
     $aTransitionDef = $aStateTransitions[$sStimulusCode];
     // Change the state before proceeding to the actions, this is necessary because an action might
     // trigger another stimuli (alternative: push the stimuli into a queue)
     $sPreviousState = $this->Get($sStateAttCode);
     $sNewState = $aTransitionDef['target_state'];
     $this->Set($sStateAttCode, $sNewState);
     // $aTransitionDef is an
     //    array('target_state'=>..., 'actions'=>array of handlers procs, 'user_restriction'=>TBD
     $bSuccess = true;
     foreach ($aTransitionDef['actions'] as $sActionHandler) {
         // std PHP spec
         $aActionCallSpec = array($this, $sActionHandler);
         if (!is_callable($aActionCallSpec)) {
             throw new CoreException("Unable to call action: " . get_class($this) . "::{$sActionHandler}");
             return;
         }
         $bRet = call_user_func($aActionCallSpec, $sStimulusCode);
         // if one call fails, the whole is considered as failed
         if (!$bRet) {
             $bSuccess = false;
         }
     }
     if ($bSuccess) {
         $sClass = get_class($this);
         // Stop watches
         foreach (MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef) {
             if ($oAttDef instanceof AttributeStopWatch) {
                 $oSW = $this->Get($sAttCode);
                 if (in_array($sNewState, $oAttDef->GetStates())) {
                     $oSW->Start($this, $oAttDef);
                 } else {
                     $oSW->Stop($this, $oAttDef);
                 }
                 $this->Set($sAttCode, $oSW);
             }
         }
         if (!$bDoNotWrite) {
             $this->DBWrite();
         }
         // Change state triggers...
         $sClassList = implode("', '", MetaModel::EnumParentClasses($sClass, ENUM_PARENT_CLASSES_ALL));
         $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT TriggerOnStateLeave AS t WHERE t.target_class IN ('{$sClassList}') AND t.state='{$sPreviousState}'"));
         while ($oTrigger = $oSet->Fetch()) {
             $oTrigger->DoActivate($this->ToArgs('this'));
         }
         $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT TriggerOnStateEnter AS t WHERE t.target_class IN ('{$sClassList}') AND t.state='{$sNewState}'"));
         while ($oTrigger = $oSet->Fetch()) {
             $oTrigger->DoActivate($this->ToArgs('this'));
         }
     }
     return $bSuccess;
 }
Ejemplo n.º 2
0
 public function AddCondition($sFilterCode, $value, $sOpCode = null)
 {
     MyHelpers::CheckKeyInArray('filter code in class: ' . $this->GetClass(), $sFilterCode, MetaModel::GetClassFilterDefs($this->GetClass()));
     $oFilterDef = MetaModel::GetClassFilterDef($this->GetClass(), $sFilterCode);
     $oField = new FieldExpression($sFilterCode, $this->GetClassAlias());
     if (empty($sOpCode)) {
         if ($sFilterCode == 'id') {
             $sOpCode = '=';
         } else {
             $oAttDef = MetaModel::GetAttributeDef($this->GetClass(), $sFilterCode);
             $oNewCondition = $oAttDef->GetSmartConditionExpression($value, $oField, $this->m_aParams);
             $this->AddConditionExpression($oNewCondition);
             return;
         }
     }
     MyHelpers::CheckKeyInArray('operator', $sOpCode, $oFilterDef->GetOperators());
     // Preserve backward compatibility - quick n'dirty way to change that API semantic
     //
     switch ($sOpCode) {
         case 'SameDay':
         case 'SameMonth':
         case 'SameYear':
         case 'Today':
         case '>|':
         case '<|':
         case '=|':
             throw new CoreException('Deprecated operator, please consider using OQL (SQL) expressions like "(TO_DAYS(NOW()) - TO_DAYS(x)) AS AgeDays"', array('operator' => $sOpCode));
             break;
         case "IN":
             if (!is_array($value)) {
                 $value = array($value);
             }
             $sListExpr = '(' . implode(', ', CMDBSource::Quote($value)) . ')';
             $sOQLCondition = $oField->Render() . " IN {$sListExpr}";
             break;
         case "NOTIN":
             if (!is_array($value)) {
                 $value = array($value);
             }
             $sListExpr = '(' . implode(', ', CMDBSource::Quote($value)) . ')';
             $sOQLCondition = $oField->Render() . " NOT IN {$sListExpr}";
             break;
         case 'Contains':
             $this->m_aParams[$sFilterCode] = "%{$value}%";
             $sOperator = 'LIKE';
             break;
         case 'Begins with':
             $this->m_aParams[$sFilterCode] = "{$value}%";
             $sOperator = 'LIKE';
             break;
         case 'Finishes with':
             $this->m_aParams[$sFilterCode] = "%{$value}";
             $sOperator = 'LIKE';
             break;
         default:
             $this->m_aParams[$sFilterCode] = $value;
             $sOperator = $sOpCode;
     }
     switch ($sOpCode) {
         case "IN":
         case "NOTIN":
             $oNewCondition = Expression::FromOQL($sOQLCondition);
             break;
         case 'Contains':
         case 'Begins with':
         case 'Finishes with':
         default:
             $oRightExpr = new VariableExpression($sFilterCode);
             $oNewCondition = new BinaryExpression($oField, $sOperator, $oRightExpr);
     }
     $this->AddConditionExpression($oNewCondition);
 }
Ejemplo n.º 3
0
 public static function Init_SetZListItems($sListCode, $aItems, $sTargetClass = null)
 {
     MyHelpers::CheckKeyInArray('list code', $sListCode, self::$m_aListInfos);
     if (!$sTargetClass) {
         $sTargetClass = self::GetCallersPHPClass("Init");
     }
     // Discard attributes that do not make sense
     // (missing classes in the current module combination, resulting in irrelevant ext key or link set)
     //
     self::Init_CheckZListItems($aItems, $sTargetClass);
     self::$m_aListData[$sTargetClass][$sListCode] = $aItems;
 }