How to set Navigation menu based on User type - SOCIALENGINE - zend-framework

How to set Navigation based on user type/profile type logged in.
All I could do is - set auth for specific user type
if (!$this->_helper->requireAuth()->setAuthParams('<module>', null, 'create')->isValid())
return;
But with this the menu will be still available
For example - If a Candidate logs in I should be able to hide create JOB menu.
Please give some insights on this. Thanks for your time!

Please create a function in menu.php in plugin of your module.You can check user module and also add a entry 'engine4_core_menuitems'. In table find your menu add entry in plugin _Plugin_Menus. Below function will have naming convention as your your function Name.
public function onMenuInitialize_JobMenu()
{
$viewer = Engine_Api::_()->user()->getViewer();
//assuming level id 4 for candidate
if( !$viewer->level_id == '4') {
return false;
}
return array(
'label' => 'create Job',
'route' => 'job_general',
'params' => array(
'action' => 'groupsms'
)
);
}

Related

TYPO3 TCA make the 'default' value dynamic

The title is rather self explanatory, but what i would like to have is a dynamic default value.
The idea behind it is to get the biggest number from a column in the database and then add one to the result. This result should be saved as the default value.
Lets take for example this code:
$GLOBALS['TCA'][$modelName]['columns']['autojobnumber'] = array(
'exclude' => true,
'label' => 'LLL:EXT:path/To/The/LLL:tx_extension_domain_model_job_autojobnumber',
'config' => [
'type' => 'input',
'size' => 10,
'eval' => 'trim,int',
'readOnly' =>1,
'default' => $result,
]
);
The SQL looks like this:
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_extension_domain_model_job');
$getBiggestNumber = $queryBuilder
->select('autojobnumber')
->from('tx_extension_domain_model_job')
->groupBy('autojobnumber')
->orderBy('autojobnumber', 'DESC')
->setMaxResults(1)
->execute()
->fetchColumn(0);
$result = $getBiggestNumber + 1;
So how can i do that "clean"?
I thought about processCmdmap_preProcess but i dont know how to pass the value to the coorisponding TCA field. Plus i do not get any results on my backend when i use the DebuggerUtility like i get them when i use processDatamap_afterAllOperations after saving the Object.
Can someone point me to the right direction?
I don't think it is supported to create a dynamic default value, see default property of input field.
What you can do however, is to create your own type (use this instead of type="input"). You can use the "user" type. (It might also be possible to create your own renderType for type="input", I never did this, but created custom renderTypes for type= "select").
You can look at the code of InputTextElement, extend that or create your own from scratch.
core code: InputTextElement
documentation for user type
more examples in FormEngine documentation
Example
(slightly modified, from documentation)
ext_localconf.php:
$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'][<current timestamp>] = [
'nodeName' => 'customInputField',
'priority' => 40,
'class' => \T3docs\Examples\Form\Element\CustomInputElement::class,
];
CustomInputElement
<?php
declare(strict_types = 1);
namespace Myvendor\MyExtension\Backend\FormEngine\Element\CustomInputElement;
use TYPO3\CMS\Backend\Form\Element\AbstractFormElement;
// extend from AbstractFormElement
// alternatively, extend from existing Type and extend it.
class CustomInputElement extends AbstractFormElement
{
public function render():array
{
$resultArray = $this->initializeResultArray();
// add some HTML
$resultArray['html'] = 'something ...';
// ... see docs + core for more info what you can set here!
return $resultArray;
}
}

Yii2 rest: checkAccess on restAction

After tackling this other question we would now like to check if the authenticated user can view, update or delete an existing record. Since checkAccess() is called by default in all restActions the following seemed the most logic thing to try:
public function checkAccess($action, $model = null, $params = []) {
if(in_array($action, ['view', 'update', 'delete'])) {
if(Yii::$app->user->identity->customer->id === null
|| $model->customer_id !== Yii::$app->user->identity->customer->id) {
throw new \yii\web\ForbiddenHttpException('You can\'t '.$action.' this item.');
}
}
}
But the API seems to ignore this function. We added this function in our controller. The actions (view, update and delete) are the default restActions.
Our BaseController sets actions like this:
...
'view' => [
'class' => 'api\common\components\actions\ViewAction',
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
'scenario' => $this->viewScenario,
],
...
Are we forgetting something?
Just add the following inside your custom action before executing any other code as it was done in the default view action (see source code here):
if ($this->checkAccess) {
call_user_func($this->checkAccess, $this->id, $model);
}
note: $this->checkAccess is defined in parent yii\rest\Action so your custom ActionView class need to either extend yii\rest\Action or redefine the variable public $checkAccess;
We obviously should have seen that the viewAction is not the default but an altered api\common\components\actions\ViewAction ... Not sure how we missed that...

cakephp select dropdown list

i have the following models: Student,Form,FormsStream.
the student model have foreign keys to both models. Student.form_id is a foreignKey to the form table,Student.stream_id is foreign to the FormsStream table. i already have entries for both forms and formsStreams.
to add the student Form and Stream i select both from respective drop-down list. i already created the drop-down list.
my Problem.
previously i was able to save/edit student form and stream but after making those fields foreign keys to be selected i cant save new or edit those fields. i use cakePHP 2.5.4.
however i can do that in SQL.
i cant seem to figure out the problem with the select lists.
my method to get the forms from the database
public function loadStudentForm() {
$this->loadModel('Student'); //load the associated models
$this->loadModel('Form');
$forms = $this->Form->find('list',array('fields' => 'form_name')); //get the forms from the database
$this->set('forms',$forms);
}
method to get streams from database
public function loadStreams() {
$this->loadModel('FormsStream');
$streams = $this->FormsStream->find('list',array('fields' => 'stream_name'));
$this->set('streams',$streams);
}
student add method
public function addStudent() {
if ($this->request->is('post')) {
$this->loadStudentForm();
$this->loadStreams();
$this->Student->create();
if ($this->Student->save($this->request->data)) {
$this->Session->setFlash(__('New student record added to the database.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The student could not be added.Please ensure all fields are correctly entered'));
}
}
}
the extract from add.ctp for selecting student form and stream
echo $this->Form->input('forms',array('label' => 'Form'));
echo $this->Form->input('streams',array('label' => 'Class stream'));
so far printing contents of validateErrors shows this:
array(
'Student' => array(
'form_id' => array(
(int) 0 => 'You must select the form'
),
'stream_id' => array(
(int) 0 => 'You must choose a stream'
)
),
'Form' => array(),
'FormsStream' => array()
)
i've even tried to intercept the request in burpsuite and i can see the form and stream id are passed.just dont get why they are empty in the above array
_method=POST&data%5BStudent%5D%5Badmission_no%5D=1002&data%5BStudent%5D%5Bfirst_name%5D=peter&data%5BStudent%5D%5Bmiddle_name%5D=per&data%5BStudent%5D%5Blast_name%5D=pee&data%5BStudent%5D%5Bgender%5D=Male&data%5BStudent%5D%5Bdate_of_birth%5D%5Bmonth%5D=11&data%5BStudent%5D%5Bdate_of_birth%5D%5Bday%5D=05&data%5BStudent%5D%5Bdate_of_birth%5D%5Byear%5D=2014&data%5BStudent%5D%5Bjoin_date%5D%5Bmonth%5D=11&data%5BStudent%5D%5Bjoin_date%5D%5Bday%5D=05&data%5BStudent%5D%5Bjoin_date%5D%5Byear%5D=2014&data%5BStudent%5D%5Bforms%5D=1&data%5BStudent%5D%5Bsstrong texttreams%5D=1
The input must be form_id and stream_id. Even if you set them with plural form cakephp recognize it in the following form.
echo $this->Form->input('form_id',array('label' => 'Form'));
echo $this->Form->input('stream_id',array('label' => 'Class stream'));

How to disable filter of a field in Zend Framework 2 in the controller?

Form to add/edit user I get from service manager with already installed filter, which is the test password. But this password is not needed when the user is edited. Can I somehow disabled password field validation in the controller?
In the getServiceConfig function of the module:
// ....
'UserCRUDFilter' => function($sm)
{
return new \Users\Form\UserCRUDFilter();
},
'UserCRUDForm' => function($sm, $param, $param1)
{
$form = new \Users\Form\UserCRUDForm();
$form->setInputFilter($sm->get('UserCRUDFilter'));
return $form;
},
// ....
In the controller I first of all getting a form object from service manager:
$form = $this->getServiceLocator()->get('UserCRUDForm');
Then disable user password validation and requirements, when user is edited and password not specified:
if ($user_id > 0 && $this->request->getPost('password') == '') {
$form->.... // Someway gained access to the filter class and change the password field validation
}
And after this i make a validation:
$form->isValid();
I found it!
// If user is editted - clear password requirement
if ($user_id > 0) {
$form->getInputFilter()->get('password')->setRequired(false);
$form->getInputFilter()->get('confirm_password')->setRequired(false);
}
This lines is disables requirement of input form fields :)
if you like to set all validators by yourself, call inside your form class
$this->setUseInputFilterDefaults(false);
to disable auto element validations/filter added from zend.
if you like to remove filter from elements call in your controller after your form object this
$form->getInputFilter()->remove('InputFilterName');
$form->get('password')->removeValidator('VALIDATOR_NAME'); should do the trick.
Note that you may have to iterate trough the Validatorchain when using Fieldsets.
$inputFilter->add(array(
'name' => 'password',
'required' => true,
'allow_empty' => true,
));
And on ModeleTable: saveModule:
public function saveAlbum(Album $album)
{
$data = array(
'name' => $album->name,
);
if (isset($album->password)){
$data['password'] = $album->password;
}

SugarCRM- How to get POPUP when click on Save button?

I want when click Save in Edit View of some module (example Contact) to get pop up with some message (later I will get options OK and Cancel on that pop up.).
My function
YAHOO.SUGAR.MessageBox.show({msg: 'Foo'} );
is working when I put it at top of editviewdefs.php (I also must include
cache/include/javascript/sugar_grp_yui_widgets.js) ) file and when opening that view I am getting that pop up. But I want it to popup on Save,not when opening EditView (this was just testing that showed me that YAHOO function is working). So I try to create seperate customJavascript.js file in custom/modules/Contacts:
//<script type="text/javascript"
src="cache/include/javascript/sugar_grp_yui_widgets.js"></script>
function check_custom_data()
{
YAHOO.SUGAR.MessageBox.show({msg: 'Foo'} );
}
I modified custom/modules/Contacts/metadata/editviewdefs.php
<?php
$module_name = 'Contacts';
$viewdefs ['Contacts'] =
array (
'EditView' =>
array (
'templateMeta' =>
array (
'form' =>
array (
'hidden' =>
array (
0 => '<input type="hidden" name="opportunity_id" value="{$smarty.request.opportunity_id}">',
1 => '<input type="hidden" name="case_id" value="{$smarty.request.case_id}">',
2 => '<input type="hidden" name="bug_id" value="{$smarty.request.bug_id}">',
3 => '<input type="hidden" name="email_id" value="{$smarty.request.email_id}">',
4 => '<input type="hidden" name="inbound_email_id" value="{$smarty.request.inbound_email_id}">',
),
),
array(
'buttons' =>
array (
0 =>
array(
'customCode' =>
'<input title="Save [Alt+S]" accessKey="S" onclick="this.form.action.value=\'Save\'; return check_custom_data();" type="submit" name="button" value="'.$GLOBALS['app_strings']['LBL_SAVE_BUTTON_LABEL'].'">',
),
1 =>'Cancel'
)
),
'includes'=> array(
array('file'=>'custom/modules/Contacts/customJavascript.js'),
),
..........
.......
but when I click Save in EditView nothing happens but I want in that moment to get pop up with message (later I will add OK and Cancel options).
What am I doing wrong?
thank you
Updated with code for pop up only with some condition:
....
window.formToCheck = formname;
var contactTypeField = document.getElementById('first_name');
if (contactTypeField.value == 'Tori')
{
if (confirm("This dialog will pop-up whenever the user click on the Save button. "
+ "If you click OK, then you can execute some custom code, and then "
+ "execute the old form check function, which will process and submit "
+ "the form, using SugarCRM's standard behavior.")) {
var customCodeVariable = 5;
customCodeVariable = 55 + (customCodeVariable * 5);
return window.old_check_form(formname);
}
return false;
}
There are a number of ways to do things in SugarCRM, which makes it both very powerful and at times very difficult to customize - as there are so many different options available to you.
To make some kind of pop-up, or any custom log, happen upon clicking the "Save" button, I'd recommend the below solution rather than altering the editviewdefs.
The below solution does not require you modify any core SugarCRM files, so it is upgrade safe and can easily be installed on another instance.
What you will need to do is create a custom installable package, and install it into SugarCRM using the Module Loader.
This is the layout of the directory structure you will ultimately need to end up with:
SugarModuelPopUp
->custom
->include
->customPopUps
->custom_popup_js_include.php
->customPopUpContacts.js
->manifest.php
Create the SugarModuelPopUp folder, which will server as the root of this custom package.
Inside of SugarModuelPopUp, create a new PHP file with the name manifest.php. This file tells SugarCRM how to install the package.
In manifest.php, paste the following code:
<?php
$manifest = array(
array(
'acceptable_sugar_versions' => array()
),
array(
'acceptable_sugar_flavors' => array()
),
'readme' => 'Please consult the operating manual for detailed installation instructions.',
'key' => 'customSugarMod1',
'author' => 'Kyle Lowry',
'description' => 'Adds pop-up dialog on save on Contacts module.',
'icon' => '',
'is_uninstallable' => true,
'name' => 'Pop-Up Dialog On Save',
'published_date' => '2013-03-06 12:00:00',
'type' => 'module',
'version' => 'v1',
'remove_tables' => 'prompt'
);
$installdefs = array(
'id' => 'customSugarMod1',
'copy' => array(
array(
'from' => '<basepath>/custom/',
'to' => 'custom/'
)
),
'logic_hooks' => array(
array(
'module' => 'Contacts',
'hook' => 'after_ui_frame',
'order' => 1,
'description' => 'Creates pop-up dialog on save action.',
'file' => 'custom/include/customPopUps/custom_popup_js_include.php',
'class' => 'CustomPopJs',
'function' => 'getContactJs'
)
)
);
Next, you will want to make the custom folder. Inside of that, create the include folder. Inside of that, create the customPopUps folder.
Next, you will want to create the custom_popup_js_include.php file. This file controls when and where your custom JavaScript gets included on the page. Paste in the below code:
<?php
// prevent people from accessing this file directly
if (! defined('sugarEntry') || ! sugarEntry) {
die('Not a valid entry point.');
}
class CustomPopJs {
function getContactJs($event, $arguments) {
// Prevent this script from being injected anywhere but the EditView.
if ($_REQUEST['action'] != 'EditView') {
// we are not in the EditView, so simply return without injecting
// the Javascript
return;
}
echo '<script type="text/javascript" src="custom/include/customPopUps/customPopUpContacts.js"></script>';
}
}
Next you will need to create the customPopUpContacts.js file, which will create the custom pop-up upon clicking the Save button in the Contacts module EditView. Paste in the below code:
function override_check_form() {
// store a reference to the old form checking function
window.old_check_form = window.check_form;
// set the form checking function equal to something custom
window.check_form = function(formname) {
window.formToCheck = formname;
// you can create the dialog however you wish, but for simplicity I am
// just using standard javascript functions
if (confirm("This dialog will pop-up whenever the user click on the Save button. "
+ "If you click OK, then you can execute some custom code, and then "
+ "execute the old form check function, which will process and submit "
+ "the form, using SugarCRM's standard behavior.")) {
// you have clicked OK, so do some custom code here,
// replace this code with whatever you really want to do.
var customCodeVariable = 5;
customCodeVariable = 55 + (customCodeVariable * 5);
// now that your custom code has executed, you can let
// SugarCRM take control, process the form, and submit
return window.old_check_form(formname);
}
// the user clicked on Cancel, so you can either just return false
// and leave the person on the form, or you can execute some custom
// code or do whatever else you want.
return false;
}
}
// call the override function, which will replace the old form checker
// with something custom
override_check_form();
Once you have created the above directory structure, and the files in the correct folders, you can create a ZIP file of the project. It is important to note that for SugarCRM installable packages, your ZIP file must contain everything in the project directory. That is, you will not be zipping up the SugarModuelPopUp folder, but rather everything inside of it.
Next, you will want to install the custom package using SugarCRM's module loader. You can do this by:
Go to the SugarCRM Admin page.
Click on "Module Loader".
Click on "Browse" and select the ZIP package.
Click on the "Upload" button.
Once the package is uploaded, find its entry in the list of installable packages, and click on "Install"; proceeding with the standard SugarCRM installation process.
With this custom package installed, whenever you click on the "Save" button in the Contacts module EditView, a dialog will pop-up. You can replace the dialog code with anything you want, so as log as you don't modify the code framing it.
Further, you should be able to use this project as a foundation for future function additions to SugarCRM EditViews. Any module which uses the check_form method upon clicking of the "Save" button can have this kind of custom logic executed.
To do so for Accounts, for example, you would do the following:
Add an entry to the logic_hooks array element in manifest.php for Accounts.
'logic_hooks' => array(
array(
'module' => 'Contacts',
'hook' => 'after_ui_frame',
'order' => 1,
'description' => 'Creates pop-up dialog on save action.',
'file' => 'custom/include/customPopUps/custom_popup_js_include.php',
'class' => 'CustomPopJs',
'function' => 'getContactJs'
),
array(
'module' => 'Accounts',
'hook' => 'after_ui_frame',
'order' => 1,
'description' => 'Creates pop-up dialog on save action.',
'file' => 'custom/include/customPopUps/custom_popup_js_include.php',
'class' => 'CustomPopJs',
'function' => 'getAccountJs'
)
)
Add a new method to the CustomPopJs in the custom_popup_js_include.php file for the Accounts JavaScript.
function getAccountJs($event, $arguments) {
// Prevent this script from being injected anywhere but the EditView.
if ($_REQUEST['action'] != 'EditView') {
// we are not in the EditView, so simply return without injecting
// the Javascript
return;
}
echo '<script type="text/javascript" src="custom/include/customPopUps/customPopUpAccounts.js"></script>';
}
Create the customPopUpAccounts.js file, and use the customPopUpContacts.js code as a base for the functionality you want.
There are other ways of accomplishing your goal in SugarCRM, but this is the one I use personally, and it has the benefit of being upgrade safe and easily migratable to other SugarCRM instances.
I have SugarCRM CE 6.5 and this method didn't work to me (I mean creating the new module). However, I modified the logic_hook and put the files directly in custom/include folder and it works!