show only a fixed number of entries of a table - symfony - symfony-1.4

I've a module (named Brèves) and in the index I need to be showed to columns: the first one shows only entries with an image; the second one shows entries without image. the thing is that I only want to be show the last 4 entries in each column. How can I do it? thank you

Symfony is a web application framework written in PHP which follows the model-view-controller (MVC) paradigm.
Your controller:
In your action:
// Action (controller) - apps/frontend/modules/youmudule/actions/actions.class.php
public function executeName(sfWebRequest $request)
{
$this->entries = Doctrine_Core::getTable('YouTableName') ->getEntry();
}
Your model:
Plase where you put all your "query"
// Model - lib/model/doctrine/YourtablenameTable.class.php
public function getEntry()
{
$q = $this->createQuery('a')
->addORDERBY ('created_at DESC')
->limit(4);
return $q->execute();
}
Your view:
// apps/frontend/modules/youmudule/templates/nameSuccess.php
<?php foreach ($entries as $entry): ?>
$entry->getSomeData()
<?php endforeach; ?>
Read this tutorial

Related

Outputting A Custom Blade Partial from Sober Controller

So, for my template, I'm creating a block of code that runs custom "Modules". Basically, they are a custom post type, with fields built in ACF. Each of these modules has their own Blade template (e.g. /resources/views/modules/brandwindow.blade.php).
I've also created a Blade template for a custom post type called "Modular Page". On this template, I handle all the calls for each and any module called in a loop. I want to be able to handle this login in a controller, however. So, I created a Controller called "LoadModules" that will handle that request.
I'm having some difficulty calling a Blade template from a Controller, though. In a function GetModules(), I loop through the ACF repeater to see which module I need to load, and then supply all the relevant fields with another Controller specific to that Module (e.g. if module = 'bwindow', Call BrandWindow->BrandWindowFields). This returns an array which I can then pass into a View.
I've added the View class to the top of the Controller (like you would in Laravel), but I'm pretty sure I'm doing something wrong here. I'm getting the following error:
Class 'View' not found in ..\app\Controllers\LoadModules.php on line 25
Here's my Controller code:
public static function GetModules()
{
$brand_window_loader = new BrandWindow;
$module_array = get_sub_field('content_type');
if ($module_array == 'bwindow') {
// Do Brand Window Stuff
$windows = $brand_window_loader->GetBrandWindows();
$output = '';
foreach ( $windows as $window ){
$feild_array = $brand_window_loader->BrandWindowFields($window);
$output .= View::make('modules.brandwindow', $feild_array);
}
return $output;
} elseif ($module_array == 'blogfeed') {
// Do Blog Feed Stuff
return $module_array;
}
}
Line 25, is throwing the error which is:
$output .= View::make('modules.brandwindow', $feild_array);
The $brand_window_loader->BrandWindowFields($window) is just an array of keyed values it pulls from ACF using the ID of the post.
'title' => 'Title',
'sub_text' => 'Sub Text',
'url' => 'http://example.com'
ETC.
The error is obviously related to the "View" class not existing, so I'm wondering if I've namespaced or the View class does not exist with Sage 9/Bedrock. If that's true, what is the best way to include a Blade template from a Controller?
Thanks
You can try :
$output .= App\template('modules.brandwindow', $feild_array)

How can I pass variables to login and register view in laravel 5.3

I am learning and developing a project in laravel 5.3. So I stucked at a point that, to every view in this project I am passing variables to views like following.
public function index()
{
$page_title = 'Page Title';
return view('home', ['title' => $page_title]);
}
so in login and register controllers there are no methods to return views. And I want to pass same variables with different string values to login and registration form. so how can i do that. And secong thing I want to ask is, that how can I add a 404 error page in my project for undefined routes. and third question is that can I set 404 page to register route (www.myproject.com/register) after adding some users in my project. looking forword for reply..
The methods to return the views are in traits, if you want to add you own logic for these methods you can simply override them by adding your own methods the class that uses the trait e.g.
RegisterController
public function showRegistrationForm()
{
$title = 'Register';
return view('auth.register', compact('register'));
}
LoginController
public function showLoginForm()
{
$title = 'Login';
return view('auth.login', compact('title'));
}
If you want to add a custom 404 error page then you just need to create that a file at resources/views/errors/404.blade.php as shown in the docs https://laravel.com/docs/5.4/errors#custom-http-error-pages
Laravel comes with a RedirectIfAuthenticated middleware that will (as the name suggests) rediect the user away from a route if they are already logged in. By default, the login and register routes already have this. If you want to change this behaviour just edit your App\Http\Middleware\RedirectIfAuthenticated class.
Hope this helps!
In the login controller > AuthenticatesUsers trait you can type your variables here.
Default path: app/Http/Controllers/Auth/LoginController.php
public function showLoginForm()
{
$test = 'test';
return view('auth.login', compact('test'));
}
Optimized this could save your 'arss'
LoginController.php
public function showLoginForm()
{
$title = 'Login';
$css = array(
asset('sign-up-in/css/index.css'),
);
$js = array(
asset('sign-up-in/js/index.js'),
);
return view('auth.login', compact('title','css','js'));
}
Do the same for RegisterController.php +(bonus) every other controller
Now slay it all with this
app.blade.php
#if(isset($js))
#foreach($js as $key => $value)
<script src="{{$value}}"></script>
#endforeach
#endif
#if(isset($css))
#foreach($css as $key => $value)
<link href="{{$value}}" rel="stylesheet">
#endforeach
#endif

Concrete5 - Why is my block controller set() not working?

I have a custom block which has a default view with a form in it. When that form is submitted I set a controller flag and the block is (should be) updated to display more information.
The problem is my view is treating it like I have no data/variables set
Controller.php
public $unlocked = false;
public $employer;
public $shortname = "not loaded";
public function on_page_view() { //already overridden because I'm compiling LESS
...
$this->setViewVariables();
}
function setViewVariables() {
$this->set('shortname', $this->shortname);
$this->set('is_unlocked', $this->unlocked);
...
}
public function action_accesscode_unlock() {
$this->unlocked = true;
$this->shortname = "fred";
//Have also tried calling $this->setViewVariables(); as well,
//before I realised view() and on_page_view() were called after this anyway
}
View.php
<?php if ( !$is_unlocked ) {
echo $shortname; //does correctly display the default value
?>
<form action="<?php echo $this->action('accesscode_unlock')?>" id="accessform" method="post">
...
</form>
<?php } else {
//THIS section is never displayed (always reloads form with default name)
echo $shortname;
} ?>
What am I doing wrong here so that the new variable values are never set in the view?
Edit
After replying to JohnTheFish I just realised, the LESS compilation code I use includes the following lines (used to get block path). Could this be changing the instance used for different parts of the lifecycle?
$bv = new BlockView();
$bv->setController($this);
$bv->setBlockObject($this->getBlockObject());
on_page_view runs before action_accesscode_unlock, so the logic of action_accesscode_unlock does not happen until after the variables are set.
You could try adding a call to setViewVariables to the end of action_accesscode_unlock.
(In answer to your edit, yes, it could)

How do you create a new view in Jomsocial

I am having a hard time figuring out how the exactly the routing works in JomSocial. Does anyone know how to create a new view?
First of all, you create a controller to make a request of a view:
file: controllers/hello.php
<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();
class CommunityHelloController extends CommunityBaseController
{
function helloWorld() //index.php?option=com_community&view=hello&task=helloWorld
{
$document = JFactory::getDocument();
$viewType = $document->getType();
$view = $this->getView('hello', '' , $viewType);
echo $view->get('helloWorld');
}
function hello() //index.php?option=com_community&view=hello&task=hello
{
$document = JFactory::getDocument();
$viewType = $document->getType();
$view = $this->getView('hello', '' , $viewType);
echo $view->get('helloWorld');
}
}
?>
View: views/hello/view.html.php
Here you place variables that will be passed to template file
For example:
<?php
defined('_JEXEC') or die('Restricted access');
jimport ( 'joomla.application.component.view' );
class CommunityViewHello extends CommunityView {
function helloWorld() //This function shows a "Hello World" without an template view
{
echo 'Hello World';
}
function hello()
{
$user = CFactory::getUser($userid);
$tmpl = new CTemplate( ); //Call a template file
echo $tmpl->set ( 'user', $user )
->fetch ( 'hello' ); // Returns the templates/default/hello.php file
}
}
File templates/default/hello.php:
<?php defined('_JEXEC') or die();?>
<h2> This is an example </h2>
<div class="container">
<p> Hello, <?php echo $user->name; ?></p>
</div>
That's all!
I may have made this as a comment to the answer given by #Thavia-Farias in 2013, but my reputation is not high enough to comment. The content of my answer will restate her information along with crucial new information, corrections, and enhancements according to my experience using Jomsocial 4.2.1:
First off, the controllers/hello.php as provided by #Thavia-Farias has an error: In both the function helloWorld() and function helloWorld()function hello(), the final line is echo $view->get('helloWorld');, but the function in function hello() should be echo $view->get('hello');. As it stands, both *index.php?option=com_community&view=hello&task=helloworld and index.php?option=com_community&view=hello&task=hello will both call the helloworld view, rather than the second one calling the hello view as it should.
Also, in my experience, rather than putting the template at the path /templates/default/hello.php, I put it at /templates/customtemplatename/html/com_community/layouts if you are using a cusomt template or /components/com_community/templates/jomsocial/layouts/ if you are using the default jomsocial template.
create /components/com_community/controllers/hello.php:
<?php
defined('_JEXEC') or die();
class CommunityHelloController extends CommunityBaseController
{
public function renderView($viewfunc, $var = NULL) {
$my = CFactory::getUser();
$jinput = JFactory::getApplication()->input;
$document = JFactory::getDocument();
$viewType = $document->getType();
$viewName = $jinput->get('view', $this->getName());
$view = $this->getView($viewName, '', $viewType);
echo $view->get($viewfunc, $var);
}
function helloWorld()
{
$this->renderView(__FUNCTION__);
}
function hello()
{
$this->renderView(__FUNCTION__);
}
function display($cacheable = false, $urlparams = false) {
$this->renderView(__FUNCTION__);
}
}
?>
create /var/www/html/components/com_community/views/hello/view.html.php:
<?php
defined('_JEXEC') or die('Restricted access');
jimport ( 'joomla.application.component.view' );
class CommunityViewHello extends CommunityView {
function helloWorld() //This function shows a "Hello World" without an template view
{
echo 'Hello World';
}
function display() //This function what happens when the hello view is called without a task
{
echo 'welcome to the main landing page for the hello view! There is nothing else shown here besides this message.';
}
function hello()
{
echo $tmpl->fetch('hello');
}
}
As you can see, if you want your view to have a default view even when no task is called, similar to what happens with /index.php?option=com_community&view=groups then you will want to name a task as function display in the controller and in the view.
finally, create /components/com_community/templates/jomsocial/layouts/hello.php:
<?php defined('_JEXEC') or die();?>
<h2> This is an example </h2>
<div class="container">
<p> Hello, <?php echo $my->name; ?></p>
</div>
$my was defined way back in the controller! When your views and tasks group big enough, you will have different files for each task. The tasks files are with the fetch function in the view.html.php.
$tmpl = new CTemplate( ); //Call a template file
echo $tmpl->set ( 'vars1', $vars1)
echo $tmpl->set ( 'vars2', $vars2)
echo $tmpl->set ( 'vars3', $vars3)
->fetch ( 'hello' );
calls the /components/com_community/templates/jomsocial/layouts/hello.php file.
Using ->fetch ( 'hello.greeting' ); calls /components/com_community/templates/jomsocial/layouts/hello.greeting.php.
If you want to create a new directory for these then ->fetch ( 'hello/create' ); calls
/components/com_community/templates/jomsocial/layouts/hello/create.php
If you want to create menu items and aliases for your new components, then you need to create a new file (as well as a second and modify a third if you want to do pass menu-item defined parameters to your tasks):
create file: /components/com_community/views/hello/metadata.xml:
<?xml version="1.0" encoding="utf-8"?>
<metadata>
<view title="Groups">
<message>
<![CDATA[
Hello view
]]>
</message>
<options var="task">
<default name="Hello" msg="displays landing page" />
<option value="hello" name="- one greeting" msg="Display detail page of one greeting" />
<option value="helloWorld" name="- helloworldview" msg="Display whatever you have in the hello world task" />
</options>
</view>
<state>
<name>Hello Groups Layout</name>
<description>Hello Groups listings</description>
</state>
</metadata>
This file will add items to the "community" section of the menu in the administrator menu panel. The option values are the names of the tasks. The option without a value that is uses the default tag will pull up the display function described earlier.
If you need to add parameters to the file, then you need to do something a bit complicated:
create /components/com_community/views/hello/tmpl/default.xml:
<?xml version="1.0" encoding="utf-8"?>
<metadata>
<layout title="Name" option="View">
<message>
</message>
</layout>
<fields name="params">
<fieldset
name="basic"
label="Selected Group">
<field
name="item_id"
query="SELECT `id`, `name` FROM #__community_groups_category WHERE ORDER BY `id`"
type="sql"
key_field="id"
value_field="name"
label="Associated Group"
require="true"
description="Select the jomsocial group whose hello task this will be associated with">
</field>
</fieldset>
</fields>
</metadata>
This will create a tab wherein users can specify one group out of available groups in the database. It will assign the id of the group to the parameters field in the #__menu database table in the params colums JSON object as the value for the item_id key. In order for your view to use that value when rendering the page, include the following code in views/hello/view.html.php:
$mainframe = JFactory::getApplication();
$jinput = $mainframe->input;
$document = JFactory::getDocument();
// Get category id from the query string if there are any.
$groupId = $jinput->getInt('group', 0);
// Load the parameters.
$params = $mainframe->getParams();
$params_array = $params->toArray();
if (isset($params_array['item_id']))
{
$groupId = $params_array['item_id'];
}
In this way, your task can receive the necessary specifics from either the URL if given from within your component(option=com_community&view=hello&task=hello&groupid=5), or from being called by a main menu or jomsocial toolbar item drawing of the stored parameters in the menu database table for that menu item.
The options and tab that you create here will be visible for all menu items of this task. If you want different tabs for different menu options, you will have to create entirely different views. having everything in one view may lead to unused and potentially misleading tabs where values can be set by your users but that will not or should not be used by the actual task specified by the user.
Forgive me for not testing every line of this code in an integrated component. I have done all of these functions in my view but have abridged my code which was built with initial guidance from #Thavia-Farias's answer. While it is more clear than posting my extensive code, it has not been tested in it's current form for functionality. Be sure to check your php error logs to debug your project. I have to log in as root (sudo su) and check with nano /var/log/mysqld/error_log on my system. Good luck!

How to create a view using code in Drupal 7?

We can create a view from admin panel. But I want to create a view using php code. Can anyone show me the way?
There is code floating around that wouldn't work for me. But This one did. Add this php to your .module file. Then create a views folder and then put all your views in there with the extension of .inc. Each view file will simply be <?php followed by the exact export of the view...
/**
* Implements hook_views_api().
*/
function MODULENAME_views_api() {
return array ('api' => 3.0);
}
function MODULENAME_views_default_views() {
// Check for all view file in views directory
$files = file_scan_directory(drupal_get_path('module', 'MODULENAME') . '/views', '/.*\.inc$/');
// Add view to list of views
foreach ($files as $filepath => $file) {
require $filepath;
if (isset($view)) {
$views[$view->name] = $view;
}
}
// At the end, return array of default views.
return $views;
}

Resources