Trying to add a logic hook to suiteCRM when creating or updating a task - sugarcrm

This is my first try into coding for sugarCRM / suiteCRM.
I should say I've been coding for Wordpress for nearly 10 years now, but I'm completely lost now I'm starting to dig into suiteCRM.
I've read that you can add a logic hook to modify the data after saving it to the database, but I don't know where to start...
Imagine I create a task for today, july 7th, related to a client I use to visit every 2 months, so there's a field in Accounts named "Visiting frequency". I'd like to add a future date (july 7th + 60 days = september 7th aprox) into the task's "Future Visiting Date" field, so I can use it to create that particular future task via Workflow.
What I'm trying to do is to calculate a field in tasks (Future visiting date), that equals to the amount of days on the accounts module's field (Visiting frequency) added to the task's own Date field.
I've been able to make it work, using the following layout:
Inside \custom\modules\Tasks\logic_hooks.php
<?php
$hook_version = 1;
$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(
1, //Processing index. For sorting the array.
'future_task_date_on_task_creation', //Label. A string value to identify the hook.
'custom/modules/Tasks/future_visit_date.php', //The PHP file where your class is located.
'before_save_class', //The class the method is in.
'future_visit_date' //The method to call.
);
?>
Inside \custom\modules\Tasks\future_visit_date.php
<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
class before_save_class {
function future_visit_date($bean, $event, $arguments) {
$bean->rhun_fecha_sig_c = date("Y-m-d H:i:s", $date);
}
}
?>
With this setup, the Future Visiting Date gets filled with the calculated date.
I've also read that this setup is not advised, and that I should use the Extension Framework and put the first file in this path:
/custom/Extension/modules/Tasks/Ext/LogicHooks/<file>.php
But I can't make it work.
Do I have to create the LogicHooks folder if it's not there?
Which filename should I assign to this file?
Do I have to change something else inside the code?

Yes, create the LogicHooks directory if it doesn't exist. The PHP file can be called anything you like.
/custom/Extension/modules/Tasks/Ext/LogicHooks/MyLogicHookFile.php
Define your logic hooks in this file as before.
<?php
$hook_version = 1;
$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(
1, //Processing index. For sorting the array.
'future_task_date_on_task_creation', //Label. A string value to identify the hook.
'custom/modules/Tasks/future_visit_date.php', //The PHP file where your class is located.
'before_save_class', //The class the method is in.
'future_visit_date' //The method to call.
);
Then run a repair and rebuild from the Admin panel.
The main advantage to using the Extension framework is that it allows multiple developers to add components to a Sugar instance without worrying about overwriting existing code.
More info can be found about it in the Developer Guide

Related

How can I create and update pages dynamically in Sulu CMS?

I have the following situation:
A database stores information about houses (address, number of rooms, date built, last selling price, etc.)
This database is being manipulated through an app (let's call that app the "backend house app") that cannot be directly integrated in a Sulu-driven app. I can access the stored data through an API that gives me JSON-representations of House-objects. I can also have the app launch some sort of call to a Sulu-driven app when a house is created, updated or deleted.
The Sulu-driven app (let's call that the "frontend house app") with templates for "house", "room", etc., is connected to a different database on a different server. This Sulu-driven app's website-environment shows house-pages with room-pages where some content is pre-filled through a connection to the "backend house app". Other content only exists on the database of the "frontend house app", like user comments, appraisals of interior design, etc., according to configured aspects of the Sulu-templates.
What I want to achieve, is a way to automate the creation, updating and deletion of "frontend house app"-pages based on activity in the "backend house app".
For instance, when a new house is added in the "backend house app", I want it to notify the "frontend house app" so that the "frontend house app" will automatically create the entire node-tree for the newly added house. Meaning: a "house"-page with the required data filled in, "room"-pages for each room, etc., so that the content manager of the "frontend house app" can see the entire tree of the newly added house in the workspace and can start manipulating content in the already available templates. In addition to automatically creating these pages, I also want to pre-set the rights to update and create, since the content manager of the "frontend house app" must not be able to create new rooms or change the name of the house, for instance.
I did not manage to get it working, I'll just add what I already done to show where I got stuck.
I started out with the following code, in a controller that extends Sulu's own WebsiteController:
$documentManager = $this->get('sulu_document_manager.document_manager');
$nodeManager = $this->get('sulu_document_manager.node_manager');
$parentHousesDocument = $documentManager->find('/cmf/immo/routes/nl/huizen', 'nl');
$newHouseDocument = $documentManager->create('page');
// The backendApi just gives a House object with data from the backend
// In this case we get an existing House with id 1
$house = $backendApi->getHouseWithId(1);
$newHouseDocument->setTitle($house->getName()); // For instance 'Smurfhouse'
$newHouseDocument->setLocale('nl'); // Nl is the only locale we have
$newHouseDocument->setParent($parentHouseDocument); // A default page where all the houses are listed
$newHouseDocument->setStructureType('house'); // Since we have a house.xml template
// I need to grab the structure to fill it with values from the House object
$structure = $newHouseDocument->getStructure();
$structure->bind([
'title' => $house->getName(),
'houseId' => $house->getId(),
]);
$newHouseDocument->setWorkflowStage(WorkflowStage::PUBLISHED); // You would expect this to automatically publish the document, but apparently it doesn't... I took it from a test I reverse-engineered in trying to create a page, I have no clue what it is supposed to change.
$nodeManager->createPath('/cmf/immo/routes/nl/huizen/' . $house->getId());
$documentManager->persist(
$newHouseDocument,
'nl',
[
'path' => '/cmf/immo/contents/huizen/' . Slugifier::slugify($house->getName()), // Assume for argument's sake that the Slugifier just slugifies the name...
'auto_create' => true, // Took this value from a test that creates pages, don't know whether it is necessary
'load_ghost_content' => false, // Idem
]
);
$documentManager->flush();
Now, when I fire the controller action, I first get the exception
Property "url" in structure "house" is required but no value was given.
I tried to fix this by just manually binding the property 'url' with value '/huizen/' . $house->getId() to $structure, at the point where I bind the other values. But this doesn't fix it, as apparently the url value is overwritten somewhere in the persist event chain, and I haven't yet found where.
However, I can, just for testing purposes, manually override the url in the StructureSubscriber that handles the mapping for this particular persist event. If I do this, something gets created in the Sulu-app-database - hurray!
My phpcr_nodes table lists two extra records, one for the RouteDocument referring to /cmf/immo/routes/nl/huizen/1, and one for the PageDocument referring to /cmf/immo/contents/huizen/smurfhouse. Both have the workspace_name column filled with the value default_live. However, as long as there are not also records that are complete duplicates of these two records except with the value default in the workspace_name column, the pages will not appear in the Sulu admin CMS environment. Needless to say, they will also not appear on the public website proper.
Furthermore, when I let the DocumentManager in my controller action try to ->find my newly created document, I get a document of the class UnknownDocument. Hence, I cannot have the DocumentManager go ->publish on it; an Exception ensues. If I visit the pages in the Sulu admin environment, they are hence unpublished; once I publish them there, they can be found by the DocumentManager in the controller action - even if I later unpublish them. They are no longer UnknownDocument, for some reason. However, even if they can be found, I cannot have the DocumentManager go ->unpublish nor ->publish - that just has NO effect on the actual documents.
I was hoping there would be a Sulu cookbook-recipe or another piece of documentation that extensively describes how to create fully published pages dynamically, thus without going through the 'manual labor' of the actual CMS environment, but so far I haven't found one... All help is much appreciated :)
PS: For the purposes of being complete: we're running Sulu on a Windows server environment on PHP 7.1; dbase is PostgreSQL, Sulu being a local forked version of release tag 1.4.7 because I had to make some changes to the way Sulu handles uploaded files to get it to work on a Windows environment.
EDIT: a partial solution for making a new house page if none exists already (not explicitly using the AdminKernel, but should of course be run in a context where the AdminKernel is active):
public function getOrCreateHuisPagina(Huis $huis)
{
$parent = $this->documentManager->find('/cmf/immo/routes/nl/huizen', 'nl'); // This is indeed the route document for the "collector page" of all the houses, but this doesn't seem to give any problems (see below)
try {
$document = $this->documentManager->find('/cmf/immo/routes/nl/huizen/' . $huis->id(), 'nl'); // Here I'm checking whether the page already exists
} catch(DocumentNotFoundException $e) {
$document = $this->setupPublishedPage();
$document->setTitle($huis->naam());
$document->setStructureType('huis_detail');
$document->setResourceSegment('/huizen');
$document->setParent($parent);
$document->getStructure()->bind([
'title' => $huis->naam(), // Not sure if this is required seeing as I already set the title
'huis_id' => $huis->id(),
]);
$this->documentManager->persist(
$document,
'nl',
[
'parent_path' => '/cmf/immo/contents/huizen', // Explicit path to the content document of the parnt
]
);
}
$this->documentManager->publish($document, 'nl');
return $document;
}
First of all I think the following line does not load what you want it to load:
$parentHousesDocument = $documentManager->find('/cmf/immo/routes/nl/huizen', 'nl');
It loads the route instead of the page document, so it should look like the following:
$parentHousesDocument = $documentManager->find('/cmf/immo/contents/nl/huizen', 'nl');
Regarding your error with the URL, instead of overriding the StructureSubscriber you should simple use the setResourceSegment method of the document, which does exactly what you need :-)
And the default_live workspace is wrong, is it possible that you are running these commands on the website kernel? The thing is that the WebsiteKernel has the default_live workspace as default, and therefore writes the content in this workspace. If you run the command with the AdminKernel it should land in the default workspace, and you should be able to copy it into the default_live workspace with the publish method of the DocumentManager.
I hope that helps :-)

Symfony FileFormField - Testing (WebTestCase) multiple file upload

In my Symfony web application I have a form allowing multiple file upload (easily done by setting the multiple property of the FileType equal to true). And this works fine: I can select multiple files and upload them. Processing the form and getting all uploaded files also goes fine. But of course, I want to foresee an integration test (WebTestCase) but I don't find any possibility to simulate a multiple file upload.
What I have now:
...
$uploadedFile = new UploadedFile(...);
$form = ...; // get the form from the crawler
$form['formtype[filename]'][0]->upload($uploadedFile);
$this->client->submit($form);
...
That works fine.
But now I want to upload 2 files by 1 form submission (because the processing logic can behave differently when multiple files are uploaded at once). How can I do this? When I look at http://api.symfony.com/3.0/Symfony/Component/DomCrawler/Field/FileFormField.html I don't see any way to pass in, for example, an array of UploadedFile objects. Anyone experience with this?
If the multiple property is set, crawler creates a file form field array with single FileFormField field. One field can hold a single file so you need multiple fields for multiple files. I came to a solution by manually adding more FileFormField to the form.
$form = ...
// get file field node in DOM
$node = $crawler->filter("input[name='formtype[filename][]']")->getNode(0);
// add additional fields to form (you can create as many as you need)
$newField = new FileFormField($node);
$form->set($newField);
...
// set files with upload()
$form['formtype[filename]'][0]->upload($uploadedFile1);
$form['formtype[filename]'][1]->upload($uploadedFile2);
...
//or with submit values
$crawler->submit($form, [
...
'formtype[filename]' => [$uploadedFile1, $uploadedFile2]
]);

Extbase Hooks - execute code upon record creation

I want to create a standard typo3 extension but when I create a record (or modify it) I want to calculate something (in my case I want to call the Google Map API to get coordinates from a given address).
SO I search for a hook or something. Any idea?
One of my project example, may helps you for hook in backend when record has been changed.
In your extension file ext_localconf.php
// Hook for cancellation
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][] = 'EXT:femanager/class.tx_femanager_tcemainprocdm.php:tx_femanager_tcemainprocdm';
hook file class.tx_femanager_tcemainprocdm.php where you can execute
your script
class tx_femanager_tcemainprocdm{
function processDatamap_postProcessFieldArray ($status, $table, $id, &$fieldArray, &$reference){
// $status also called action like delete
// $table - table name of excute backend action
// $id - record UID
// $fieldArray - fields of your table
if($table = 'your_extension_table_name'){
// your script
}
}
}
Maybe this answer is useful to you.
Register your class as a data handling hook in your extension. This one "is called AFTER all commands of the commandmap" were executed. Maybe you need to look for a more appropriate one.
Then in your registered Hook i.e. 'typo3conf/ext/your_ext/Classes/Hooks/AfterCreate.php' do your calculation. Hope this sets you on the right track.
In my special case there was no need to calculate the coordinates when the record got saved. So I just used the listAction in the controller, check if coordinates are there and if not call the Google API (and send an email if the Google API does not give a coordinate back).
In another case where the new record comes from a frontend plugin and I had to do something with this data I used the createAction in the Controller. (I am not sure if the createAction is also called when the record is created from the backend.)

How to customize register and contact forms in PrestaShop?

I need to know how to customize my contact and register forms. How to add new fileds ( and ) and make the information from these fields required or not required.
I need to know which files I must edit for these forms...
I use prestashop 1.4.7.0
This is really two separate questions as there are major differences in how you would handle each case.
Answer 1
For the registration form you can write a module which contains two hook handler functions. These will be:
public function hookCreateAccountForm() {}
public function hookCreateAccount($params) {}
The first function allows you to add additional fields to the registration form (by default these are inserted at the end of the form authentication.tpl, although you could move them all as a single group elsewhere). It should simply return the additional form html you require.
The second function provides you with two parameters to handle the account creation process. This is executed after the standard fields have been validated and the new customer has been created. Unfortunately you cannot do validation on your additional fields using this (you would need to either use javascript or override AuthController to perform your own authentication in the preProcess() member function). In one of my own custom modules for a site I have the following, for example:
public function hookCreateAccount($params)
{
$id_lang = (int)Configuration::get('PS_LANG_DEFAULT');
$customer = $params['newCustomer'];
$address = new Address(Address::getFirstCustomerAddressId((int)$customer->id));
$membership_number = $params['_POST']['membership_number'];
....
....
}
$params['newCustomer'] is a standard Prestashop element in the array and contains the newly created customer object. Your fields will be in the $params['_POST'] array - in my case it was an input field called membership_number.
Answer 2
For the contact form it's a whole lot more complicated I'm afraid. The simplest method for the html is to just hard-code your additional fields in the template file contact-form.tpl.
To actually process the form you will need to create an override for the controller by ceating a file called ContactController.php in /<web-root>/<your-optional-ps-folder>/override/controller containing something like:
<?php
class ContactController extends ContactControllerCore {
function preProcess()
{
if (Tools::isSubmit('submitMessage'))
{
// The form has been submitted so your field validation code goes in here.
// Get the entered values for your fields using Tools::getValue('<field-name>')
// Flag errors by adding a message to $this->errors e.g.
$this->errors[] = Tools::displayError('I haven't even bothered to check!');
}
parent::preProcess();
if (Tools::isSubmit('submitMessage') && is_empty($this->errors))
{
// Success so now perform any addition required actions
// Note that the only indication of success is that $this->errors is empty
}
}
}
Another method would be to just copy the entire preProcess function from controllers\ContactController and just hack away at it until it does what you want....

Best Zend Framework architecture for large reporting site?

I have a site of about 60 tabular report pages. Want to convert this to Zend. The report has two states: empty report and filled in with data report. Each report has its own set of input boxes and select drop downs to narrow down searches. You click on submit and it retrieves the data. Thats all each page does.
Do I create 60 controllers with each one with default index action and getData action? All I have read online do not really describe how to architect a real site.
If the method of fetching and retrieving data is pretty similar as you mention between all 60 reports. It would seem silly to create 60 controllers (+PHP files).
It seems that you are trying to solve this problem with the default rewrite router. You can add a route to the router that will automatically store your report name, and you can abstract and delegate the logic off to some report-runner-business-object-thingy.
$router = $ctrl->getRouter(); // returns a rewrite router by default
$router->addRoute(
'reports',
new Zend_Controller_Router_Route('reports/:report_name/:action',
array('controller' => 'reports',
'action' => 'view'))
);
And then something like this in your controller...
public function viewAction() {
$report = $this->getRequest()->getParam("report_name");
// ... check to see if report name is valid
// ... stuff to set up for viewing report...
}
public function runAction() {
$report = $this->getRequest()->getParam("report_name");
// ... check to see if report name is valid
// Go ahead and pass the array of request params, as your report might need them
$reportRunner = new CustomReportRunner( $report, $this->getRequest()->getParams() );
$reportRunner->run();
}
You get the point; hope this helps!