1 <?php
2 /**
3 * Created by JetBrains PhpStorm.
4 * User: tobias
5 * Date: 25.02.13
6 * Time: 17:11
7 * To change this template use File | Settings | File Templates.
8 */
9 class P3MediaSelect extends CWidget
10 {
11 public $model;
12 public $attribute;
13
14 function run()
15 {
16 echo CHtml::activeTextField($this->model,
17 $this->attribute,
18 array(
19 'data-init-text' => ($this->getMediaModel())?$this->getMediaModel()->title:null
20 )
21 );
22
23 $id = get_class($this->model) . "_" . $this->attribute;
24 $this->widget('ESelect2',
25 array(
26 'selector' => "#" . $id,
27 'options' => array(
28 'width' => '100%',
29 'height' => '500px',
30 'placeholder' => 'Search Media File',
31 'minimumInputLength' => 0,
32 'ajax' => array(
33 'url' => Yii::app()->controller->createUrl('/p3media/p3Media/ajaxSearch'),
34 'dataType' => 'jsonp',
35 'data' => 'js: function(term,page) {
36 return {
37 q: term,
38 page_limit: 10,
39 };
40 }',
41 'results' => 'js: function(data,page){
42 return {results: data};
43 }',
44 ),
45 'formatResult' => 'js:function(data){
46 var markup = "<table class=\"data-result\"><tr>";
47 if (data.image !== undefined) {
48 markup += "<td class=\"data-image\">" + data.image + "</td>";
49 }
50 markup += "<td class=\"data-info\"><div class=\"data-title\">" + data.title + "</div>";
51 markup += "</td></tr></table>";
52 return markup;
53 }',
54 'formatSelection' => 'js: function(data) {
55 return data.title;
56 }',
57 'initSelection' => 'js: function(element, callback) {
58 var elementText = $(element).data("init-text");
59 callback({"title":elementText});
60 }'
61 ),
62 ));
63 }
64
65 private function getMediaModel()
66 {
67 $model = P3Media::model()->findByPk($this->model->{$this->attribute});
68 return $model;
69 }
70 }
71