protected function resolveIdAndNameByModel(RedBeanModel $model) { $existingPeople = null; if (!isset($this->modelDerivationPathToItemFromContact)) { $this->modelDerivationPathToItemFromContact = RuntimeUtil::getModelDerivationPathToItem('Contact'); } if (!isset($this->modelDerivationPathToItemFromUser)) { $this->modelDerivationPathToItemFromUser = RuntimeUtil::getModelDerivationPathToItem('User'); } try { $contact = $model->person->castDown(array($this->modelDerivationPathToItemFromContact)); if (get_class($contact) == 'Contact') { $existingPeople = array('id' => $contact->getClassId('Item'), $this->getWidgetPropertyToSearch() => strval($contact)); } else { throw new NotFoundException(); } } catch (NotFoundException $e) { try { $user = $model->person->castDown(array($this->modelDerivationPathToItemFromUser)); //Owner is always added first. if (get_class($user) == 'User' && $user->id != $this->model->owner->id) { $readOnly = false; $existingPeople = array('id' => $user->getClassId('Item'), $this->getWidgetPropertyToSearch() => strval($user), 'readonly' => $readOnly); } } catch (NotFoundException $e) { //This means the item is not a recognized or expected supported model. throw new NotSupportedException(); } } return $existingPeople; }
public static function resolveAsUserOrContact(Item $item) { foreach (array('Contact', 'User') as $modelClassName) { try { $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem($modelClassName); $model = $item->castDown(array($modelDerivationPathToItem)); $moduleClassName = $modelClassName::getModuleClassName(); $linkRoute = '/' . $moduleClassName::getDirectoryName() . '/default/details'; return ActionSecurityUtil::resolveLinkToModelForCurrentUser(strval($model), $model, $moduleClassName, $linkRoute); } catch (NotFoundException $e) { } } throw new NotSupportedException(); }
protected function resolveIdAndNameByModel(RedBeanModel $model) { $existingContact = null; if (!isset($this->modelDerivationPathToItemFromContact)) { $this->modelDerivationPathToItemFromContact = RuntimeUtil::getModelDerivationPathToItem('Contact'); } try { $contact = $model->castDown(array($this->modelDerivationPathToItemFromContact)); if (get_class($contact) == 'Contact') { $existingContact = array('id' => Meeting::CONTACT_ATTENDEE_PREFIX . $contact->id, 'name' => self::renderHtmlContentLabelFromContactAndKeyword($contact, null)); } } catch (NotFoundException $e) { //do nothing } return $existingContact; }
/** * @param Item $item * @param $dateTime * @param $modelClassName * @throws FailedToSaveModelException */ public static function resolveItemToModelAndPopulateLatestActivityDateTime(Item $item, $dateTime, $modelClassName) { assert('is_string($dateTime)'); assert('is_string($modelClassName)'); $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem($modelClassName); try { $castedDownModel = $item->castDown(array($modelDerivationPathToItem)); if (DateTimeUtil::isDateTimeStringNull($castedDownModel->latestActivityDateTime) || $dateTime > $castedDownModel->latestActivityDateTime) { $castedDownModel->setLatestActivityDateTime($dateTime); $saved = $castedDownModel->save(); if (!$saved) { throw new FailedToSaveModelException(); } } } catch (NotFoundException $e) { //do nothing } catch (AccessDeniedSecurityException $e) { //do nothing, since the current user cannot update the related model. Fail silently. } }
protected static function renderActivityItemsContentsExcludingContacts(Meeting $meeting) { $activityItemsModelClassNamesData = ActivitiesUtil::getActivityItemsModelClassNamesDataExcludingContacts(); $content = null; foreach ($activityItemsModelClassNamesData as $relationModelClassName) { $activityItemForm = null; //ASSUMES ONLY A SINGLE ATTACHED ACTIVITYITEM PER RELATION TYPE. foreach ($meeting->activityItems as $item) { try { $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem($relationModelClassName); $castedDownModel = $item->castDown(array($modelDerivationPathToItem)); if ($content != null) { $content .= ', '; } $content .= strval($castedDownModel); break; } catch (NotFoundException $e) { } } } if ($content != null) { $content .= '<br/>'; } return $content; }
public static function castDownActivityItem(Item $activityItem) { $relationModelClassNames = ActivitiesUtil::getActivityItemsModelClassNames(); foreach ($relationModelClassNames as $relationModelClassName) { try { $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem($relationModelClassName); return $activityItem->castDown(array($modelDerivationPathToItem)); } catch (NotFoundException $e) { } } }
protected static function castDownItem(Item $item) { foreach (array('Contact', 'User') as $modelClassName) { try { $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem($modelClassName); return $item->castDown(array($modelDerivationPathToItem)); } catch (NotFoundException $e) { } } throw new NotSupportedException(); }
/** * Should not throw exception, should cast down ok * @depends testWebsiteCanBeSavedWithoutUrlScheme */ public function testCastDown() { $user = User::getByUsername('steven'); $account = new Account(); $account->owner = $user; $account->name = 'Account'; $this->assertTrue($account->save()); $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem('Account'); $itemId = $account->getClassId('Item'); $item = Item::getById($itemId); $castedDownModel = $item->castDown(array($modelDerivationPathToItem)); $this->assertEquals('Account', get_class($castedDownModel)); }
protected static function resolveRelatedModelsForInferredRelations($attribute, $model, $relation, $resolvedAttributeModelClassName) { if (ModelRelationsAndAttributesToWorkflowAdapter::isAttributeInferred($attribute)) { $relatedModels = array(); foreach ($model->{$relation} as $item) { if (get_class($item) == $resolvedAttributeModelClassName) { $relatedModels[] = $item; } else { try { $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem($resolvedAttributeModelClassName); $relatedModels[] = $item->castDown(array($modelDerivationPathToItem)); } catch (NotFoundException $e) { } } } return $relatedModels; } else { return $model->{$relation}; } }
protected static function resolveMergeTagToStandardOrRelatedAttribute($attributeAccessorString, $model, $language, $timeQualifier, $errorOnFirstMissing, $params) { $attributeName = strtok($attributeAccessorString, '->'); if (SpecialMergeTagsAdapter::isSpecialMergeTag($attributeName, $timeQualifier)) { return SpecialMergeTagsAdapter::resolve($attributeName, $model, $errorOnFirstMissing, $params); } else { if (!isset($model)) { return static::PROPERTY_NOT_FOUND; } elseif (!method_exists($model, 'isAttribute') || !$model->isAttribute($attributeName)) { if ($model instanceof Activity) { $metadata = $model::getMetadata(); $activityItemsModelClassNamesData = $metadata['Activity']['activityItemsModelClassNames']; foreach ($model->activityItems as $activityItem) { if (ucfirst($attributeName) == get_class($activityItem)) { $attributeAccessorString = str_replace($attributeName . '->', '', $attributeAccessorString); return static::resolveMergeTagToStandardOrRelatedAttribute($attributeAccessorString, $activityItem, $language, $timeQualifier, $errorOnFirstMissing, $params); } if (get_class($activityItem) == 'Item' && array_search(ucfirst($attributeName), $activityItemsModelClassNamesData) !== false) { try { $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem(ucfirst($attributeName)); $castedDownModel = $activityItem->castDown(array($modelDerivationPathToItem)); if (ucfirst($attributeName) == get_class($castedDownModel)) { $attributeAccessorString = str_replace($attributeName . '->', '', $attributeAccessorString); return static::resolveMergeTagToStandardOrRelatedAttribute($attributeAccessorString, $castedDownModel, $language, $timeQualifier, $errorOnFirstMissing, $params); } } catch (NotFoundException $e) { //Do nothing } } unset($activityItemsModelClassNamesData[get_class($activityItem)]); } foreach ($activityItemsModelClassNamesData as $relationModelClassName) { if (ucfirst($attributeName) == $relationModelClassName) { $model = new $relationModelClassName(); $attributeAccessorString = str_replace($attributeName . '->', '', $attributeAccessorString); return static::resolveMergeTagToStandardOrRelatedAttribute($attributeAccessorString, $model, $language, $timeQualifier, $errorOnFirstMissing, $params); } } } return static::PROPERTY_NOT_FOUND; } elseif ($model->{$attributeName} instanceof CurrencyValue) { $model = $model->{$attributeName}; if ($attributeName === $attributeAccessorString) { $attributeAccessorString = null; } else { $attributeAccessorString = str_replace($attributeName . '->', '', $attributeAccessorString); } if (empty($attributeAccessorString)) { // If a user specific a relation merge tag but not a property, we assume he meant "value" property. $currencyValueModel = $model; $value = static::getAttributeValue($currencyValueModel, 'value', $timeQualifier); return CLocale::getInstance($language)->getCurrencySymbol($currencyValueModel->currency->code) . $value; // We can't use code below because it converts integer values in flat and also add slashes to '.' in float numbers //return Yii::app()->numberFormatter->formatCurrency($value, // $currencyValueModel->currency->code); } return static::resolveMergeTagToStandardOrRelatedAttribute($attributeAccessorString, $model, $language, $timeQualifier, $errorOnFirstMissing, $params); } elseif ($model->{$attributeName} instanceof CustomField) { $value = static::getAttributeValue($model->{$attributeName}, 'value', $timeQualifier); // TODO: @Shoaibi/@Jason: Low: need to apply localizations(Date/time/currency formats, ...) here besides translation if ($value) { $value = Zurmo::t($model::getModuleClassName(), $value, array(), null, $language); } return $value; } elseif ($model->isRelation($attributeName)) { $model = $model->{$attributeName}; if ($attributeName === $attributeAccessorString) { $attributeAccessorString = null; } else { $attributeAccessorString = str_replace($attributeName . '->', '', $attributeAccessorString); } if (empty($attributeAccessorString)) { // If a user specific a relation merge tag but not a property, we assume he meant "value" property. if (empty($timeQualifier)) { return strval($model); } else { return static::PROPERTY_NOT_FOUND; } } if ($model instanceof RedBeanModels) { $modelClassName = $model->getModelClassName(); if ($attributeAccessorString == lcfirst($modelClassName)) { $values = array(); foreach ($model as $relatedModel) { $values[] = strval($relatedModel); } return ArrayUtil::stringify($values); } } return static::resolveMergeTagToStandardOrRelatedAttribute($attributeAccessorString, $model, $language, $timeQualifier, $errorOnFirstMissing, $params); } else { $attributeType = ModelAttributeToMixedTypeUtil::getType($model, $attributeName); //We don't have any accessor operator after the attributeName e.g. its the last in list if ($attributeName === $attributeAccessorString) { $content = static::getAttributeValue($model, $attributeName, $timeQualifier); if ($attributeType == 'DateTime') { $content .= ' GMT'; } return $content; } else { return static::PROPERTY_NOT_FOUND; } } } }
protected function renderNonEditableElementsForRelationsByRelationsData($relationModelClassNames) { $content = null; $formClassName = static::getRelatedItemFormClassName(); foreach ($relationModelClassNames as $relationModelClassName) { $relatedItemForm = null; //ASSUMES ONLY A SINGLE ATTACHED RELATEDITEM PER RELATION TYPE. foreach ($this->getRelatedItemsFromModel() as $item) { try { $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem($relationModelClassName); $castedDownModel = $item->castDown(array($modelDerivationPathToItem)); $relatedItemForm = new $formClassName($castedDownModel); break; } catch (NotFoundException $e) { //do nothing } } if ($relatedItemForm != null) { $canAccess = true; $modelElementType = RelatedItemRelationToModelElementUtil::resolveModelElementTypeByActionSecurity($relationModelClassName, Yii::app()->user->userModel, $canAccess); if ($canAccess) { $elementInformation = array('attributeName' => $relationModelClassName, 'type' => $modelElementType); FormLayoutSecurityUtil::resolveElementForNonEditableRender($relatedItemForm, $elementInformation, Yii::app()->user->userModel); if ($elementInformation['attributeName'] != null) { $elementclassname = $elementInformation['type'] . 'Element'; $element = new $elementclassname($relatedItemForm, $elementInformation['attributeName'], $this->form, array_slice($elementInformation, 2)); assert('$element instanceof ModelElement'); $element->nonEditableTemplate = $this->getRelatedItemNonEditableTemplate(); $content .= $element->render(); } } } } return $content; }
private function checkIfUserFoundInSubscribersList($task, $compareId) { $isUserFound = false; $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem('User'); foreach ($task->notificationSubscribers as $subscriber) { $user = $subscriber->person->castDown(array($modelDerivationPathToItem)); if ($user->id == $compareId) { $isUserFound = true; } } return $isUserFound; }
protected function getExistingContactRelationsIdsAndLabels() { $existingContacts = array(); $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem('Contact'); foreach ($this->model->activityItems as $item) { try { $contact = $item->castDown(array($modelDerivationPathToItem)); if (get_class($contact) == 'Contact') { $existingContacts[] = array('id' => $contact->id, 'name' => self::renderHtmlContentLabelFromContactAndKeyword($contact, null)); } } catch (NotFoundException $e) { //do nothing } } return $existingContacts; }
protected function getExistingPeopleRelationsIdsAndLabels() { $existingPeople = array(array('id' => $this->model->owner->getClassId('Item'), 'name' => strval($this->model->owner), 'readonly' => true)); $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem('Contact'); foreach ($this->model->conversationParticipants as $participant) { try { $contact = $participant->person->castDown(array($modelDerivationPathToItem)); if (get_class($contact) == 'Contact') { $existingPeople[] = array('id' => $contact->getClassId('Item'), 'name' => strval($contact)); } else { throw new NotFoundException(); } } catch (NotFoundException $e) { $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem('User'); try { $user = $participant->person->castDown(array($modelDerivationPathToItem)); //Owner is always added first. if (get_class($user) == 'User' && $user->id != $this->model->owner->id) { $readOnly = false; $existingPeople[] = array('id' => $user->getClassId('Item'), 'name' => strval($user), 'readonly' => $readOnly); } } catch (NotFoundException $e) { //This means the item is not a recognized or expected supported model. throw new NotSupportedException(); } } } return $existingPeople; }
/** * @param $modelClassName * @param $castedDownModelClassName * @return array */ protected function resolveModelDerivationPathToItemForCastingDown($modelClassName, $castedDownModelClassName) { assert('is_string($modelClassName)'); assert('is_string($castedDownModelClassName)'); $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem($castedDownModelClassName); if ($modelClassName == 'Item') { return $modelDerivationPathToItem; } foreach ($modelDerivationPathToItem as $key => $modelClassNameToCastDown) { unset($modelDerivationPathToItem[$key]); if ($modelClassName == $modelClassNameToCastDown) { break; } } return $modelDerivationPathToItem; }
/** * @param ByTimeWorkflowInQueue $byTimeWorkflowInQueue * @return RedBeanModel */ protected static function resolveModel(ByTimeWorkflowInQueue $byTimeWorkflowInQueue) { $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem($byTimeWorkflowInQueue->modelClassName); return $byTimeWorkflowInQueue->modelItem->castDown(array($modelDerivationPathToItem)); }
/** * @param Item $workflowInQueueModel * @return An|RedBeanModel */ protected static function resolveModel(Item $workflowInQueueModel) { assert('$workflowInQueueModel instanceof ByTimeWorkflowInQueue || $workflowInQueueModel instanceof WorkflowMessageInQueue'); $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem($workflowInQueueModel->modelClassName); return $workflowInQueueModel->modelItem->castDown(array($modelDerivationPathToItem)); }
/** * @param RedBeanModel $inQueueModel * @return An|RedBeanModel */ protected static function resolveModel(RedBeanModel $inQueueModel) { $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem($inQueueModel->modelClassName); return $inQueueModel->modelItem->castDown(array($modelDerivationPathToItem)); }
/** * @covers resolveDetailSubscribeUrl */ public function testResolveDetailSubscriptionLink() { $tasks = Task::getByName('MyTest'); $task = $tasks[0]; if ($task->doNotificationSubscribersContainPerson(Yii::app()->user->userModel) === false) { $notificationSubscriber = new NotificationSubscriber(); $notificationSubscriber->person = Yii::app()->user->userModel; $task->notificationSubscribers->add($notificationSubscriber); $task->save(); } $link = TasksUtil::getDetailSubscriptionLink($task, 0); $this->assertContains('detail-unsubscribe-task-link', $link); $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem('User'); foreach ($task->notificationSubscribers as $index => $notificationSubscriber) { $user = $notificationSubscriber->person->castDown(array($modelDerivationPathToItem)); if ($user->id == Yii::app()->user->userModel->id) { $task->notificationSubscribers->remove($notificationSubscriber); } } $task->save(); $link = TasksUtil::getDetailSubscriptionLink($task, 0); $this->assertContains('detail-subscribe-task-link', $link); }
/** * Update all account read permissions items when permissions for item is removed from user */ public static function securableItemLostPermissionsForUser(SecurableItem $securableItem) { if ($securableItem instanceof Account) { $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem('Account'); $account = $securableItem->castDown(array($modelDerivationPathToItem)); self::updateAccountReadSubscriptionTableBasedOnBuildTable($account->id); } }
protected static function renderActivityItemsContentsExcludingContacts(Meeting $meeting) { $activityItemsModelClassNamesData = ActivitiesUtil::getActivityItemsModelClassNamesDataExcludingContacts(); $content = null; foreach ($activityItemsModelClassNamesData as $relationModelClassName) { $activityItemForm = null; //ASSUMES ONLY A SINGLE ATTACHED ACTIVITYITEM PER RELATION TYPE. foreach ($meeting->activityItems as $item) { try { $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem($relationModelClassName); $castedDownModel = $item->castDown(array($modelDerivationPathToItem)); if ($content != null) { $content .= '<br/> '; } $params = array('label' => strval($castedDownModel), 'redirectUrl' => null, 'wrapLabel' => false); $moduleClassName = $castedDownModel->getModuleClassName(); $moduleId = $moduleClassName::getDirectoryName(); $element = new DetailsLinkActionElement('default', $moduleId, $castedDownModel->id, $params); //Render icon $content .= '<i class="icon-' . strtolower(get_class($castedDownModel)) . '"></i> '; $content .= $element->render(); break; } catch (NotFoundException $e) { } } } if ($content != null) { $content .= '<br/>'; } return $content; }
/** * @param WorkflowMessageInQueue $workflowMessageInQueue * @return RedBeanModel */ protected function resolveModel(WorkflowMessageInQueue $workflowMessageInQueue) { $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem($workflowMessageInQueue->modelClassName); return $workflowMessageInQueue->modelItem->castDown(array($modelDerivationPathToItem)); }
/** * @covers beforeSave::resolveAndSetDefaultSubscribers */ public function testAddSubscriberToTask() { Yii::app()->user->userModel = User::getByUsername('super'); $user = User::getByUsername('billy'); $task = new Task(); $task->name = 'MyTest'; $task->owner = $user; $nowStamp = DateTimeUtil::convertTimestampToDbFormatDateTime(time()); $this->assertTrue($task->save()); $this->assertEquals($user, $task->owner); //There would be two here as default subscribers are added $this->assertEquals(2, count($task->notificationSubscribers)); $user = Yii::app()->user->userModel; $notificationSubscriber = new NotificationSubscriber(); $notificationSubscriber->person = $user; $notificationSubscriber->hasReadLatest = false; $task->notificationSubscribers->add($notificationSubscriber); $this->assertTrue($task->save()); $task = Task::getById($task->id); $subscriber = $task->notificationSubscribers->offsetGet(0); $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem('User'); $subscribedUser = $subscriber->person->castDown(array($modelDerivationPathToItem)); $this->assertEquals($user, $subscribedUser); $this->assertEquals(3, count($task->notificationSubscribers)); }
/** * @param array $relationModelClassNames * @param RedBeanModel $model * @return string */ protected static function getFirstActivityItemStringContent($relationModelClassNames, RedBeanModel $model) { assert('is_array($relationModelClassNames)'); foreach ($relationModelClassNames as $relationModelClassName) { //ASSUMES ONLY A SINGLE ATTACHED ACTIVITYITEM PER RELATION TYPE. foreach ($model->activityItems as $item) { try { $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem($relationModelClassName); $castedDownModel = $item->castDown(array($modelDerivationPathToItem)); return strval($castedDownModel); } catch (NotFoundException $e) { //do nothing } } } }
/** * Should not throw exception, should cast down ok */ public function testCastDown() { Yii::app()->user->userModel = User::getByUsername('super'); $startStamp = DateTimeUtil::convertTimestampToDbFormatDateTime(time()); $meeting = new Meeting(); $meeting->name = 'aTest'; $meeting->startDateTime = $startStamp; $this->assertTrue($meeting->save()); $itemId = $meeting->getClassId('Item'); $item = Item::getById($itemId); $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem('Meeting'); $castedDownModel = $item->castDown(array($modelDerivationPathToItem)); $this->assertEquals('Meeting', get_class($castedDownModel)); }
/** * @param string $relation * @param $model * @return array of models */ public static function getInferredModelsByAtrributeAndModel($relation, $model) { assert('is_string($relation)'); $realAttributeName = ModelRelationsAndAttributesToWorkflowAdapter::resolveRealAttributeName($relation); $relationModelClassName = ModelRelationsAndAttributesToWorkflowAdapter::getInferredRelationModelClassName($relation); $relatedModels = array(); foreach ($model->{$realAttributeName} as $item) { try { $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem($relationModelClassName); $relatedModels[] = $item->castDown(array($modelDerivationPathToItem)); } catch (NotFoundException $e) { } } return $relatedModels; }
/** * Get meeting attendees from activity items. It returns Contact, Opportunity or Account ids * @param $meeting * @return array * @throws Exception */ protected static function getAttendeesFromActivityItems($meeting) { $data = array(); $activityClassNames = ActivitiesUtil::getActivityItemsModelClassNames(); try { foreach ($activityClassNames as $activityClassName) { foreach ($meeting->activityItems as $activityItem) { try { $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem($activityClassName); $castedDownModel = $activityItem->castDown(array($modelDerivationPathToItem)); $item = array(); $item['id'] = $castedDownModel->id; if ($castedDownModel instanceof Contact) { if ($castedDownModel->primaryEmail->emailAddress != null) { $item['email'] = $castedDownModel->primaryEmail->emailAddress; } $item['firstName'] = $castedDownModel->firstName; $item['lastName'] = $castedDownModel->lastName; } elseif ($castedDownModel instanceof Account || $castedDownModel instanceof Opportunity) { $item['name'] = $castedDownModel->name; } $data[$activityClassName][] = $item; } catch (NotFoundException $e) { } } } } catch (Exception $e) { $message = $e->getMessage(); throw new Exception($message); } return $data; }