Only show todays news - Silverstripe - php

Hello Silverstripe Specialists!
I made the tutorial "extending a basic site"
(http://doc.silverstripe.org/en/tutorials/extending_a_basic_site)
That all worked very well so far.
I made this to show the latest news on the HomePage:
In HomePage.php:
// ...
public function LatestNews($num=5) {
$holder = ArticleHolder::get()->First();
return ($holder) ? ArticlePage::get()->filter('ParentID',
$holder->ID)->sort('Date DESC')->limit($num) : false;
}
And this in HomePage.ss:
// ...
public function LatestNews($num=5) {
$holder = ArticleHolder::get()->First();
return ($holder) ? ArticlePage::get()->filter('ParentID',
$holder->ID)->sort('Date DESC')->limit($num) : false;
}
That works very well!
Now my Question: All my News have a Date-Field. Is it possible to show only
the News of the current Date on the HomePage?
I tried this, but this wont work (Server Error) (Datum is my Date of the News):
public function LatestNews($num) {
$holder = ArticleHolder::get()->First();
return ($holder) ? ArticlePage::get()->filter('ParentID', "datum == CURDATE()",
$holder->ID)->sort('Date DESC')->limit($num) : false;
}
Thank you very much for your help!

filter() needs either two values (column and value) or an array of key-value pairs of what to filter. So if you want to filter for more than one thing you need an array as parameter:
$today = date('Y-m-d');
$todaysNews = ArticlePage::get()->filter(array(
'ParentID' => $holder->ID,
'datum' => $today
));
This will return a DataList you can sort and limit like you did in your example.
See also in docs:
Data Model and ORM general overview
Search filters how to filter "greater than" etc...
EDIT:
So a method in your controller could look like:
public function getTodaysNews($num=5) {
$holder = ArticleHolder::get()->First();
$today = date('Y-m-d');
return $holder
? ArticlePage::get()->filter(array(
'ParentID' => $holder->ID,
'datum' => $today
))->sort('Date DESC')->limit($num)
: false;
}

Related

How to sort and filter with checkbox in yii

I have gridview with filters+checkboxes like below:
Gridview is filtered when checkbox is checked and its working v. nice but when i'm clicking on the column name to sort columns sort is taking controll over the filters and they just stop working...
I cant even unclick checkboxes and change values in filters inputs cause "sort" keeps values from before i clicked column name.
Is there any way to repair this?
My search function in model looks like below:
public function Search(): CActiveDataProvider
{
$request = Yii::app()->request;
$requestusr = $request->getParam('Users');
$this->xyz_name_filter = isset($requestusr['xyz_name_filter']) ? $requestusr['xyz_name_filter'] : 1;
$this->xyz_surname_filter = isset($requestusr['xyz_surname_filter']) ? $requestusr['xyz_surname_filter'] : 1;
$this->xyz_street_filter = isset($requestusr['xyz_street_filter']) ? $requestusr['xyz_street_filter'] : 0;
$criteria = new CDbCriteria();
$this->xyz_name_filter == 1 ? $criteria->compare('t.xyz_name', $this->xyz_name, true) : null;
$this->xyz_surname_filter == 1 ? $criteria->compare('t.xyz_surname', $this->xyz_surname, true) : null;
$this->xyz_street_filter == 1 ? $criteria->compare('t.xyz_street', $this->xyz_street, true) : null;
return new CActiveDataProvider(
$this, array(
'criteria' => $criteria,
'pagination' => array(
'pageSize' => $this->pagesize,
),
)
);
}
Thanks for reply.
Please debug the code what you see in the backed when clicking to sort with some value and add this to your code.
For example, the request came as $requestusr['xyz_name_filter_ASC']
And add this in your code
$this->xyz_name_filter_ASC == 1 ? $criteria->order = 't.xyz_name ASC';
In your code you have not any order
The problem wos in config...
adding line:
'admin/<lang:[a-z]{2}>/<controller:\w+>/<action:\w+>'=>'admin/<lang>/<controller>/<action>'
solves the problem.

Filtering in date range

I try to build custom search form and filter results in some range of dates:
$form = BootstrapForm::create (
$this,
'LetterSearchForm',
FieldList::create (
DateField::create('Sent_After','Sent After'),
DateField::create('Sent_Befor','Sent Before')
...
),
...
);
public function index (SS_HTTPRequest $request)
{
$letters = Letter::get()->sort('DateUpload');
if($search = $request->getVar('Sender')) {
$letters = $letters->filter(array(
'Sender:PartialMatch' => $search
));
}
if ( $search1 = $request->getVar('Sent_After') && $search2 = $request->getVar('Sent_Befor'))
{
What must be here?
}
}
}
Can I use here something like WithinRangeFilter?
I don't know of any range filter, but according to the documentation you could use something like:
$dateFilteredList = $letters->filter(array(
'DateUpload:LessThanOrEqual' => $search2, // Sent_Befor
'DateUpload:GreaterThanOrEqual' => $search1 // Sent_After
));
I made a few assumptions with the code above:
DateUpload is the date field you're trying to filter on.
The date format supplied by the form is already database friendly, ie yyyy-mm-dd (eg 2017-09-05)
You may need to edit it accordingly.
Hope that is what you're looking for :)

Laravel Eloquent Array Loop

I have an eloquent query that is returning a 'show', and any 'show_ref's relating to it:
Show::where('show_slug','=',$id)->with('showrefs')->firstOrFail();
This brings back a nested array successfully, as follows:
{
"show_id":2,
"show_name":"ShowNo1",
"show_logo":null,
"show_facebook_url":null,
"show_twitter_url":null,
"show_website_url":null,
"show_slug":"shown1",
"created_at":"2014-09-27 11:57:32",
"updated_at":"2014-09-27 11:57:32",
"showrefs":[
{
"show_ref":1,
"show_id":2,
"show_start_date":"2014-05-16",
"show_end_date":"2014-05-21",
"show_year":2014,
"show_common_name":"Mid May",
"venue_id":null,
"created_at":"2014-09-27 12:17:40",
"updated_at":"2014-09-28 06:26:54"
},
{
"show_ref":2,
"show_id":2,
"show_start_date":"2014-05-23",
"show_end_date":"2014-05-28",
"show_year":2014,
"show_common_name":"Late May",
"venue_id":null,
"created_at":"2014-09-27 12:21:24",
"updated_at":"2014-09-28 06:26:58"
},
{
"show_ref":4,
"show_id":2,
"show_start_date":"2013-02-12",
"show_end_date":"2013-02-13",
"show_year":2013,
"show_common_name":"Early February",
"venue_id":null,
"created_at":"2014-09-28 17:50:02",
"updated_at":"2014-09-28 17:50:02"
},
{
"show_ref":5,
"show_id":2,
"show_start_date":"2013-04-27",
"show_end_date":"2013-04-28",
"show_year":2013,
"show_common_name":"Late April",
"venue_id":null,
"created_at":"2014-09-28 17:50:42",
"updated_at":"2014-09-28 17:50:42"
}
]
}
What I am trying to get from my view is the following format:
ShowNo1 (show name)
2014
list of any shows in 2014
2013
List of any shows in 2013
Is there anyway this is possible from what I have coded so far? or have I gone about this the wrong way?
Thanks.
You have a showrefs function in your Show model somewhere that is defining this relationship. #Armon's answer will work, but will run on the order of O(n * years) where years is the number of years you want to show. Your example only has 2, but could be many more, and you can instead display your data in O(n) time.
You can orderBy in the methods defining relationships.:
// app/models/Show.php
public function showrefs()
{
return $this->hasMany('ShowRef')->orderBy('show_start_date', 'DESC');
}
If the tables you are joining together both contain a column which you want to order by, you may need to qualify the order by by appending the table name, as in:
->orderBy('`show_refs`.`show_start_date`', 'DESC')
After that, its just a matter of keeping track of the current year and noticing when it changes when looping over your data:
$show = Show::where('show_slug','=',$id)->with('showrefs')->firstOrFail();
$currentYear = '';
foreach($show->showrefs as $showref) {
$showRefYear = strtok($showref->show_start_date, '-');
if($currentYear != $showRefYear) {
$currentYear = $showRefYear;
// Output Year heading
}
// Output show details
}
Without knowing anything about Eloquent, you can use a simple function to walk through your nested array and collect your shows for a given year:
function collect_shows_with_year($shows, $year)
{
$result = array();
foreach($shows["showrefs"] as $show_ref)
{
if($show_ref["show_year"] == $year)
{
$result[] = $show_ref;
}
}
return $result;
}
$shows_in_2014 = collect_shows_with_year($shows, 2014);
$shows_in_2013 = collect_shows_with_year($shows, 2013);

How to show zend framework data grid data sorted by "date" as default?

How to show zend framework data grid data sorted by "date" as default?
When i get on the page, i wish to see data grid sorted by time as default, without getting params in URL like .../order/created_DESC/
$testimonials = new Application_Model_DbTable_Testimonials();
$source = new Bvb_Grid_Source_Zend_Table($testimonials);
Thanks.
I solved this by passing to datagrid $select instead of $object
$testimonials = new Application_Model_DbTable_Testimonials();
$source = new Bvb_Grid_Source_Zend_Table($testimonials);
is now
$testimonials = new Application_Model_DbTable_Testimonials();
$testimonials->setSortOrder('created');
$source = new Bvb_Grid_Source_Zend_Select($testimonials->getTestimonials());
It's hard to see what your class is doing since you didn't posted that one.
But when it's usess the fetchAll() function you can do two things:
Option 1
When there is an fetchAll()-call in the Grid class you can make this one:
$testimonialsTable->fetchAll(null, 'date'));
Option 2
You can re-write the fetchAll-Method in the Application_Model_DbTable_Testimonials class, when you make
public function fetchRow($where = null, $order = null, $offset = null)
{
if ($order===null) : $order='date'; endif;
return parent::fetchAll(where, $order, $offset)
}
Please notice in the last example fetchess always will be default sorted by the date-field.

Symfony : error while accessing an array

i want to output an array containing numbers.
I'm creating the array like this (it recieved the statistics for the last 7 days) :
<?php public function getStatisticsTeams()
{
$tab = array();
for($i=7;$i=0;$i--)
{
$q = Doctrine_Query::create()
->from('stJob j')
->where('j.created_at = ?', date('Y-m-d h:i:s' , time() - 86400 * $i ))
->execute()
->count();
$tab[] = $q;
}
return $tab;
}
action.class.php
$this->st_job = Doctrine::getTable('StJob')->getStatisticsTeams();
Use of the array in my template.php :
$chart->inlineGraph(array('hits' => $st_job), array('Monday', 'Tuesday', 'Wednesday' ....), 'div_id');
When i try to access to my array it fails because the function i use must have an array which is supposed to contain for example (43,5,87,3,29,8,10) , and when i var_dump($st_job) (my array)
object(sfOutputEscaperArrayDecorator)#363 (3) { ["count":"sfOutputEscaperArrayDecorator":private]=> int(0) ["value":protected]=> array(0) { } ["escapingMethod":protected]=> string(16) "esc_specialchars" }
Do you have any idea of what i'm doing wrong ?
Thank you
Yes, symfony is set to automatically apply escaping strategies to the data you pass from your controllers to your views. You can either remove the setting, which is not recommended, or use:
$original_array = $sf_data->getRaw('st_job');

Categories