I have pagination with links [1, 2, 3, 4, 5, 6, 7, 8 ... 99, 100] and how can I change limit to display [1, 2, 3, ... 98, 99, 100] ? I have custom paginator class, but I can't find this limit to override in my new class.
By checking the classes I found that you have to override the presenter used by the Paginator.
Its done by calling render($presenter) your presenter must extend BootstrapThreePresenter If you wish to use bootstrap links and you just have to override the constructor and pass number of links you want on each side $this->window = UrlWindow::make($numberOfLinksEachSide)
These are just instructions you'll have to look by yourself, I'm sorry for not being able to provide complete code, I'm on phone.
Please let me know if this worked.
This is my solution to the same problems... In LengthAwarePaginator updated function links:
public function links($view = null, $data = [], $onEachSide = 3)
{
if(!$data){
$data = [];
}
$this->onEachSide = $onEachSide;
return $this->render($view, $data);
}
And in URLWindow function make:
public static function make(PaginatorContract $paginator)
{
return (new static($paginator))->get($paginator->onEachSide);
}
This removes the parameter $onEachSide from function make - which is never passed in anywhere - and allows it to be passed to function links as a parameter.
To use this you need to call this links method like this:
{{ $collection->links('view-to-use'|null, $dataArray|null, 2)}}
Where 2 is the number on each side.
You can do this easily by changing some core fields (although not recommended to change core files).
Find- vendor/laravel/framework/src/Illuminate/Pagination and go to UrlWindow. On this page find some parameters like- $onEachSide, $window. Change and play with these.
Related
Suppose I have page for searching cars, page takes 3 optional parameters, brand, year and color
Simplified route example:
Route::get('/cars/{brand?}/{year?}/{color?}', function ($brand = NULL, $year = NULL, $color = NULL) {
echo "brand is:".$brand."<br>";
echo "year is:".$year."<br>";
echo "color is:".$color."<br>";
});
I don't realise how to pass for example only year parameter?
Works if passed all of 3 parameters, for example: /cars/_/2010/_ but this is very inelegant solution.
What is proper way for this ?
I don't know if this is possible since you may end up passing only 2 parameters and Laravel wouldn't be able to understand if this is brand, color or year.
I will leave my two cents regarding on my method of URL parameters that I use:
public function getCars(Request $request){
Validator::validate($request->all(), [
'brand' => 'nullable|string',
'year' => 'nullable|integer',
'color' => 'nullable|string'
]);
$cars = Car::select('id', '...');
if($request->has('brand')){
// get cars with that brand
$cars->where('brand', $request->brand);
}
// ... and so on with the other parameters
$cars = $cars->paginate(10); // or $cars->get()
}
This is a fairly simple example so you will have to customize to your needs. Hope that helps.
As the official documentation says, Route parameters are injected into route callbacks / controllers based on their order. In this specific case, the only way Laravel has to know which is each parameter is like you suggest (see https://laravel.com/docs/5.6/routing#route-parameters).
Anyway, if 3 parameters are required to perform a search, you could probably think of changing the request verb from GET to POST, and pass all of them as POST request data instead of in the query string itself.
I'm calling these 3 functions one after other in this exact order
public function setPrintFitToWidth()
{
$this->sheet->getPageSetup()->setFitToWidth(1);
}
public function setPrintArea($cell_area)
{
$this->sheet->getPageSetup()->setPrintArea($cell_area);
}
public function setPrintMargins($top, $right, $bottom, $left)
{
$this->sheet->getPageMargins()->setTop($top);
$this->sheet->getPageMargins()->setRight($right);
$this->sheet->getPageMargins()->setLeft($left);
$this->sheet->getPageMargins()->setBottom($bottom);
}
The problem is that, opening resulting Excel file, I've page margin set to 'custom' but, in fact, set to different values instead of margin I passed to my function. In fact I called with argument (1,0.5,0.5,1) but I got, in the same orders, 2, 0.8, 0.8, 2. It's really strange ...
Also: I cannot get working setFittoWidth(1); I expect to see adapted for all column in one page, but Excel tell me It's setup on adapt sheet on a page.
What am I doing wrong?
Resolved:
changed
public function setPrintFitToWidth()
{
$this->sheet->getPageSetup()->setFitToWidth(1);
}
to
public function setPrintFitToWidth()
{
$this->sheet->getPageSetup()->setFitToWidth(1);
$this->sheet->getPageSetup()->setFitToHeight(0);
}
About the margins: I tried with zero and margin are respected, so I concluded than PHPExcel unit are in someway 'scaled down'... So, after some 'try' and 'redo', I found the values that generate the correct magins
I followed this tutorial to set up my back end server based on Yii framework and then this tutorial to set up my API and everything is working as it should.
But I am not sure how to accomplish the next step:
My return array right now is pulling records that look like this:
{
"id": 1,
"user_id": 1,
"description": "This is a summary of article 1",
"status": 2,
"type": 1,
"created_at": 1426210780,
"updated_at": 1426365319
}
The value for 'type' is '1' in the db, but I want to store all the possible values of 'type' in another table like this:
1 : Red
2 : Blue
3 : Green
And then I want the JSON returned via my API to contain "type":'red' instead of "type":1. I assume I need to override something in my Model, but I can't figure out what to override or with what.
I'm happy to read through tutorials or documentation but I'm such a beginner at this that I'm not sure what terms to search for. Thanks for your help!
Have a look at models and their relationships to other models, this will allow you to get the information you need.
http://www.yiiframework.com/doc/guide/1.1/en/database.arr
Once the relationship is working correctly you should be able to get the colour from the original model.
Although this is from a earlier version of Yii it may help you understand how the models will interact as well
http://www.yiiframework.com/wiki/285/accessing-data-in-a-join-table-with-the-related-models/
#Burrito's response provided documentation but I want to give the full solution for other searchers:
First, I needed to set up a model for 'Type'.
Second, I needed to declare the relationship between 'Report' (my main model) and 'Type' like this (in my Report model):
public function getType()
{
return $this->hasOne(Type::className(), ['id' => 'type']);
}
(I'm not sure if that step is necessary, but the documentation makes it seem necessary.)
Third, I created getTypeName (in Report model), to get the name of the type based on the ID:
public function getTypeName($type = null)
{
return Type::findOne($type)->name;
}
Lastly, in my apiController, I modified the function that I am using to get all records to include a loop for each record that called getTypeName:
protected function findAllReports()
{
// get all reports
$reports = Report::find()
->asArray()
->all();
if( $reports ){
$i = 0;
// loop through each report
foreach($reports as $report){
$model = new Report();
// add a new key/value pair to each report array, populate with getTypeName and pass the type ID to it as a parameter
$reports[$i]['typeName'] = $model->getTypeName($reports[$i]['type']);
$i++;
}
return $reports;
} else {
// error or no results
}
}
For reference, the other routine needed here is the action that the API hits, which calls findAllReports():
public function actionList()
{
$reports=$this->findAllReports();
$this->setHeader(200);
echo json_encode(array('status'=>1,'data'=>$reports),JSON_PRETTY_PRINT);
}
Finally, now if I called [url]/api/list, I get an array of reports, including the typeName.
I'd like to be able to return an image in Black&white in a controller, so I can use it in a template. On this page I found that the GD class has a greyscale method. Unfortunately I don't understand the GD class and how I can use it. I tried doing
$final = $image->getFormattedImage('greyscale',36,36,36);
But that didn't work. It does return an image object with a new URL but the image does not exist.
Can anyone explain to me how to make an imageobject into a greyscale image in a Silverstripe page Controller?
Well I had a go myself and this is what I came up with:
_config.php
Object::add_extension('Image', 'Greyscaled');
UPDATE: as of SilverStripe 3.1, you should use the config system instead of _config.php. Put the following in your mysite/_config/config.yml (Don't forget to ?flush=1 to reload the config cache after adding it):
Image:
extensions:
- 'Greyscaled'
Greyscaled.php
<?php
class Greyscaled extends DataExtension {
//This allows the template to pick up "GreyscaleImage" property, it requests a copy of the image from the cache or if it doesn't exist, generates a new one
public function GreyscaleImage($RGB = '76 147 29') {
return $this->owner->getFormattedImage('GreyscaleImage', $RGB);
}
//This is called internally by "generateFormattedImage" when the item is not already cached
public function generateGreyscaleImage(GD $gd, $RGB) {
$Vars = explode(' ', $RGB);
return $gd->greyscale($Vars[0], $Vars[1], $Vars[2]);
}
}
UPDATE2: With newer Versions of 3.1 ?? you can pass in more than 2 parameters and GD has been renamed to Image_Backend. This way you do not have spaces between the RGB-values in the image-name. Be aware $gd->greyscale needs a lot of juice - so you probable better downsize first and GreyscaleImage afterwards.
UPDATE3: Since this answer got some votes recently I assume people still using it, but I think in 2017 CSS filters are in many cases a better choice. Prefixed you'll have close to 90% coverage.
css-filters on caniuse.com
<?php
class Greyscaled extends DataExtension {
public function GreyscaleImage($R = '76', $G = '147', $B = '29') {
return $this->owner->getFormattedImage('GreyscaleImage', $R, $G, $B);
}
public function generateGreyscaleImage(Image_Backend $gd, $R, $G, $B) {
return $gd->greyscale($R, $G, $B);
}
}
and in the template:
<img src="$Images.GreyscaleImage.CroppedImage(1000,400).URL" alt="$Images.Title" />
Silverstripe 3.1 Image API
There is a module for this. Sorry but it's not on packagist just yet.
https://github.com/NightJar/ssrigging-greyscaleimages
I am using Yii Booster, and one of the widgets is TbTotalSumColumn.
When it renders the total in the footer, it uses the following code:
echo $this->totalValue? $this->evaluateExpression($this->totalValue, array('total'=>$this->total)) : $this->grid->getFormatter()->format($this->total, $this->type);
I have used CFormatter and created a 'currency' type, I have applied the formatting directly in the 'value' attribute, I have gone into the widget and applied the currency formatter there. It seems no matter what I do, I can only get either the values in the column to be formatted as currency, or the footer, never both.
Any help would be greatly appreciated.
I created a new class file in the components folder called TbTotalColumnCurrency.php. Then I call TbTotalSumColumnCurrency in my TbExtendedGridView code.
Yii::import('bootstrap.widgets.TbTotalSumColumn');
class TbTotalSumColumnCurrency extends TbTotalSumColumn
{
protected function renderFooterCellContent()
{
if(is_null($this->total))
return parent::renderFooterCellContent();
echo $this->totalValue? $this->evaluateExpression($this->totalValue, array('total'=>number_format($this->total), 2, '.', '')) : $this->grid->getFormatter()->format(number_format($this->total, 2, '.', ''), $this->type);
}
}
Hope this helps
array(
'name'=>'Total',
'type'=>'text',
'value'=>'number_format($data->price*$data->quantity, 2, \'.\', \'\')',
'class'=>'TbTotalSumColumnCurrency'
),