/**
  * 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));
     }
 }
 /**
  * Resolves portlet configurable parameters
  * @param boolean $portletsAreMovable
  * @param boolean $portletsAreRemovable
  */
 protected function resolvePortletConfigurableParams(&$portletsAreMovable, &$portletsAreRemovable)
 {
     assert('is_bool($portletsAreMovable)');
     assert('is_bool($portletsAreRemovable)');
     $getData = GetUtil::getData();
     if (isset($getData['lockPortlets'])) {
         $lockPortlets = (bool) $getData['lockPortlets'];
         if ($lockPortlets) {
             ZurmoDefaultViewUtil::setLockKeyForDetailsAndRelationsView('lockPortletsForDetailsAndRelationsView', true);
         } else {
             ZurmoDefaultViewUtil::setLockKeyForDetailsAndRelationsView('lockPortletsForDetailsAndRelationsView', false);
         }
     }
     $isViewLocked = ZurmoDefaultViewUtil::getLockKeyForDetailsAndRelationsView('lockPortletsForDetailsAndRelationsView');
     //Default case for the first time
     if ($isViewLocked === null) {
         ZurmoDefaultViewUtil::setLockKeyForDetailsAndRelationsView('lockPortletsForDetailsAndRelationsView', true);
         $isViewLocked = true;
     }
     if ($isViewLocked == true) {
         $portletsAreRemovable = false;
         $portletsAreMovable = false;
     }
 }
 /**
  * In a detail view, after you hit select from a sub view a modal listview will appear. If you select a row
  * in that view, then this action is called. This action will relate the selected model to the detail view model.
  * Then it will redirect to a portlet action that refreshes the portlet. Some parameters are passed to that
  * redirect that ensure continuity on futher actions that rely on existing $_GET information.
  * @param string $modelId
  * @param string $portletId
  * @param string $uniqueLayoutId
  * @param string $relationAttributeName
  * @param string $relationModelId
  * @param string $relationModuleId
  * @param null|string $relationModelClassName
  */
 public function actionSelectFromRelatedListSave($modelId, $portletId, $uniqueLayoutId, $relationAttributeName, $relationModelId, $relationModuleId, $relationModelClassName = null)
 {
     if ($relationModelClassName == null) {
         $relationModelClassName = Yii::app()->getModule($relationModuleId)->getPrimaryModelName();
     }
     $relationModel = $relationModelClassName::getById((int) $relationModelId);
     $modelClassName = $this->getModule()->getPrimaryModelName();
     $model = $modelClassName::getById((int) $modelId);
     $redirectUrl = $this->createUrl('/' . $relationModuleId . '/default/details', array('id' => $relationModelId));
     if (!$model->{$relationAttributeName}->contains($relationModel)) {
         $model->{$relationAttributeName}->add($relationModel);
         if (!$model->save()) {
             $this->processSelectFromRelatedListSaveFails($model);
         }
     } else {
         $this->processSelectFromRelatedListSaveAlreadyConnected($model);
     }
     $isViewLocked = ZurmoDefaultViewUtil::getLockKeyForDetailsAndRelationsView('lockPortletsForDetailsAndRelationsView');
     $this->redirect(array('/' . $relationModuleId . '/defaultPortlet/modalRefresh', 'id' => $relationModelId, 'portletId' => $portletId, 'uniqueLayoutId' => $uniqueLayoutId, 'redirectUrl' => $redirectUrl, 'portletParams' => array('relationModuleId' => $relationModuleId, 'relationModelId' => $relationModelId), 'portletsAreRemovable' => !$isViewLocked));
 }
 public function actionAddOpportunityRelation($relationModuleId, $portletId, $uniqueLayoutId, $id, $relationModelId, $relationAttributeName, $redirect)
 {
     $opportunityProduct = OpportunityProduct::GetById(intval($id));
     $opportunity = Opportunity::GetById(intval($relationModelId));
     if ($opportunityProduct->opportunity != $opportunity) {
         $opportunityProduct->opportunity = $opportunity;
         $opportunityProduct->save();
     }
     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));
     }
 }