1 <?php
2 /**
3 * WhImageColumn class
4 *
5 * Displays images in a column
6 *
7 * @author Antonio Ramirez <amigo.cobos@gmail.com>
8 * @copyright Copyright © 2amigos.us 2013-
9 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
10 * @package YiiWheels.widgets.grid
11 * @uses Yiistrap.widgets.TbDataColumn
12 */
13
14 Yii::import('zii.widgets.grid.CGridColumn');
15
16 class WhImageColumn extends CGridColumn
17 {
18 /**
19 * @var array the HTML options of the image tag
20 */
21 public $imageOptions = array();
22
23 /**
24 * @var string $imagePathExpression is evaluated in every data cell and
25 * is used as the path of the image. The expression will have:
26 * <code>$row</code> the row number
27 * <code>$data</code> the data model of the row
28 * <code>$this</code> the column object
29 */
30 public $imagePathExpression;
31
32 /**
33 * @var string $emptyText renders if $imagePathExpression is null
34 */
35 public $emptyText = '';
36
37 /**
38 * @var bool $userPlaceHoldIt whether to use a bogus image from placehold.it or not. If true, will render an image
39 * from placehold.it according to the size set at $placeHoldItSize. Defaults to false, now placehold.it only grants
40 * access for certain amount of time. You need to ask for permission :(
41 */
42 public $usePlaceHoldIt = false;
43
44 /**
45 * @var bool $userPlaceKitten whether to use bogus image from placekitten.com or not. If true, will render an image
46 * from placekitten.com according to the size set at $placeKittenSize. Defaults to true (what can I say? I love kitten)
47 */
48 public $usePlaceKitten = true;
49
50 /**
51 * @var string $placeHoldItSize the size of the image to render if $imagePathExpression is null and $userPlaceHoldIt
52 * is set to true
53 */
54 public $placeHoldItSize = '48x48';
55
56 /**
57 * @var string $placeKittenSize the size of the image to render if $imagePathExpression is null and $usePlaceKitten
58 * is set to true
59 */
60 public $placeKittenSize = '48/48';
61
62 /**
63 * Renders the data cell content
64 *
65 * @param int $row the row number (zero based)
66 * @param mixed $data teh data associated with the row
67 */
68 protected function renderDataCellContent($row, $data)
69 {
70 $content = $this->emptyText;
71 if ($this->imagePathExpression && $imagePath = $this->evaluateExpression(
72 $this->imagePathExpression,
73 array('row' => $row, 'data' => $data)
74 )
75 ) {
76 $this->imageOptions['src'] = $imagePath;
77 $content = CHtml::tag('img', $this->imageOptions);
78 } elseif ($this->usePlaceHoldIt && !empty($this->placeHoldItSize)) {
79 $content = CHtml::tag(
80 'img',
81 array('src' => 'http://placehold.it/' . $this->placeHoldItSize)
82 );
83 } elseif ($this->usePlaceKitten && !empty($this->placeKittenSize)) {
84 $content = CHtml::tag('img', array('src' => 'http://placekitten.com/' . $this->placeKittenSize));
85 }
86 echo $content;
87 }
88 }