Jump to content


Photo

Button to remove Opportunities from Contacts


  • Please log in to reply
9 replies to this topic

#1 k1000o

k1000o

    Advanced Member

  • Members
  • PipPipPip
  • 112 posts
  • LocationMadrid

Posted 25 May 2012 - 02:25 PM

Hello

I am working in a button that allows to remove the association between Opportunities and Contacts.

The idea is to make the following
1- Create a new Function in the Opportunities Adapter that receives as a parameter The id of the Contact and the Id of the Opportunity that will remove the relation.
2- Modify OpportunitiesRelatedListView to add a new type of column that will present the trash button
3- In the Opportunities Module add a new element like this
class ColorDeleteElement extends Element implements DerivedElementInterface
4- Create a new adapter for this column that will will call via ajax the method in the controller. Something like this
class OpportunityDeleteListViewColumnAdapter extends ListViewColumnAdapter
.......
protected function resolveToRenderDeleteLink($modelId)
		{
			$checkboxId = 'closeTask' . $modelId;
			// Begin Not Coding Standard
			$content	= 'CHtml::link("Delete<span class=\'icon\'></span>", "#",
									   array("class" => "remove",
											 "onclick" => "deleteOpportunityFromAccountListView(this, \'' . XXXXX . '\')"))';

The problem is that in the location of the XXXXX I can retrieve easily the ID of the Opportunity with $this->id, but I do not know how to retrieve the id of the Contact.

Any idea?

Thanks,

Camilo

#2 Jason

Jason

    Administrator

  • Administrators
  • 717 posts
  • LocationChicago

Posted 26 May 2012 - 02:20 AM

Where you declare 'type' => 'OpportunityDelete' in the metadata of OpportunitiesRelatedListView, you should be able to pass in another param like 'myParam' => 'eval:$this->params['relationModel']->id'

Then in your adapter that should be available as $this->params['myParam']

Hope this helps.

Then once this works, you would actually declare the OpportunityDelete as a column not in OpportunitiesRelatedListView but in OpportunitiesForContactRelatedListView and merge it with the parent metadata. And you would want to do this not in the file system but via the customization hook to load it into the db.

Jason Green
Zurmo Team
Need support? Upgrade now


#3 k1000o

k1000o

    Advanced Member

  • Members
  • PipPipPip
  • 112 posts
  • LocationMadrid

Posted 26 May 2012 - 08:12 AM

Hi

I defined in OpportunitiesRelatedListView the following field
'elements' => array(
array('attributeName'		 => 'null',
'type'				  => 'DeleteRelated',
'relationModelId'	   => 'eval:$this->params["relationModel"]->id',
),
Then in the adapter I try to retrieve it as follows
$relationModelId = $this->params['relationModelId'];
and I receive the full string with 'eval:$this->params["relationModel"]->id' but not the actual value of relationModel->id

What I am doing wrong?

#4 Jason

Jason

    Administrator

  • Administrators
  • 717 posts
  • LocationChicago

Posted 26 May 2012 - 01:40 PM

Hi

I defined in OpportunitiesRelatedListView the following field

'elements' => array(
array('attributeName'		 => 'null',
'type'				  => 'DeleteRelated',
'relationModelId'	   => 'eval:$this->params["relationModel"]->id',
),
Then in the adapter I try to retrieve it as follows
$relationModelId = $this->params['relationModelId'];
and I receive the full string with 'eval:$this->params["relationModel"]->id' but not the actual value of relationModel->id

What I am doing wrong?

Actually on further review, you cannot do what i suggested. But it is even easier, because the view gets passed into the adapter. So from the adapter you can just call $this->view->params["relationModel"]->id

Jason Green
Zurmo Team
Need support? Upgrade now


#5 k1000o

k1000o

    Advanced Member

  • Members
  • PipPipPip
  • 112 posts
  • LocationMadrid

Posted 26 May 2012 - 02:37 PM

SOLVED!
This was my first reflex but I got this error

Cannot access protected property ColorsForColorChartRelatedListView::$params

After I tried to write a method in the view to get the value
		public function getRelationModelId()
		{
			return  $this->params["relationModel"]->id ;
		}

And it works

#6 Jason

Jason

    Administrator

  • Administrators
  • 717 posts
  • LocationChicago

Posted 26 May 2012 - 08:19 PM

SOLVED!
This was my first reflex but I got this error

Cannot access protected property ColorsForColorChartRelatedListView::$params

After I tried to write a method in the view to get the value
		public function getRelationModelId()
		{
			return  $this->params["relationModel"]->id ;
		}

And it works

Ah yes, params is protected, so your solution with getRelationModelId is great.

Jason Green
Zurmo Team
Need support? Upgrade now


#7 k1000o

k1000o

    Advanced Member

  • Members
  • PipPipPip
  • 112 posts
  • LocationMadrid

Posted 27 May 2012 - 02:25 PM

I am actually a little bit stuck, I managed to create the function on the controller and pass it the required parameters via ajax, but no sure how to remove the relation.

	    public function actionDeleteRelChartFromAjax($relName, $opportunityId, $ContactId)
	    {
		    if ($relName == 'contacts') {
			    if (YYYYY) {
				    ??????;
			    }
		    }
	    }


#8 Jason

Jason

    Administrator

  • Administrators
  • 717 posts
  • LocationChicago

Posted 27 May 2012 - 10:13 PM

I am actually a little bit stuck, I managed to create the function on the controller and pass it the required parameters via ajax, but no sure how to remove the relation.

		public function actionDeleteRelChartFromAjax($relName, $opportunityId, $ContactId)
		{
			if ($relName == 'contacts') {
				if (YYYYY) {
					??????;
				}
			}
		}

Do something like:
$opportunity = Opportunity::getById($opportunityId);
$contact = Contact::getById($contactId);

$opportunity->contacts->remove($contact);
$opportunity->save();

Jason Green
Zurmo Team
Need support? Upgrade now


#9 k1000o

k1000o

    Advanced Member

  • Members
  • PipPipPip
  • 112 posts
  • LocationMadrid

Posted 28 May 2012 - 02:25 PM

It works thank you. I will open a new topic on how to include it using the customization hook.

Below a summary of the customization steps

modules->accounts->adapters-columns-> DeleteRelatedAccountListViewColumnAdapter



class DeleteRelatedAccountListViewColumnAdapter extends ListViewColumnAdapter
{
	public function renderGridViewData()
	{
		return array(
			'name'		=> $this->attribute,
			'header'	  => '',
			'value'	   => $this->resolveToRenderDeleteLink('$data->' . 'id'),
			'type'		=> 'raw',
			'htmlOptions' => array('class' => 'delete-opportunity-column')
		);
	}
	protected function resolveToRenderDeleteLink($modelId)
	{
		$checkboxId = 'closeTask' . $modelId;
		// Begin Not Coding Standard
		$content = 'CHtml::link("Delete<span class='icon'></span>", "#",
								   array("class" => "remove",
										 "onclick" => "actionDeleteOpportunityFromRelation (this, '' .
				$this->view->getRelationAttribName() . '',' . ''' .
				$modelId  . '',' . ''' .
				$this->view->getRelationModelId() . ''' . ')"))';
      
		Yii::app()->clientScript->registerScript('deleteOpportunityRelationFromListViewScript',
												 "
			function actionDeleteOpportunityFromRelation(element,relName, opportunityId, accountId)
			{				
					$.ajax({
						url : '" . Yii::app()->createUrl('opportunities/default/DelRelAccFromAjax') . "?relName=' + relName + '&opportunityId=' + opportunityId + '&accountId=' + accountId ,
						type : 'GET',
						dataType : 'json',
						success : function(data)
						{
							//remove row
								$(element).parent().parent().remove();
						},
						error : function()
						{
							//todo: error call
						}
					});
			}
		",
																																																																					CClientScript::POS_END);
		// End Not Coding Standard
		return $content;
	}
}


modules->accounts->views->related->OpportunitiesRelatedListView.php




Add the Following column in the related grid
array('cells' =>
	array(
		array(
			'elements' => array(
				array('attributeName' => 'null',
					'type'		  => 'DeleteRelatedAccount',
				),
			),
		),
	)
),


And add the following functions

public function getRelationModelId()
{
	return $this->params["relationModel"]->id;
}
public function getRelationAttribName()
{
	return $this->getRelationAttributeName();
}
public function getRelationModuleId()
{
	return $this->params["relationModuleId"];
}
public function getUniqueLayoutId()
{
	return $this->uniqueLayoutId;
}  


modules->accounts ->controllers->DefaultController.php

Add the following function
public function actionDelRelAccFromAjax($relName,
										$opportunityId,
										$accountId)
{
	if ($relName == 'account') {
		$account = Account::GetById(intval($accountId));
		ControllerSecurityUtil::resolveAccessCanCurrentUserDeleteModel($account);
		$opportunity = Opportunity::GetById(intval($opportunityId));
		$account->opportunities->remove($opportunity);
		$account->save();
	}
}  

The result will be a button to as shown in the attached image

Attached Thumbnails

  • RemoveOpportunities.png


#10 Jason

Jason

    Administrator

  • Administrators
  • 717 posts
  • LocationChicago

Posted 28 May 2012 - 04:10 PM

Great! Sounds like a good tutorial even. Or another wiki article to relate to the customization wiki

Jason Green
Zurmo Team
Need support? Upgrade now





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users