How does one create a SugarCRM View that combines 2 Detail Views - sugarcrm

I would like to extend the Contact Detail View so that a Detail View of the associated account appears on the same view.
MY instinct is to override the display function for the Contacts Detail View and from there create an instance of the Accounts Detail and attach it's display output.
But I don't know if there is a standard way of pulling this of.

I learned that in the up coming version (6.3), there will be a way of generating computed fields that have access to the fields of a related module.
If this is the case, then one option will be to create computed fields that reference the Account fields and then add a panel to Contact DetailView with the referenced Account fields.
Though, my original hunch proved to be doable as well and not as hacky as I had assumed at first:
<?php
require_once('include/MVC/View/views/view.detail.php');
class ContactsViewDetail extends ViewDetail {
function ContactsViewDetail() {
parent::ViewDetail();
}
function preDisplay(){
parent::preDisplay();
// Configuration to display All account info
$this->dv2 = new DetailView2();
$this->dv2->ss =& $this->dv->ss;
$this->bean2 = new Account();
$this->bean2->retrieve($this->bean->account_id);
$accountMetadataFile = 'custom/modules/Accounts/metadata/detailviewdefs.php';
$accountTemplate = 'custom/modules/Accounts/tpls/AccountsDetailView.tpl';
$this->dv2->setup('Accounts', $this->bean2, $accountMetadataFile, $accountTemplate);
}
function display(){
parent::display();
// Display Accounts information.
$this->dv2->process();
echo $this->dv2->display();
}
}
?>
In summary
Override the detail view.
Add a new display to the current View.
Add a new bean (module) to the View.
Process the display with the new bean.
Echo the display.

Another easier option may be just add an iframe field, which loads the detailview on the account inside of it. Not as pretty, but lots less hacking as well.

Related

Best practice to lazy load data on tab click

Within the onBeforeRendering() function of a view how should I determine if a specific node is present in the model and modify the model to include the node if not? This question is about lazy loading of data to the model for data that will be infrequently accessed by users but would have a performance penalty if loaded with initial model data.
My use case has a list page leading to a detail page for whatever item in the list the use clicks. On the detail page is a set of tabs which expose sub-details related to the selected detail. Specifically a long text description of a the brief for a task.
The tab control leads via manifest.json style routing to display a view in the tabs content area.
This is my current working code which is within the onBeforeRendering event of the view controller:
onBeforeRendering: function(oEvent){
var sPath = this.getView().getBindingContext("Projects").getPath(); // something like /task/6
console.log('Path='+sPath)
var oModel = this.getView().getModel("Projects");
var oTask = oModel.getProperty(sPath + "/brief");
if (oTask) { // looks like /brief exists so must already have loaded the brief
// nothing to do
console.log('Use existing data')
}
else { // /brief not yet present so we need to load it up
console.log('Load new data')
oModel.setProperty(sPath + "/brief", "This is the brief...") // replace with loaddata() from server, use attachRequestCompleted to call function to modify model.
}}
Question - is this the correct approach?
Edit: Based on discussion in this question I modified my code to use an event that fires per display of the view. onBeforeRendering turned out to run without much apparent predictability - which I am sure it has but in any case I wanted a one-per-display event. Also, I fleshed out the code further but retained the basic structure and it appears to do what I wanted.
This is a valid approach. But you should think aboute following use case: What happens if the data you loaded have been changed at the backend? The JSONModel does not give you any support here as it acts dumb data store only.

Zend creating forms based on requests within one controller/action

I don't really know how to word the title well, but here's my issue. I decided instead of having 25 controllers to handle pages, I have one PageController with a viewAction that takes in a :page parameter - for example, http://localhost/website/page/about-us would direct to PageController::viewAction() with a parameter of page = about-us. All of the pages are stored in a templates folder, so the viewrenderer is set to render application\templates\default\about-us.phtml.
I did this so I can consolidate and it seemed like a better approach. My question is the following: lets say when the page request is contact-us, I would need a Zend_Form to be used within the contact page. So, I would need a way within PageController::viewAction() to recognize that the page needs to have a form built, build the form, and also upon submission the need to process it (maybe this should be handled in an abstract process method - not sure).
I have no idea how to implement this. I thought maybe I can store a column with the name of a form and a connecting page identifier. Even better, create a one-to-many page to forms, and then in the submission loop through the forms and check if submitted and if so then process it (maybe there is a isSubmitted() method within zend_form. I really don't know how to handle this, and am looking for any help i can get.
Thanks!
Here is something that came to mind that may work or help point you in a direction that works for you.
This may only work well assuming you were to have no more than one form per page, if you need more than one form on a page, you would have to do something beyond this automatic form handling.
Create a standard location for forms that are attached to pages (e.g. application/forms/page). This is where the automatic forms associated with pages will be kept.
In your viewAction, you could take advantage of the autoloader to see if a form for that page exists. For example:
$page = $this->getParam('page');
$page = ucfirst(preg_replace('/-(\w)/ie', "strtoupper('$1')", $page)); // contact-us -> ContactUs
$class = 'Application_Form_Page_' . $page;
// class_exists will invoke the autoloader to map a class to a file
if (class_exists($class)) {
// a form is defined for this page
$form = new $class();
// check if form was posted
if ($this->getRequest()->isPost()) {
if ($form->isValid($this->getRequest()->getPost()) {
// form is valid - determine how to process it
}
}
// assign the form to the view
$this->view->pageForm = $form;
}
All this really leaves out is the action you take to process a specific form. Since the contact form will likely generate an email, and another form may insert data into a database, you will need some sort of callback system or perhaps another class that can be mapped automatically which contains the form processor code.
Anyway something along those lines is what came to mind first, I hope that helps give you some more ideas.

How do I avoid repetition when passing variables from the Controller/Action to the Layout

I am currently working on a project developed using Zend Framework, based on the structure of my web page design I have reached a point where I have to pass a small number of variables to my layout from each Controller/Action. These variables are:
<?php Zend_Layout::getMvcInstance()->assign('pageId', 'page1'); ?>
<?php Zend_Layout::getMvcInstance()->assign('headerType', '<header id="index">'); ?>
The reason for passing this information is firstly, I pass the page id as the multi column layout may change depending on the content being displayed, thus the page id within the body tag links the appropriate CSS to how the page should be displayed. Secondly I display a promotional jQuery slider only on the index page, but I need the flexibility to have it displayed on potentially multiple pages in case the wind changes and the client changes their mind.
My actual question: Is there a more appropriate method of passing this information to the Layout that I am overlooking?
I am not really questioning whether the information has to be sent, rather is there some Zend Framework feature that I have, in my haste, overlooked which would reduce the amount of repetitive redundant code which may very well be repeated in multiple Actions within the same controller?
You could turn that logic into an action helper than you can call from your controllers in a more direct way. You could also make a view helper to accomplish the same thing but view helpers usually generate data for the view rather than set properties.
// library/PageId.php
class Lib_PageId extends Zend_Controller_Action_Helper_Abstract
{
public function direct($title, $pageId, $headerType)
{
$view = $this->getActionController()->view;
$view->headTitle()->append($title);
$view->pageId = $pageId;
$view->headerType = $headerType;
}
}
In your controller actions you can now do this:
$this->_helper->PageId('Homepage', 'page1', 'index');
// now pageId and headerType are available in the view and
// Homepage has been appended to the title
You will also need to register the helper path in your Bootstrap like this:
protected function _initActionHelpers()
{
Zend_Controller_Action_HelperBroker::addPrefix('Lib');
}
Doing it like that can reduce the amount of repetitive code and remove needing to assign the values from the view. You can do it in the controller very quickly. You can also have default values in the case that the helper hasn't been called.
You shoudn't really be passing anything from the view to the layout, for a start the view should be included IN the layout, not the other way around.
So, setting your page title should be done using similar code to what you have, but inside the controller action being called:
$this->view->headTitle()->append('Homepage');
And the other two issues - you need to rethink as I stated to begin with. Maybe you're misunderstanding the layout/view principle? If you include the different views per action, then you simply change the div id when needed, and include the header for your banner only in the index.phtml file.

Automatically clear captured placeholder view-helper content after it is displayed

Generating a mail message in a partial, I use the placeholder view-helper as follows:
<?php $this->placeholder('mykey')->startCapture() ?>
Some content here that is actually more complicated than just text.
Trust me that, in this case the, using the placeholder for capturing
is desirable.
<?php
$this->placeholder('mykey')->endCapture();
echo $this->placeholder('mykey');
?>
The problem is that if I use the same key in a different partial for a different mail message within the same request, then this captured content is still stored in the container for that key. In principle, I'd like the partials to be free to use whatever placeholder keys they want without having to sweat what other partials are using.
I know I can use different keys in different partials. Alternatively, I can manually clear the content after use/display with something like:
$this->placeholder('mykey')->set('');
But I'd hate to put the burden of all that on the view script that uses the placeholder.
I suspect what I want to do is create my own custom placeholder view-helper that auto-clears his captured content after it has been output.
I've tried creating a custom placeholder container (extends the Standalone container, overriding the toString() method), creating a custom view-helper (extends the the standard Placeholder view-helper), and telling the view-helper to use the custom container class.
But I keep bumping into some error associated to a missing view object. Clearly, I'm missing something about the how the view object, the container, and the registry all interact - and probably even something how the plugin system loads them all.
Any advice and general explanation greatly appreciated.
You need to set this container in the Placeholder view helper because otherwise the Zend_View_Helper_Placeholder_Registry loads automatically the Zend_View_Helper_Placeholder_Container. So, first you need to set your custom container manually. In a view script:
$this->getHelper('placeholder')
->getRegistry()
->setContainerClass('My_View_Helper_Placeholder_Container');
Or for exameple in a _initCustomContainer() in your Bootstrap.php:
$view = $this->bootstrap('view')->getResource('view');
$view->getHelper('placeholder')
->getRegistry()
->setContainerClass('My_View_Helper_Placeholder_Container');
Then, you need to create this class based on the Zend_View_Helper_Placeholder_Container (and not the Zend_View_Helper_Placeholder_Container_Standalone. I'd suggest you keep the option open to reset the content or not, you do that with a setter:
class My_View_Helper_Placeholder_Container
extends Zend_View_Helper_Placeholder_Container
{
protected $_resetCapture = true; // defaults true for new behaviour
public function setResetCapture($flag)
{
$this->_resetCapture = (bool) $flag;
return $this;
}
public function toString($indent = null)
{
$return = parent::toString($indent);
if ($this->_resetCapture) {
$this->exchangeArray(array());
}
return $return;
}
}
By default, the reset capture is already on, but to switch it off and start capturing:
$this->placeholder('my_key')->setResetCapture(false)->startCapture();
And to switch it on again:
$this->placeholder('my_key')->setResetCapture(true);
In a view script, use:
$this->placeholder('mykey')->captureStart('SET');
or using the class constant:
$this->placeholder('mykey')->captureStart(Zend_View_Helper_Placeholder_Container_Abstract::SET);

Zend framework - access controller or model from the view

I need to give the front-end designer the ability to choose whether or not to display a single xml feed or an mash-up, from the view.phtml file
This means I need to be able to call a method from the controller or model which then returns a variable to the view containing the requested feed(s).
So how do I access methods of the controller or model from the view?
you don't call controller methods in view , but you can create an instance of model (for read only purposes) inside view and then call its public methods .eg
Foo.phtml
<?php $feedsTb = new Default_Model_Feeds() ?>
<?php $allFeeds = $feedsTb->fetchAll(); ?>
I don't know if i got your problem right, but this is something i'd probably do in a way like
Controller:
if($this->_getParam('single')) {
$this->view->data = $model->getFeedSingleData();
$this->render('single_feed.phtml');
} else { //mashup
$this->view->data = $model->getMashUpData();
$this-render('mashup_feed.phtml');
}
Though admittedly an example like this is better off with two different actions (singleAction() and mashupAction())
But i really don't know if i got your problem figured out at all :S You may explain it further