OROCRM Datagrid cell template ignored in DatagridListener using datagridConfig addColumn() method - datagrid

I have an OROCRM application and there is a datagrid, needs to be extended with a column named by userNames, and intented to display data in a custom twig cell.
To maintain this i made a DataGridListener class and in it a method to extend columns:
/**
* Creates specific column names for selected persons
*
* #param Datagrid $datagrid
* #param array $persons
*
* #return array
*/
protected function addColumnsForPersons(Datagrid $datagrid, array $persons): array
{
$config = $datagrid->getConfig();
$additionalColumns = [];
foreach ($persons as $person) {
$column = [
'label' => $person->getName(),
'template' => 'MyBundle:Partials/Datagrid/Attendance:person_attendance.html.twig',
'type' => 'twig',
'frontend_type' => 'html',
'data_name' => 'person_' . $person->getId(),
'data' => 'person_' . $person->getId(),
'translatable' => false,
'editable' => false,
'align' => 'middle'
];
$config->addColumn('person_' . $person->getId(), $column);
$additionalColumns[] = 'person_' . $person->getId();
}
return $additionalColumns;
}
I add also data for person by the following:
$wrapperData = $datagrid->getData();
foreach($attendancesAtDay as $attendance) {
// This block can be changed to a workaround
$dayData['person_' . $attendance['personid']] = (object)([
'arrival' => Carbon::createFromFormat('Y-m-d G:i:s', $attendance['arrival'])->format('H:i'),
'leave' => Carbon::createFromFormat('Y-m-d G:i:s', $attendance['leave'])->format('H:i'),
'logins' => $attendance['logins'],
'worktime' => gmdate('H:i', $attendance['worktime'] * 60)
]);
}
$gridData[] = $dayData;
$wrapperData->setData($gridData);
The template is reachable and PHPStorm can follow its link from reference, and is the following:
<div>
<div style="width:40px">
{{ value.worktime }}
</div>
<div style="width:40px">
{{ value.arrival }}
</div>
<div style="width:40px">
{{ value.leave }}
</div>
<div style="width:40px">
{{ value.logins }}
</div>
</div>
With this method i got the columns named by person, but with empty data. With xdebug the code never goes into the template.
With some workaround if i change the block in the foreach it renders needed data.
The workaround is the following:
$dayData['person_' . $attendance['personid']] = sprintf(
'<table>
<tr>
<td>
Worktime
</td>
<td>
Arrival
</td>
<td>
Leave
</td>
<td>
Logins
</td>
</tr>
<tr>
<td>
%s
</td>
<td>
%s
</td>
<td>
%s
</td>
<td>
%s
</td>
</tr>
</table>',
gmdate('H:i', $attendance['worktime'] * 60),
Carbon::createFromFormat('Y-m-d G:i:s', $attendance['arrival'])->format('H:i'),
Carbon::createFromFormat('Y-m-d G:i:s', $attendance['leave'])->format('H:i'),
$attendance['logins']);
I tried to use array instead of object without success, or any error message in var/logs/dev.log .
I wanted to avoid this workaround, and wondering why don't throws error in case i added booth twig template to column and html code as data, and why doesn't runs into the cell template?
So what do i wrong?
Thanks for any help!

Related

add checked elements into my entity

I have an entity Equipe (Team) that have OneToMany relation with Employe (a team member). So in order to create a team, in my interface I have a list of all employees and for each employee I have a checkbox, if you checked it means that that employee will be a member of this team. I have no idea how to get it work. This is my entity Equipe.
/**
* #ORM\OneToMany(targetEntity="OC\UserBundle\Entity\Employe", mappedBy="equipe", cascade={"remove", "persist"})
*/
protected $employe;
this my entity Employe
/**
* #ORM\ManyToOne(targetEntity="OC\EquipeBundle\Entity\Equipe", inversedBy="employe")
* #ORM\JoinColumn(name="Eq_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $equipe;
and this is my twig to add a team
{%for c in agents %}
{%if (( c.direction == app.user) )%}
<tr class="odd gradeX">
<td>{{c.id}}</td>
<td>{{c.nom}}</td>
<td>{{c.prenom}}</td>
<td>{{c.Poste}}</td>
<td><div class="checkbox">
<center><label>
<input type="checkbox" value="" name="check{{c.id}}">
</label></center>
</div></td> </tr>
{%endif%}
{%endfor%}
And finaly this is image of my twig add a team
Any suggestion how to get it work ? Thanks !
You can doing what you need with Ajax :
In your js file
function check(checkbox) {
if (checkbox.checked) {
$.ajax({
url: Routing.generate('your_route_name'),
type : 'GET',
data: $('#employee_code').val(),
});
}
}
in your twig file
<input type="checkbox" name="checkEmployee" onclick = "check(this)"/>
With this function check, you can verify if your checkox input in your table is checked and you call a method in a controller that doing the rest!
Good luck!
Actually this is how I proceeded. In my EquipeType
$builder
->add('date')
->add('nom')
->add('employes', 'entity', array(
'class' => 'OCUserBundle:Employe',
'property' => 'username',
'multiple' => true,
'expanded' => true,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.id', 'ASC');
And in my EquipeController
foreach ($equipe->getEmployes() as $emp) {
$emp->setEquipe($equipe);
$em->persist($emp);
}
And this is how I called it in my twig
{{form_widget(form.employes)}}
And that's it .. it works but I still have some issues in the twig I didn't know how to show them one by one. I think I'm going to post it in another subject . Thanks for your help

Which is best paginator for in doctrine symfony2?

I have written custom queries in my repository class and they returns arrays then I do some processing on those arrays then displays to twig.
So please suggest the best pagination method to apply paging on this custom queries resulting in arrays.
I am new to symfony2, does default paging will work and how? I mean what syntax, please provide example.
You should try Knp Paginator. It is simple and customizable.
Simple code example (Doctrine MongoDB ODM):
// Pay attention: query, not result.
$query = $this->getRepositoryOfferKind()->createQueryBuilder()
->field('is_private')->equals(false)
->field('is_deleted')->notEqual(true)
->sort('updated_at', 'DESC')->getQuery();
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate($query, $request->get('page', 1), 20);
/* #var $pagination SlidingPagination */
$pagination->setUsedRoute('admin_offer_kind_index');
$pagination->setPageRange(10);
return array(
'objects' => $pagination,
);
And twig:
<table>
<thead>
<tr>
<th>Title</th>
</tr>
</thead>
<tbody>
{% for object in objects %}
<tr>
<td>
{{ object.title }}
</td>
</tr>
{% else %}
<tr>
<td>No data</td>
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr>
<td>
{{ knp_pagination_render(objects) }}
</td>
</tr>
</tfoot>
</table>
You can try this native solution
public function getPagination($someValue, int $page = 1, $limit = 20, $sort = "createdAt", $sortOrder = 'DESC')
{
$qb = $this->createQueryBuilder();
$qb->field('some_filed')->equals($someValue);
// and so on
return $qb->sort($sort, $sortOrder)
->skip(($page - 1) * $limit)
->limit($limit)
->getQuery()->toArray();
}

How to iterate over the current object in an array and only print the values in Handlebars?

Consider this array :
var urls = [
{ alias : 'home',
path : '/page/home',
title: 'Home',
desc : 'simple test'
},
{ alias : 'home1',
path : '/page/home1',
title: 'Home1',
desc : 'simple test1'
},
{ alias : 'home2',
path : '/page/home2',
title: 'Home2',
desc : 'simple test2'
},
];
How can I iterate over all the properties inside an object in the array and print their values? I don't care about the names of the properties.
I would like to do someting like:
{{#urls}}
<tr>
{{#each .}}
<td>
{{value}}
</td>
{{/each}}
</tr>
{{/urls}}
And this would output
<tr><td>home</td><td>/page/home</td><td>Home</td><td>simple test</td></tr>
<tr><td>home1</td><td>/page/home1</td><td>Home1</td><td>simple test1</td></tr>
<tr><td>home2</td><td>/page/home2</td><td>Home2</td><td>simple test2</td></tr>
I don't want to type:
{{#urls}}
<tr>
<td>
{{alias}}
</td>
<td>
{{path}}
</td>
<td>
{{title}}
</td>
<td>
{{desc}}
</td>
</tr>
{{/urls}}
I might have more than 10 properties so I don't want to type their names anywhere I want to iterate over them.
My problem is I don't know how to access the current object in iteration or pass it to a helper. Do you know how to do that in Handlebars?
I don't want to put the values in arrays instead because I still need to see the properties for debugging.
working jsFiddle
Helper:
Handlebars.registerHelper('renderUrl', function(urls) {
var dom = '';
urls.forEach(function (url) {
dom += '<tr>';
for(key in url) {
dom += '<td>' + url[key] + '</td>'
}
dom += '</tr>';
});
return new Handlebars.SafeString(dom);
});

image upload in WordPress admin option page not working

i working on plugin that has in back-end to add url of of site or post and short description and in front end show then in a widget,i have button for small image of post to be uploaded but it didi't work out but same code work fine in normal php...
$upload_errors = array(
// http://www.php.net/manual/en/features.file-upload.errors.php
UPLOAD_ERR_OK => "No errors.",
UPLOAD_ERR_INI_SIZE => "Larger than upload_max_filesize.",
UPLOAD_ERR_FORM_SIZE => "Larger than form MAX_FILE_SIZE.",
UPLOAD_ERR_PARTIAL => "Partial upload.",
UPLOAD_ERR_NO_FILE => "No file.",
UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.",
UPLOAD_ERR_CANT_WRITE => "Can't write to disk.",
UPLOAD_ERR_EXTENSION => "File upload stopped by extension."
);
// process the form data
$tmp_file = $_FILES['file_upload']['tmp_name'];
$target_file = basename($_FILES['file_upload']['name']);
$upload_dir = "uploads";
// You will probably want to first use file_exists() to make sure
// there isn't already a file by the same name.
// move_uploaded_file will return false if $tmp_file is not a valid upload file
// or if it cannot be moved for any other reason
if(move_uploaded_file($tmp_file, $upload_dir."/".$target_file)) {
$message = "File uploaded successfully.";
} else {
$error = $_FILES['file_upload']['error'];
$message = $upload_errors[$error];
}
this is the form used to upload image
<form action='' method='post' name="text_form" onsubmit="return Blank_TextField_Validator()" enctype="multipart/form-data">
<table class='form-table'><tr valign='top'>
<th scope='row'><lable for='new_Directory_name'>Enter the Title:</lable></th>
<td><input type='text' id='newtextchange' name='newtextchange' size="100" /></br></td>
</tr>
<tr>
<th scope='row'><lable for='new_Directory_name'>Enter the Description:</lable></th>
<td><textarea rows="4" cols="50" name='textarea1'>
</textarea></br></td>
</tr>
<tr>
<th scope='row'><lable for='new_Directory_name'>Enter the URL:</lable></th>
<td><input type='text' id='newtextchange1' name='newtextchange1' size="100" /></br></td>
</tr>
<tr>
<th scope='row'><lable for='new_Directory_name'>Upload image:</lable></th>
<td> <input type="hidden" name="MAX_FILE_SIZE" value="1000000" /><input type="file" name="file_upload" /><br><br><input id='addtobow' class='button-secondary action' type='submit' value='Add to Best of web' name='submit'/></td>
</tr>
</table>
</form>
You should look at wp_handle_upload for this one.
The example given there is very usefull.
To save the url you can use the following lines:
$upload_overrides = array( 'test_form' => false );
$source = wp_handle_upload( $_FILES['file'], $upload_overrides );
if ( $source )
$input = serialize( $source );
Hope it helps!
problem is with the file upload url and this is how i fix it..,now it is working fine...
$tmp_file = $_FILES['file_upload']['tmp_name'];
$target_file = basename($_FILES['file_upload']['name']);
//$upload_dir = "D:\softwares_installed\wamp\www\wordpress\wp-content\plugins\bestofweb\uploads";
$upload_dir =ABSPATH . "wp-content/plugins/bestofweb/uploads";
$up_urlp1="/wp-content/plugins/bestofweb/uploads";
// You will probably want to first use file_exists() to make sure
// there isn't already a file by the same name.
// move_uploaded_file will return false if $tmp_file is not a valid upload file
// or if it cannot be moved for any other reason
if(move_uploaded_file($tmp_file, $upload_dir."/".$target_file)) {
//$message = "File uploaded successfully.";
//echo $upload_dir."/".$target_file;
//echo bloginfo('wpurl');
$up_url= $up_urlp1."/".$target_file;
//echo $up_url;
//if($message == "File uploaded successfully.")
// {
// $imgpath=$upload_dir.
// }
} else {
$error = $_FILES['file_upload']['error'];
// $message = $upload_errors[$error];
}

Drupal theme_table... A way to theme nested tables?

Title pretty much explains what I want to do... I'm not a fan of using nested tables, so believe me, I'm unhappy and would so, so, so totally prefer something else... but, c'est la vie...
Essentially, I'm trying to figure out how to create a nested table utilizing the theme_table function... I can't seem to find any information on how to do that...
The markup I'm aiming to achieve ($data is the array of information that I'm building the table off of):
<table class="atb">
<tbody>
<tr class="action">
<table class="inner-atb">
<tr class="un"><td colspan="2">$data['name']</td></tr>
<tr class="data">
<td class="img">$data['image']</td>
<td class="untext">
<span class="untext-style">
<span class="untext">$data['text']</span>
<span class="separator"></span>
<span class="timestamp">$data['timestamp']</span>
</span>
</td>
</tr>
</table>
</tr>
</tbody>
</table>
The info you need can be found here.
What you need to do would be something like this disclaimer, not tested
$header_inner = array();
$rows_inner = array();
$rows_inner[] = array('data' => $data['name'], 'colspan' => 2, 'class' => 'un');
$rows_inner[] = array(
array('data' => $data['image']),
array('data' => '<span>...</span>'),
);
$header_outer = array();
$rows_outer = array(theme('table', $header_inner, $rows_inner, array('class' => 'inner-atb')));
$output = theme('table', $header_outer, $rows_outer);
I didn't put all the classes and stuff in, but have given you an overview of how to do it. If things fail check the doc link provided.

Resources