This topic contains 9 replies, has 2 voices, and was last updated by shreyas 7 years, 7 months ago.
- Topic
Hi Team,
I need to customize the field values in During Lead Creation in (LeadType.php).so in Lead form type i have used CreateQueryBuilder to get values from DB to display as options.Here i have a requirement wherein there are 3 types of users to be displayed based on their roles in 3 different cols.(i.e Secondary Salesperson , sales manager & BDC) which have different role associated using oro_access_role which is mapped with respective user using oro_user_access_role.So i tried joining the oro_user with roles table & its giving result as expected. Here i have another requirement wherein i need to check the user logged in & using his logged in Id i need to fetch only those rows which are under him.(i.e if a Manager Logs in then he should only see the sales persons who are under him). So i have queries to do it as given below:
1234567891011121314151617181920212223242526272829303132333435363738394041424344private function getallusersidforlogin() {$usr = $this->get('security.context')->getToken()->getUser();$loggedinuserId = $usr->getId();$em = $this->getDoctrine()->getEntityManager();$connection = $em->getConnection();$qrysecurity = "select a.id, a.business_unit_owner_id, a.username, c.label, group_concat(d.business_unit_id) as mainbuid, concat_ws(',', group_concat(e.id), group_concat(f.id)) as childbuidfrom oro_user ainner join oro_user_access_role b on b.user_id = a.idinner join oro_access_role c on c.id = b.role_idinner join oro_user_business_unit d on d.user_id = a.idleft join oro_business_unit e on e.business_unit_owner_id = d.business_unit_idleft join oro_business_unit f on f.business_unit_owner_id = e.idwhere a.id = $loggedinuserId";$securitystatement = $connection->prepare($qrysecurity);$securitystatement->execute();$secuirtyqry1 = $securitystatement->fetchAll();foreach ($secuirtyqry1 as $result) {$level = $result['label'];$childbuid = $result['childbuid'];}if ($level == 'Sales Rep') {//add qry --u.id = $loggedinuserId$getusersid = $loggedinuserId;} else {if ($childbuid != '') {$getusersid1 = $childbuid;} else {$getusersid1 = 0;}//$getusersid = $this->getusersformgrs($getusersid1);$q1 = "select group_concat(id) as res from oro_user where business_unit_owner_id in ($getusersid1)";$q1res = $connection->prepare($q1);$q1res->execute();$q1result = $q1res->fetchAll();foreach ($q1result as $result) {$getusersid = $result['res'];}}return $getusersid;}This function i have used in my controller for same kind of requirement & it works perfectly fine.
But the same way when i try it in FormType its not possible for me to get User logged Inn Id. I get some errors . So i thought that since its not extending controller but extending AbstractType i am facing issue & so tried creating an object of Lead Controller & calling a function similar to above which returns the Id’s required but it again threw an exception. I also looked at some of links on how to get context Id , But i could not make it clearly.Please find the links given below:
Get User ID in Form TypeAccess Current User In form Type
Please find the code given below for the Lead Form Type:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111public function buildForm(FormBuilderInterface $builder, array $options){//$loggedUser = $this->context->getToken()->getUser(); -- error// print_r($loggedUser);$builder->add('name', 'text', array('required' => true, 'label' => 'orocrm.sales.lead.name.label'))->add('dataChannel','orocrm_channel_select_type',array('required' => true,'label' => 'orocrm.sales.lead.data_channel.label','entities' => ['OroCRM\\Bundle\\SalesBundle\\Entity\\Lead'],))->add('namePrefix', 'text', array('required' => false, 'label' => 'orocrm.sales.lead.name_prefix.label'))->add('firstName', 'text', array('required' => true, 'label' => 'orocrm.sales.lead.first_name.label'))->add('lastName', 'text', array('required' => true, 'label' => 'orocrm.sales.lead.last_name.label'))->add('customer','orocrm_sales_b2bcustomer_select',array('required' => false, 'label' => 'orocrm.sales.lead.customer.label'))->add('salesType','entity',['label' => 'orocrm.sales.lead.salesType.label','required' => false,'class' => 'OroCRMSalesBundle:SalesType','query_builder' => function (EntityRepository $entityRepository) {return $entityRepository->createQueryBuilder('salesType')->orderBy('salesType.order', 'ASC');}])->add('leadStatus','entity',['label' => 'orocrm.sales.lead.salesLeadStatus.label','required' => false,'class' => 'OroCRMSalesBundle:SalesLeadStatus','query_builder' => function (EntityRepository $entityRepository) {return $entityRepository->createQueryBuilder('leadStatus')->orderBy('leadStatus.order', 'ASC');}])->add('secondarySalesPerson','entity',['label' => 'orocrm.sales.lead.secondarySalesPerson.label','required' => false,'class' => 'OroUserBundle:User','query_builder' => function (EntityRepository $entityRepository) {return $entityRepository->createQueryBuilder('s')->join('s.roles', 'role')->where('role = :role')->setParameter('role', 2);//oro_access_role table to get id of role required: 7 -Leads Development Rep ,4 - Sales Manager,2 -Sales Rep}])->add('salesManager','entity',['label' => 'orocrm.sales.lead.salesManager.label','required' => false,'class' => 'OroUserBundle:User','query_builder' => function (EntityRepository $entityRepository) {$arr = array(1,2,5,6,7); // here get the loggedin users uid & all his team mates based on buid// $arr = $this->getalldata();$parameters = array('role' => 4//oro_access_role table to get id of role required: 7 -Leads Development Rep ,4 - Sales Manager,2 -Sales Rep,'usridrr' => $arr);return $entityRepository->createQueryBuilder('s')->join('s.roles', 'role')->where('role = :role')->andWhere('s.id IN(:usridrr)')->setParameters($parameters);}])->add('bdc','entity',['label' => 'orocrm.sales.lead.bdc.label','required' => false,'class' => 'OroUserBundle:User','query_builder' => function (EntityRepository $entityRepository) {return $entityRepository->createQueryBuilder('s')->join('s.roles', 'role')->where('role = :role')->setParameter('role', 7);//oro_access_role table to get id of role required: 7 -Leads Development Rep ,4 - Sales Manager,2 -Sales Rep}])->add('account','orocrm_account_select',['required' => false,'label' => 'orocrm.sales.lead.relatedAccount.label',]);}In the Above code given for salesManager field i am trying to get AllUserIdsunderloggedin Mgr (right now hardcoded in code by an array of Ids) but got stuck on how to do it using the LoggedIn user concept as explained in above function getallusersidforlogin().
Please find the LeadController.php code for Creating Lead :
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748protected function update(Lead $entity,Request $request){$usr= $this->get('security.context')->getToken()->getUser();$loggedinuserId = $usr->getId();$getusersid = '';$secuirtyqry1 = $this->securitychecker($loggedinuserId);foreach ($secuirtyqry1 as $result) {$level = $result['label'];$childbuid = $result['childbuid'];}if ($level == 'Sales Rep') {$getusersid = $loggedinuserId;} else {if ($childbuid != '') {$getusersid1 = $childbuid;} else {$getusersid1 = 0;}$getusersid = $this->getusersformgrs($getusersid1);}// $form = $this->get('form.factory')->create('orocrm_sales_lead',$entity);// $form->handleRequest($request);return $this->get('oro_form.model.update_handler')->handleUpdate($entity,$this->get('orocrm_sales.lead.form'),function (Lead $entity) {return array('route' => 'orocrm_sales_lead_update','parameters' => array('id' => $entity->getId()));},function (Lead $entity) {return array('route' => 'orocrm_sales_lead_view','parameters' => array('id' => $entity->getId()));},$this->get('translator')->trans('orocrm.sales.controller.lead.saved.message'),$this->get('orocrm_sales.lead.form.handler'));}Here i also tried using the $form by using form Factory (commented in code given above) but i could not get it through this too.Please need help if any resources or suggestions on how to go further to solve this issue.
Please find the screenshot given below of create Lead form shown:
Screenshot of Customized Lead Creation FormI am also trying to find out on how to display the options similar to the one that is displayed in Owner Field (i.e can be searched & selected from options also – As shown in below image)
OwnerDisplayFieldPlease need some suggestions on how to go forward in this issue.
Thanks & Regards,
Shreyas S
The forum ‘OroCRM’ is closed to new topics and replies.