public function testSaveModelFromPostSuccessfulSave() { //Unfreeze since the test model is not part of the standard schema. $freezeWhenComplete = false; if (RedBeanDatabase::isFrozen()) { RedBeanDatabase::unfreeze(); $freezeWhenComplete = true; } Yii::app()->user->userModel = User::getByUsername('super'); $savedSuccessfully = false; $modelToStringValue = null; $postData = array('member' => 'abc'); $model = new OwnedSecurableTestItem(); $this->assertFalse($model->hasErrors()); $controllerUtil = new ZurmoControllerUtil(); $model = $controllerUtil->saveModelFromPost($postData, $model, $savedSuccessfully, $modelToStringValue); $this->assertTrue($savedSuccessfully); $this->assertEquals('abc', $modelToStringValue); $this->assertFalse($model->hasErrors()); $this->assertTrue($model->id > 0); //Re-freeze if needed. if ($freezeWhenComplete) { RedBeanDatabase::freeze(); } }
public function testSaveModelFromPostSuccessfulSave() { Yii::app()->user->userModel = User::getByUsername('super'); $savedSuccessfully = false; $modelToStringValue = null; $postData = array('member' => 'abc'); $model = new OwnedSecurableTestItem(); $this->assertFalse($model->hasErrors()); $controllerUtil = new ZurmoControllerUtil(); $model = $controllerUtil->saveModelFromPost($postData, $model, $savedSuccessfully, $modelToStringValue); $this->assertTrue($savedSuccessfully); $this->assertEquals('abc', $modelToStringValue); $this->assertFalse($model->hasErrors()); $this->assertTrue($model->id > 0); }
/** * (non-PHPdoc) * @see ModelHasRelatedItemsZurmoControllerUtil::afterSetAttributesDuringSave() */ protected function afterSetAttributesDuringSave($model, $explicitReadWriteModelPermissions) { parent::afterSetAttributesDuringSave($model, $explicitReadWriteModelPermissions); if ($model instanceof Item) { FileModelUtil::resolveModelsHasManyFilesFromPost($model, 'files', 'filesIds'); } }
public function actionListDefault() { $data = ZurmoControllerUtil::resolveUserDefaultPermissionsForCurrentUser(); $resultClassName = Yii::app()->apiRequest->getResultClassName(); $result = new $resultClassName(ApiResponse::STATUS_SUCCESS, $data, null, null); Yii::app()->apiHelper->sendResponse($result); }
/** * Hook to alter $model or $data before we attempt to save it. * @param RedBeanModel $model * @param array $data */ protected function preAttemptToSaveModelFromDataHook(RedBeanModel $model, array &$data) { // if its a new model and if explicit permissions are not set in data if ($model->id < 0 && !isset($data['explicitReadWriteModelPermissions'])) { // get user's default permissions $defaultPermissions = ZurmoControllerUtil::resolveUserDefaultPermissionsForCurrentUser($model); // merge them with current data, put data last. $data = CMap::mergeArray($defaultPermissions, $data); } }
/** * @throws MissingRecipientsForEmailMessageException */ public function process() { $emailTemplate = EmailTemplate::getById((int) $this->emailMessageForm->emailTemplateId); $emailMessage = new EmailMessage(); $emailMessage->owner = $this->triggeredByUser; $emailMessage->subject = $this->resolveEmailTemplateSubjectForModelData($emailTemplate); $emailContent = new EmailMessageContent(); $emailContent->textContent = $this->resolveEmailTemplateTextContentForModelData($emailTemplate); $emailContent->htmlContent = $this->resolveEmailTemplateHtmlContentForModelData($emailTemplate); $emailMessage->content = $emailContent; $emailMessage->sender = $this->resolveSender(); $this->resolveRecipients($emailMessage); $this->resolveAttachments($emailMessage, $emailTemplate); if ($emailMessage->recipients->count() == 0) { throw new MissingRecipientsForEmailMessageException(); } $box = EmailBox::resolveAndGetByName(EmailBox::NOTIFICATIONS_NAME); $emailMessage->folder = EmailFolder::getByBoxAndType($box, EmailFolder::TYPE_DRAFT); Yii::app()->emailHelper->send($emailMessage); ZurmoControllerUtil::updatePermissionsWithDefaultForModelByUser($emailMessage, $this->triggeredByUser); }
/** * Create product from product template when user select a product * template while adding a product in product portlet view * @param string $relationModuleId * @param int $portletId * @param string $uniqueLayoutId * @param int $id * @param int $relationModelId * @param string $relationAttributeName * @param string $relationModelClassName */ public function actionCreateProductFromProductTemplate($relationModuleId, $portletId, $uniqueLayoutId, $id, $relationModelId, $relationAttributeName, $relationModelClassName = null, $redirect = true) { if ($relationModelClassName == null) { $relationModelClassName = Yii::app()->getModule($relationModuleId)->getPrimaryModelName(); } $productTemplate = static::getModelAndCatchNotFoundAndDisplayError('ProductTemplate', intval($id)); $product = new Product(); $product->name = $productTemplate->name; $product->description = $productTemplate->description; $product->quantity = 1; $product->productTemplate = $productTemplate; $sellPrice = new CurrencyValue(); $sellPrice->value = $productTemplate->sellPrice->value; $sellPrice->currency = $productTemplate->sellPrice->currency; $product->priceFrequency = $productTemplate->priceFrequency; $product->sellPrice = $sellPrice; $product->type = $productTemplate->type; $controllerUtil = static::getZurmoControllerUtil(); $controllerUtil->resolveStageDefaultValue($product); foreach ($productTemplate->productCategories as $productCategory) { $product->productCategories->add($productCategory); } $relatedModel = $relationModelClassName::getById((int) $relationModelId); $product->{$relationAttributeName} = $relatedModel; $this->addRelatedModelAccountToModel($product, $relatedModel); $product->save(); ZurmoControllerUtil::updatePermissionsWithDefaultForModelByCurrentUser($product); if ((bool) $redirect) { $isViewLocked = ZurmoDefaultViewUtil::getLockKeyForDetailsAndRelationsView('lockPortletsForDetailsAndRelationsView'); $redirectUrl = Yii::app()->createUrl('/' . $relationModuleId . '/default/details', array('id' => $relationModelId)); $this->redirect(array('/' . $relationModuleId . '/defaultPortlet/modalRefresh', 'portletId' => $portletId, 'uniqueLayoutId' => $uniqueLayoutId, 'redirectUrl' => $redirectUrl, 'portletParams' => array('relationModuleId' => $relationModuleId, 'relationModelId' => $relationModelId), 'portletsAreRemovable' => !$isViewLocked)); } }
/** * @depends testCreateAndGetProductTemplateById */ public function testUpdateProductTemplateFromForm() { $user = User::getByUsername('Steven'); $currencies = Currency::getAll(); $currency = $currencies[0]; $currency->save(); $postData = array('name' => 'Editable Sell Price', 'description' => 'Template Description', 'status' => ProductTemplate::STATUS_ACTIVE, 'type' => ProductTemplate::TYPE_PRODUCT, 'sellPriceFormula' => array('type' => 1, 'discountOrMarkupPercentage' => 0), 'priceFrequency' => ProductTemplate::PRICE_FREQUENCY_ANNUALLY, 'cost' => array('currency' => array('id' => $currency->id), 'value' => 240), 'listPrice' => array('currency' => array('id' => $currency->id), 'value' => 200), 'sellPrice' => array('currency' => array('id' => $currency->id), 'value' => 170)); $controllerUtil = new ZurmoControllerUtil(); $productTemplate = new ProductTemplate(); $savedSucessfully = false; $modelToStringValue = null; $productTemplate = $controllerUtil->saveModelFromPost($postData, $productTemplate, $savedSucessfully, $modelToStringValue); $this->assertTrue($savedSucessfully); $id = $productTemplate->id; unset($productTemplate); $productTemplate = ProductTemplate::getById($id); $this->assertEquals('Editable Sell Price', $productTemplate->name); $this->assertEquals(170, $productTemplate->sellPrice->value); $this->assertEquals(240, $productTemplate->cost->value); $this->assertEquals(200, $productTemplate->listPrice->value); }
/** * Override to handle UserStatus processing. * @see ZurmoBaseController::attemptToSaveModelFromPost() */ protected function attemptToSaveModelFromPost($model, $redirectUrlParams = null, $redirect = true, $returnOnValidate = false) { assert('$model instanceof User || $model instanceof UserPasswordForm || $model instanceof UserAvatarForm'); assert('$redirectUrlParams == null || is_array($redirectUrlParams) || is_string($redirectUrlParams)'); $postVariableName = get_class($model); if (isset($_POST[$postVariableName])) { $postData = $_POST[$postVariableName]; if (isset($_POST[$postVariableName]['userStatus'])) { $userStatus = UserStatusUtil::makeByPostData($_POST[$postVariableName]); $sanitizedPostdata = UserStatusUtil::removeIfExistsFromPostData($postData); } else { $userStatus = null; $sanitizedPostdata = $postData; } $savedSucessfully = false; $modelToStringValue = null; $oldUsername = $model->username; $controllerUtil = new ZurmoControllerUtil(); $model = $controllerUtil->saveModelFromPost($sanitizedPostdata, $model, $savedSucessfully, $modelToStringValue); if ($savedSucessfully) { if ($userStatus != null) { if ($model instanceof UserPasswordForm || $model instanceof UserAvatarForm) { UserStatusUtil::resolveUserStatus($model->getModel(), $userStatus); } else { UserStatusUtil::resolveUserStatus($model, $userStatus); } } if ($model->id == Yii::app()->user->userModel->id && $model->username != $oldUsername) { //If the logged in user changes their username, a logout must occur to properly to properly //restart the session. Yii::app()->getSession()->destroy(); $this->redirect(Yii::app()->homeUrl); } $this->actionAfterSuccessfulModelSave($model, $modelToStringValue, $redirectUrlParams); } } return $model; }
public function actionCreateEmailMessage($toAddress = null, $relatedId = null, $relatedModelClassName = null) { $postData = PostUtil::getData(); $getData = GetUtil::getData(); $personOrAccount = self::resolvePersonOrAccountFromGet($relatedId, $relatedModelClassName); $emailMessage = new EmailMessage(); $emailMessageForm = new CreateEmailMessageForm($emailMessage); $emailMessageForm->setScenario('createNonDraft'); $postVariableName = get_class($emailMessageForm); if ($toAddress == null && $personOrAccount != null && $personOrAccount->primaryEmail->emailAddress != null) { $toAddress = $personOrAccount->primaryEmail->emailAddress; } if (isset($postData[$postVariableName])) { EmailMessageUtil::resolveEmailMessageFromPostData($postData, $emailMessageForm, Yii::app()->user->userModel); $this->actionValidateCreateEmailMessage($postData, $emailMessageForm); $this->attemptToSaveModelFromPost($emailMessageForm, null, false); ZurmoControllerUtil::updatePermissionsWithDefaultForModelByCurrentUser($emailMessageForm->getModel()); Yii::app()->jobQueue->add('ProcessOutboundEmail'); } else { EmailMessageUtil::resolveSignatureToEmailMessage($emailMessage, Yii::app()->user->userModel); EmailMessageUtil::resolvePersonOrAccountToEmailMessage($emailMessage, Yii::app()->user->userModel, $toAddress, $relatedId, $relatedModelClassName); $createEmailMessageModalEditView = new CreateEmailMessageModalEditView($this->getId(), $this->getModule()->getId(), $emailMessageForm); $view = new ModalView($this, $createEmailMessageModalEditView); Yii::app()->getClientScript()->setToAjaxMode(); echo $view->render(); } }
/** * Instead of saving from post, we are saving from the API data. * @see attemptToSaveModelFromPost */ protected function attemptToSaveModelFromData($model, $data, $redirectUrlParams = null, $redirect = true) { assert('is_array($data)'); assert('$redirectUrlParams == null || is_array($redirectUrlParams) || is_string($redirectUrlParams)'); $savedSucessfully = false; $modelToStringValue = null; if (isset($data)) { $this->preAttemptToSaveModelFromDataHook($model, $data); $controllerUtil = new ZurmoControllerUtil(); $model = $controllerUtil->saveModelFromSanitizedData($data, $model, $savedSucessfully, $modelToStringValue, false); } if ($savedSucessfully && $redirect) { $this->actionAfterSuccessfulModelSave($model, $modelToStringValue, $redirectUrlParams); } return $model; }