1 <?php
2
3 4 5 6 7 8 9 10
11
12 13 14 15 16 17
18 class P3Media extends BaseP3Media
19 {
20
21 const TYPE_FILE = 1;
22 const TYPE_FOLDER = 2;
23
24 public $treeParent;
25
26
27 public static function model($className = __CLASS__)
28 {
29 return parent::model($className);
30 }
31
32 public function init()
33 {
34 return parent::init();
35 }
36
37 public function __toString()
38 {
39 return (string)$this->title;
40 }
41
42 public function rules()
43 {
44 return array_merge(
45
46 parent::rules()
47 );
48 }
49
50 public function relations()
51 {
52 return array(
53 'metaData' => array(self::HAS_ONE, 'P3MediaMeta', 'id'),
54 );
55 }
56
57 public function getP3MediaMeta()
58 {
59 return $this->metaData;
60 }
61
62 public function get_label()
63 {
64 return $this->title;
65 }
66
67 public function behaviors()
68 {
69
70
71 if (Yii::app() instanceof CConsoleApplication) {
72 $return = array_merge(
73 array(
74 'MetaData' => array(
75 'class' => 'P3MetaDataBehavior',
76 'metaDataRelation' => 'metaData',
77 )
78 ), parent::behaviors()
79 );
80 }
81 else {
82 $return = array_merge(
83 array(
84 'UploadBehaviour' => array(
85 'class' => 'P3FileUploadBehavior',
86 'dataAlias' => Yii::app()->getModule('p3media')->dataAlias,
87 'trashAlias' => Yii::app()->getModule('p3media')->dataAlias . ".trash",
88 'dataSubdirectory' => Yii::app()->user->id,
89 'uploadInstance' => 'fileUpload',
90 ),
91 'MetaData' => array(
92 'class' => 'P3MetaDataBehavior',
93 'metaDataRelation' => 'metaData',
94 )
95 ), parent::behaviors()
96 );
97 }
98
99 return $return;
100 }
101
102 public function search()
103 {
104 $criteria = new CDbCriteria;
105 $criteria->with = 'metaData';
106
107 if ($this->treeParent == "_ROOT") {
108 $criteria->addCondition('metaData.treeParent_id IS NULL');
109 }
110 elseif ($this->treeParent !== null) {
111 $criteria->compare('metaData.treeParent_id', $this->treeParent, true);
112 }
113
114 $criteria->compare('t.id', $this->id);
115 $criteria->compare('title', $this->title, true);
116 $criteria->compare('description', $this->description, true);
117 $criteria->compare('t.type', $this->type);
118 $criteria->compare('path', $this->path, true);
119 $criteria->compare('md5', $this->md5, true);
120 $criteria->compare('originalName', $this->originalName, true);
121 $criteria->compare('mimeType', $this->mimeType, true);
122 $criteria->compare('size', $this->size);
123
124
125
126 return new CActiveDataProvider(
127 get_class($this),
128 array(
129 'criteria' => $criteria,
130 'sort' => array(
131 'defaultOrder' => 't.id DESC',
132 ),
133 ));
134 }
135
136 public function image($preset = null)
137 {
138 return CHtml::image(
139 $this->createUrl($preset), $this->title);
140 }
141
142 public function createUrl($preset = null)
143 {
144 return Yii::app()->controller->createUrl(
145 '/p3media/file/image', array('id' => $this->id, 'preset' => $preset));
146 }
147
148 public function getFolderItems()
149 {
150 $criteria = new CDbCriteria();
151 $criteria->order = "title";
152 if ($this->id === null) {
153 $criteria->condition = "t.type = " . P3Media::TYPE_FOLDER . " AND metaData.treeParent_id IS NULL";
154 }
155 else {
156 $criteria->condition = "t.type = " . P3Media::TYPE_FOLDER . " AND metaData.treeParent_id = " . $this->id;
157 }
158 $models = P3Media::model()->with('metaData')->findAll($criteria);
159 $items = array();
160 foreach ($models AS $model) {
161 if ($model->getFolderItems() === array()) {
162 $items[] = array('label' => $model->title, 'url' => array("", "id" => $model->id));
163 }
164 else {
165 $items[] = array('label' => $model->title, 'url' => array("", "id" => $model->id),
166 'items' => $model->getFolderItems());
167 }
168 }
169 return $items;
170 }
171 }
172