Activity Log in dashboard sonata admin - symfony

I'm trying to implement activity log in the dashboard i.e. a notification in each row that says which entities have a change since the last login of a user.
To do it I'm thinking about overwrite the class AdminListBlockService and the template block_admin_list.html.twig
but i don't have yet clear how do it.
someone know a better way to do it? if that is the better way, how can I achieve it?
thanks a lot!
ok
I found a better way... i have overwritten only the block_admin_list.html.twig so:
//config.yml
sonata_admin:
templates:
list_block: AdminBundle:Block:block_admin_list.html.twig
note the diference "SonataAdminBundle" and "AdminBundle"
next step add in the template:
{% if admin.activityLog() is defined and admin.isGranted('LIST') %}
<a class="btn btn-link" href="{{ admin.generateUrl('list')>admin.activityLog</a>
{% endif %}
and finally create the logic for each Entity where i want the notification
//in the exempleAdmin
public function activityLog(){
// custom code $activity= ....
return $activity;
}
if someone know a better way to do it, please let me know, thanks

Well what you can do is override the template like this:
In your admin class:
// Configure our custom roles for this entity
public function configure() {
parent::configure();
$this->setTemplate('list', 'MyAdminBundle:CRUD:list-myentity.html.twig');
}
Then in your template you can do something like:
{# The default template which provides batch and action cells, with the valid colspan computation #}
{% extends 'SonataAdminBundle:CRUD:list.html.twig' %}
{% block table_body %}
<tbody>
{% for object in admin.datagrid.results %}
<style>
table tr.green-color td {background-color: #2BFF5D !important; }
</style>
<tr {% if changed %} class="green-color" {% endif %}>
{% include admin.getTemplate('inner_list_row') %}
</tr>
{% endfor %}
</tbody>
{% endblock %}

Related

Symfony 3.2 Easyadminbundle how to hide/remove default actions link

I was wondering if someone can tell me how to hide the action links from the list view base on a status column.
More details: I have a list view in which shows a list of items, In this list I have column named status. For each record in this list in which status is set to close, I would like to hide the edit/delete and other custom actions links from the list. Is this doable? if so, how?
Thanks
A possible solution is to override just the item_actions Twig block in the list.html.twig template used by that entity. In practice, if the entity is called Order, a template like this should work:
{# app/Resources/views/easy_admin/Order/list.html.twig #}
{% extends '#EasyAdmin/default/list.html.twig' %}
{% block item_actions %}
{% if item.status != 'close' %}
{{ parent() }}
{% endif %}
{% endblock item_actions %}

Using raw Cypher to query Neo4j in Symfony

I am trying to go throught this tutorial: http://www.sitepoint.com/adding-social-network-features-php-app-neo4j/ But using the Symfony Framework instead of Silex.
I have been able to set up Neo4j to run with Symfony and am able to right user data to the graph. Now I would like to display all user email addresses in a list. I have taken this script:
public function home(Application $application, Request $request)
{
$neo = $application['neo'];
$q = 'MATCH (user:User) RETURN user';
$result = $neo->sendCypherQuery($q)->getResult();
$users = $result->get('user');
return $application['twig']->render('index.html.twig', array(
'users' => $users
));
}
And adapted it to read:
public function showUsersAction()
{
$em = $this->container->get('neo4j.manager');
$query = 'MATCH (n:`User`) RETURN n';
$users = $em->cypherQuery($query);
//print_r($users);
return $this->render('UserBundle:Account:showUsers.html.twig', array('users' =>$users));
}
And The twig looks as follows:
{% extends '::base.html.twig' %}
{% block content %}
<h1>get all users:</h1>
<ul>
{% for user in users %}
<li>{{ user.property('email') }}</li>
{% endfor %}
</ul>
{% endblock %}
But something in the twig is wrong, im getting the error:
Method "property" for object "Everyman\Neo4j\Query\Row" does not exist in UserBundle:Account:showUsers.html.twig at line 6
The problem was found in the syntax of the twig file. After consulting this page: https://github.com/jadell/neo4jphp/wiki/Cypher-and-gremlin-queries it became clear, that I had to include user['n'] in my twig template. The twig template now looks as such:
{% extends '::base.html.twig' %}
{% block content %}
<h1>get all users:</h1>
<ul>
{% for user in users %}
<li>{{ user['n'].getProperty('email') }}</li>
{% endfor %}
</ul>
{% endblock %}
I'm the author of the article you mentioned. The thing is that you use a different neo4j library than the one used in the article, hence neoclient, so the methods used in the article are different than the methods provided with neo4jphp.
As NeoClient uses heavily the Symfony components, integrating it in Symfony is really easy, you just need to override the DI. Example here : https://github.com/graphaware/GithubNeo4j/tree/master/src/GraphAware/Neo4jBundle
You'll then be able to use the methods illustrated in the 3 articles I wrote on Sitepoint.
So your problem with the twig template is that he doesn't find the getProperty method of the node object class, which is normal as neo4jphp returns Row object classes.
If you switch back to neoclient, as in the article, in the Twig template you can just write :
{% for user in users %}
<li>{{ user.getProperty('email') }}</li>
{% endfor %}

symfony twig extends: 'only extend if'

In twig there is the 'extends' tag, as found here; http://twig.sensiolabs.org/doc/tags/extends.html#conditional-inheritance
Now what I wanna do is something along the lines of the following example from that page:
{% extends standalone ? "minimum.html" : "base.html" %}
But rather than having 2 templates to extend from, I just want to extend from a template if a specific condition is met.
Now I've tried things such as:
{% extends boolean ? "template.html.twig" : "" %}
and:
{% if boolean %}
{% extends "template.html.twig" %}
{% endif %}
but the former gives an error saying it cannot find a template (since "" obviously isnt a valid path), and the latter just doesn't appear to do anything at all (or rather, it loads for a while and ends up not showing anything)
I've tried some other approaches, but couldn't come up with anything, so figured I'd ask here if I might be missing something.
Thanks in advance for any replies :)
EDIT: To sum up my intent; I am wondering if I can tell my template to only extend if a certain condition is met, and otherwise skip the extend step. (if condition then extend else do nothing)
Twig files are generated into PHP classes.
The extends tag should be the first tag in the template, as:
the {% extends %} tag will be converted to the PHP extends so the child template will inherit from the parent template.
the {% if %} tag is generated as a PHP if, inside a method of the template class, so you can't use {% if %} to extend some class or not.
Anyway, you can extends a variable coming from your context, so you should put your condition in the controller.
if ($boolean) {
$template = 'hello.twig';
} else {
$template = 'world.twig';
}
$this->render("MyBundle:MyFeature:child.html.twig", array('template' => $template);
And then in child.html.twig:
{% extends template %}
I came with this hack: added empty layout only with content block. Seems to be working :) I can pass variable from controller and page is loaded with or without layout.
<!-- base.html.twig -->
<head>
...stuff...
</head>
<body>
{% block content %}{% endblock %}
</body>
<!-- empty.html.twig -->
{% block content %}{% endblock %}
<!-- some_page.html.twig -->
{% extends boolean ? 'base.html.twig' : 'empty.html.twig' %}
{% block content %}
Now this is my real content
{% endblock %}
In pure twig language, it could be something like this :
{% if app.request.pathinfo starts with '/react' %}
{% set extendPath = "::react_base.html.twig" %}
{% else %}
{% set extendPath = "CoreBundle::layout.html.twig" %}
{% endif %}
{% extends extendPath %}

SonataAdmin: replace ID in breadcrumbs

How can I replace Object's ID in SonataAdmin breadcrumbs by some other text?
If I set __toString() in my document, it works only for editing. When I attempt to create new record, there is something like MyDocument:0000000000e09f5c000000006a48ef49 in the last breadcumb.
I'm searching for a method which allows me to set some text as the last breadcump if Document::toString() returns null.
This behaviour is implemented directly in the entity:
public function __toString()
{
return $this->getFoo() ? : '-';
}
Bundles are using variants of this, including return (string)$this->getFoo(); or $this->getFoo() ? : 'n/a'; etc.
Related question: toString method for SonataAdminBundle Listing in Symfony2
BTW something cool to know, you can completely customize the breadcrumb via a Twig template:
{% block sonata_breadcrumb %}
{% set _breadcrumb %}
<li>Home</li>
<li>Library</li>
<li class="active">Data</li>
{% endset %}
{{ parent() }}
{% endblock %}

symfony2 - twig - how to render a twig template from inside a twig template

I have a xxx.html.twig file which shows a page, but when I want to refresh the page with different data and just update it with new data, I have a select and a submit button for it.
The thing is that I don't know how do I call an action in the controller which I pass parameters to from my twig and call for new data and then I render the same twig template again with new parameters.
How do I do so?
Here are a few different ways:
{{ render(app.request.baseUrl ~ '/helper/test', {"hostid2": hostid } ) }}
or
{% include 'MyCoreBundle:Helper:test.html.twig' with {"hostid2": hostid } only %}
or
{% render controller("MyCoreBundle:Helper:test", {'hostid2': hostid}) %}
Symfony 2.1:
{% render 'YourBundle:YourController:yourAction' with {'var': value} %}
Symfony 2.6+:
{{ render(controller('YourBundle:YourController:yourAction', {'var': value})) }}
And, of course, read the documentation.
I think some parts are depricated here.
To make the include work in latest Symfony 3.1.10, I solved it like this:
{% extends 'base.html.twig' %}
{% block body %}
{{ include('AppBundle:Default:inner_content.html.twig') }}
{% endblock %}
Note: include() with parentheses.
Then all the variables are included from the parent template. If you like to restrict some variables in the child template, you use with ... only (look over)

Resources