I have a plugin with 2 components. One is for 'posts', and the other is for a 'profile'. The profile belongs to a user and hasMany 'posts'.
However when I access the relationship it loads every post. I just want to load 5 then paginate or lazy load, how can I do this?
Profile.php model
public $hasMany = [
'posts' => [
'Redstone\Membership\Models\Post',
'table' => 'redstone_membership_profiles_posts',
]
];
public $belongsTo = [
'user' => [
'Rainlab\User\Models\User',
]
];
Post.php model
public $belongsTo = [
'profile' => ['Redstone\Membership\Models\Profile',
'table' => 'redstone_membership_profiles_posts',
]
];
Profile.php component
protected function loadProfile()
{
$id = $this->property('profileUsername');
$profile = new UserProfile
$profile = $profile->where(['slug' => $id]);
$profile = $profile->first();
return $profile;
}
profile/default.htm - component view
{% set profile = __SELF__.profile %}
{% set posts = __SELF__.profile.posts %}
{% for post in posts %}
<div class="card">
<div class="card-header">
<div class="ml-2">
<div class="h5 m-0">{{ profile.title }}</div>
</div>
{{ post.published_at|date('M d') }}
</div>
<div class="card-body text-left">
{{ post.content|raw }}
</div>
</div>
{% else %}
<h1>This user has not made any posts.</h1>
{% endfor %}
Well you can either do something with the OctoberCMS pagination service or a php function or you could build a function through twig.
PHP using slice: This is assuming when you call $profile->posts you get a collection of posts. You could also add a query to the Url like example.com/profile?q=15 to change 5 to 10 15 etc I added an if statement to check to make sure the input is numeric and greater than 5.
protected function loadProfile()
{
$id = $this->property('profileUsername');
$profile = UserProfile::where(['slug' => $id])->first();
if (is_numeric(Input::get('q')) == true && Input::get('q') > 5) {
$profile->posts = $profile->posts->slice(0, Input::get('q'));
} else {
$profile->posts = $profile->posts->slice(0, 5);
}
return $profile;
}
Twig using slice: This is done very similar to the PHP way but done in the htm file instead.
PHP -
protected function getQuery()
{
if (is_numeric(Input::get('q')) == true && Input::get('q') > 5) {
return Input::get('q');
} else {
return 5;
}
}
Twig -
{% set query = __SELF__.getQuery %}
{% set profile = __SELF__.profile %}
{% set posts = __SELF__.profile.posts | slice(0, query) %}
{% for post in posts %}
<div class="card">
<div class="card-header">
<div class="ml-2">
<div class="h5 m-0">{{ profile.title }}</div>
</div>
{{ post.published_at|date('M d') }}
</div>
<div class="card-body text-left">
{{ post.content|raw }}
</div>
</div>
{% else %}
<h1>This user has not made any posts.</h1>
{% endfor %}
Related
I have the following ajax I call:
$('.checkbox').click(function () {
$.ajax({
type:'POST',
url: '/loadProducts',
data: {},
success: function(response) {
$('.js-products').html(response);
}});
return false;
});
Now when I call my endpoint loadProducts, I wants to get products, render a product template(s) and return all of them.
In my endpoint I could do this:
/**
* #Route("/loadProducts", name="loadProducts")
*/
public function loadProducts() {
/* #var \AppBundle\Service\ProductService $productService */
$productService = $this->get('app.product');
$products = $productService->getProducts();
$productItems = [];
foreach ($products as $product) {
$productItems = $this->render('home/productItem.html.twig', [
'product' => $product
]);
}
$response = new Response();
$response->headers->set('Content-Type', 'application/json');
$response->setContent(json_encode($productItems));
return $response;
}
But that only renders one product. How could I return rendering multiple product.html.twig files? (apart from creating a new template that renders all)
You will get something like this. Your action will start to render products.html.twig which have a loop that renders multiple times product.html.twig
products.html.twig
{% extends base_template %}
{% block body %}
<div class="container">
<div class="row">
{% for product in products %}
<div class="col-md-3">
{% include 'shop/product.html.twig' %}
</div>
{% endfor %}
</div>
</div>
{% endblock %}
product.html.twig
<div class="product">
<img src="somesource.jpg">
<div class="description">
<h3>{{ product.name }}</h3>
<p>{{ product.description }}</p>
</div>
</div>
I'm following this tutorial to create a simple OctoberCMS plugin.
Here is content of /acme/demo/components/todo/default.htm page:
{% set tasks = __SELF__.tasks %}
<form data-request="{{ __SELF__ }}::onAddItem" data-request-success="$('#inputItem').val('success')">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Tasks assigned to: {{__SELF__.name}} </h3>
</div>
<div class="panel-body">
<div class="input-group">
<input name="task" type="text" id="inputItem" class="form-control" value=""/>
<span class="input-group-btn">
<button type="submit" class="btn btn-primary">Add</button>
</span>
</div>
</div>
<ul class="list-group" id="result">
{% for task in tasks %}
<li class="list-group-item">
{{ task }}
<button class="close pull-right">×</button>
</li>
{% endfor %}
</ul>
</div>
</form>
And here is the content of /acme/demo/components/Todo.php:
<?php namespace Acme\Demo\Components;
use Cms\Classes\ComponentBase;
use Acme\Demo\Models\Task;
class Todo extends ComponentBase
{
/**
* This is a person's name.
* This variable will be available in the page as a twig variable
* #var string
*/
public $name;
/**
* The collection of tasks.
* #var array
*/
public $tasks;
public function componentDetails()
{
return [
'name' => 'Todo Component',
'description' => 'A database driven TODO list'
];
}
public function defineProperties()
{
return [];
}
public function init()
{
// This will execute when the component is first initialized, including AJAX events.
}
public function onRun()
{
$this->name = 'Meysam';
$this->tasks = Task::lists('title');
}
public function onAddItem()
{
$taskName = post('task');
$task = new Task();
$task->title = $taskName;
$task->save();
}
}
The problem is that onAddItem is never called. It seems that the form is not submitted properly when I add a new item. Does anybody know how I can fix this? I thought maybe the ajax libraries are missing, so I included {% framework %} as well, which again didn't help:
{% set tasks = __SELF__.tasks %}
{% framework %}
<form data-request="{{ __SELF__ }}::onAddItem" data-request-success="$('#inputItem').val('success')">
Please note that my model is working and $this->tasks = Task::lists('title'); returns the list of tasks.
The problem was that I should have included the jquery file as well:
<script src="{{ [
'assets/javascript/jquery.js',
]|theme }}"></script>
{% framework %}
I am having trouble displaying blob image on my web site, am using symfony 2.7.3...
i tried to encode the blob data in base 64, am able to display the base 64 value but when i embed it in nothing happens bellow is my code.
pls what am in doing wrong ??
controller
$badges = $this->getDoctrine()
->getRepository('AppBundle:News')->findAll();
$images = array();
foreach ($badges as $key => $badge) {
$images[$key] = base64_encode(stream_get_contents($badge->getImage()));
}
return $this->render('default/index.html.twig', array(
'badges' => $badges,
'images' => $images,
));
}
view
{% if badges %}
{% block badge %}
{% for key,badge in badges %}
<div style = "margin:4px;height:100px" class="thumbnail">
<div class="pull-left">
<a href=""><img alt="embeded image"src="data:image/png;base64,{{ images[key] }}" />
</div>
<div style="width:auto" class = "pull-right" >
{{ badge.title }}
</div>
</a>
</div>
{% endfor %}
{% endblock %}
{% endif %}
I want to get the id attribute from an entity using "lazy loading".
This is my controller:
public function indexAction() {
$em = $this->getDoctrine()->getManager();
$topics = $em->getRepository('BackendBundle:Topic')->findAll();
$posts = $em->getRepository('BackendBundle:Post')->findAll();
return $this->render('BackendBundle:Home:home.html.twig',
['topics' => $topics, 'posts' => $posts]
);
}
And here is my template block:
{% block article %}
{% for post in posts %}
<h3>
<a href="{{ path('backend_posts_post',
{ 'topic_id': post.topic.id, 'post_id': post.id }) }}">
{{ post.title }}
</a>
</h3>
<br>
<p>{{ post.text }}</p>
<hr>
{% endfor %}
{% endblock %}
I'm trying to get the topic id. Every post depends on a topic and should be possible to get his identifier.
Solved. I was trying to access a post whitout topic.
I'm facing a problem that I can summarize as it follows:
I have a TWIG template page like this (reg.html.twig):
{% extends "::base.html.twig" %}
{% block body %}
<ul class="tabs">
<li class="left">tab1</li>
<li class="left">tab2</li>
<li class="left">tab3</li>
<li class="right">tab4</li>
</ul>
<div class="tabs_container">
<div id="tab1" class="blocco-tab">
<form action="{{ path('AAA') }}" method="post" {{ form_enctype(form) }}>
<div id="name_field">
{{ form_row(form.name) }}
</div><!-- /name_field -->
<div id="address">
{{ form_row(form.addresses[0].road) }}
</div><!-- /address_field -->
</form>
</div>
<div id="tab2" class="blocco-tab">
<form action="{{ path('BBB') }}" method="post" {{ form_enctype(form) }}>
<div id="surname_field">
{{ form_row(form.surname) }}
</div><!-- /surname_field -->
</form>
</div>
</div> <!-- contenitore_tabs -->
{% endblock %}
Fields name, surname and addresses belong to a sample Symfony2 entity Person.
addresses is the first and only element of a collection of addresses (I need this as collection for other reasons)
The working JS file is:
jQuery(document).ready(function() {
$(".blocco-tab").hide();
$("ul.tabs li:first").addClass("active").show();
$(".blocco-tab:first").show();
$("ul.tabs li").click(function() {
$("ul.tabs li").removeClass("active");
$(this).addClass("active");
$(".blocco-tab").hide();
var activeTab = $(this).find("a").attr("href");
$(activeTab).fadeIn();
return false;
});
});
The Entity file:
class Person {
protected $name;
protected $surname;
protected $addresses;
public function __construct(){
$this->addresses = new ArrayCollection();
}
}
And in the DefaultController:
public function tab1Action(Request $request){
$person = new Person();
$address = new Address();
$addr_coll = new ArrayCollection();
$addr_coll->add($address);
$tab1_type = new Tab1Type();
$person->setAddresses($addr_coll);
$form = $this->createForm($tab1_type, $person);
if ($request->getMethod() == 'POST')
{
$form->bindRequest($request);
if ($form->isValid())
/*ecc ecc ecc*/
}
public function tab2Action(Request $request){
$person = new Person();
$tab2_type = new Tab2Type();
$form = $this->createForm($tab2_type, $person);
if ($request->getMethod() == 'POST')
{
$form->bindRequest($request);
if ($form->isValid())
/*ecc ecc ecc*/
}
Actually I took the way of having every FormType having all fields that I don't need rendered but put 'hidden' and 'property_path' => false, because I can't render only my desired fields cause the other ones will cause errors at runtime (they're null) , but I still get problems handling both cases in a joined way.
Putting every form in a different page (== different Route), with different Controllers, everything works fine, so it's not a problem related to basic use of symfony, It's the integration of N forms in a single page with JQuery UI that makes me cry.
Fixed that I have to use this tabs, how can I solve?
Do I have to make a single Action handling everything?
Do I have to make a single form?
Do I miss something?
Thanks in advance, I hope I've been clear in explaining my issue.
You just used the same variable for different forms
<div id="tab1" class="blocco-tab">
<form action="{{ path('AAA') }}" method="post" {{ form_enctype(**form1**) }}>
<div id="name_field">
{{ form_row(**form1**.name) }}
</div><!-- /name_field -->
<div id="address">
{{ form_row(**form1**.addresses[0].road) }}
</div><!-- /address_field -->
</form>
</div>
<div id="tab2" class="blocco-tab">
<form action="{{ path('BBB') }}" method="post" {{ form_enctype(**form2**) }}>
<div id="surname_field">
{{ form_row(**form2**.surname) }}
</div><!-- /surname_field -->
</form>
</div>
Try with single form
{% extends "::base.html.twig" %}
{% block body %}
<form action="{{ path('AAA') }}" method="post" {{ form_enctype(form) }}>
<ul class="tabs">
<li class="left">tab1</li>
<li class="left">tab2</li>
<li class="left">tab3</li>
<li class="right">tab4</li>
</ul>
<div class="tabs_container">
<div id="tab1" class="blocco-tab">
<div id="name_field">
{{ form_row(form.name) }}
</div><!-- /name_field -->
<div id="address">
{{ form_row(form.addresses[0].road) }}
</div><!-- /address_field -->
</div>
<div id="tab2" class="blocco-tab">
<div id="surname_field">
{{ form_row(form.surname) }}
</div><!-- /surname_field -->
</div>
</div> <!-- contenitore_tabs -->
</form>
{% endblock %}
Then you have in ocontroller aaaAction()
public function aaaAction(Request $request){
$person = new Person();
$address = new Address();
$addr_coll = new ArrayCollection();
$addr_coll->add($address);
$person->setAddresses($addr_coll);
$form = $this->createForm(new PersonType(), $person);
if ($request->getMethod() == 'POST')
{
$form->bindRequest($request);
if ($form->isValid())
/*ecc ecc ecc*/
}
and class for form builder like
class PersonType extends AbstractType {
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('name', null, array())
->add('surname', null, array())
->add('addresses', null, array())
;
}
public function getName()
{
return 'person';
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Acme\YourBundle\Entity\Person',
);
}
}
as I mentioned before I solved wrapping all tabs in a single form. Both of your solutions are ok, thank you for your time.
Linuxatico
In case anyone else has this problem, you can solve it like this (I am not sure if this is the most elegant solution but I think its better than making one big form for everything). This example mixes the profile and changePassword FOSUserBundle forms:
Inside your main show template (Profile:show.html.twig in my case):
{% if profileForm is defined %}
{% include 'MyBundle:Profile:edit.html.twig' with {'form':profileForm} %}
{% else %}
{% render 'MyBundle:Profile:edit' %}
{% endif %}
Repeat for changePassword:
{% if passwdForm is defined %}
{% include 'MyBundle:ChangePassword:changePassword.html.twig' with {'form':passwdForm} %}
{% else %}
{% render 'FOSUserBundle:ChangePassword:changePassword' %}
{% endif %}
In your controllers (add else):
if ($form->isValid()) {
.....
else {
return $this->container->get('templating')->renderResponse('FOSUserBundle:Profile:show.html.'.$this->container->getParameter('fos_user.template.engine'),
array('user' => $user, 'profileForm' => $form->createView()));
}
Add the profileForm and passwdForm accordingly. In my case the modified controllers were the ProfileController and ChangePasswordControllers (FOSUserBundle overrides).
As for your tabs, you can add javascript (or twig) to open the tab if any error is found.
Hope this helps :)