I'm creating an Invoice Module on SugarCRM Community Edition (version 6.5.16).

The trick I want to pull of is to make a field readonly based on the value of a checkbox named 'Sent'. So: If the invoice is sent the user is not able to edit the invoice anymore.

I know making a field readonly in editviewdefs can be done by setting type to readonly:

0 => 
array (
'name' => 'invoicenumber',
'label' => 'LBL_INVOICENUMBER',
'type' => 'readonly',
),

However, this field is now readonly regardless of the checkbox 'sent' is checked.

I tried editing the array with PHP in editviewdefs.php but this resulted in always closing the fields. That makes sense if editviewdefs.php is built once for all records.

Is there a way to do this with PHP or SugarCode (which is PHP of course)? The last resort I want to turn to is using javascript, but kind of want to avoid it since this is quite important functionality. One user accidentally turning off javascript could eventually be turning into quite a mess.

So, If you have any questions, please ask. For the sake of this OP I have included as little code as possible, but if you have questions regarding code feel free to ask!

Thanks a lot!

有帮助吗?

解决方案

I found it.

The problem wasn't where I put the code, the problem was caching of the viewdefs. So the first item seen after repair+rebuild would be the default for the others. I didn't spot the problem at first since I had SugarCRM set to Developer Mode, in which it does not cache viewdefs.

The easiest way to go is like this: First, make a copy editviewdefs and name it whatever you like (mine is: closededitviewdefs.php) Now, Change whatever you like in the newly created editview file. After you made the changes, change the 5th line in closededitviewdefs.php from 'EditView' => to 'ClosedEditView' => When done, create a file in your module/views folder: view.edit.php. These are it's contents (replace module specific contents where applicable):

<?php
class [REPLACEWITHMODULENAME]ViewEdit extends ViewEdit {
     public function preDisplay() {

        $bean = $this->bean;
        $isClosed = $bean->sent;

        if ($isClosed==1) {
            $metadataFile = 'custom/modules/[MODULEFOLDER]/metadata/closededitviewdefs.php';
            $this->ev = $this->getEditView();
            $this->ev->view = 'ClosedEditView';
        } else {
            $metadataFile = 'custom/modules/[MODULEFOLDER]/metadata/editviewdefs.php';
            $this->ev = $this->getEditView();
        }
        $this->ev->ss =& $this->ss;
        $this->ev->setup($this->module, $this->bean, $metadataFile, get_custom_file_if_exists('include/EditView/EditView.tpl'));
    }
}
?>

This way two cache files are created from now on. There was one major problem with this: No header and footer in your closededitview! You can easily tackle this by adding the following to closededitviewdefs.php:

'form' =>
array(
    'headerTpl' => 'include/EditView/header.tpl',
    'footerTpl' => 'include/EditView/footer.tpl',
),

Make sure developer mode is off for correct testing results. Run Repair & rebuild. Now everything should work as expected. :)

其他提示

This would be a great example where Sugarcrm's Sugar Logic works great. The sugar development blog wrote about a similar example a while ago http://developer.sugarcrm.com/2012/11/08/using-sugar-logic-to-conditonal-make-fields-read-only/

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top