22
33use ChannelData \Base \BaseModel ;
44use ChannelData \Model \Channel ;
5+ use ChannelData \Model \ChannelField ;
56use ChannelData \Component \ChannelQueryBuilder ;
67use ChannelData \Component \QueryString ;
78use ChannelData \Component \ChannelEntriesApi ;
9+ use ChannelData \Component \ChannelEntriesParser ;
810
911// namespace ChannelData;
1012
@@ -65,7 +67,7 @@ class ChannelModel extends BaseModel {
6567 public function __construct ($ data = array ())
6668 {
6769 ee ()->lang ->loadfile ('content ' );
68-
70+
6971 $ this ->uidField = $ this ->prefix .'uid ' ;
7072 $ this ->required [] = $ this ->prefix .'uid ' ;
7173
@@ -111,6 +113,11 @@ public function hasErrors()
111113
112114 public function channel ()
113115 {
116+ if (!$ this ->channel )
117+ {
118+ return FALSE ;
119+ }
120+
114121 $ channel = Channel::findByName ($ this ->channel );
115122
116123 if (!$ channel )
@@ -137,7 +144,7 @@ public function delete()
137144 }
138145
139146 protected function _createRecord ($ data = array ())
140- {
147+ {
141148 if (count ($ data ))
142149 {
143150 $ this ->fill ($ data );
@@ -150,6 +157,10 @@ protected function _createRecord($data = array())
150157 {
151158 $ this ->errors = $ this ->apiResponse ;
152159 }
160+ else
161+ {
162+ $ this ->exists = TRUE ;
163+ }
153164
154165 return $ this ;
155166 }
@@ -160,9 +171,9 @@ protected function _updateRecord($data)
160171 {
161172 $ this ->fill ($ data );
162173 }
163-
174+
164175 $ this ->apiResponse = ChannelEntriesApi::update_entry ($ this ->channel_id , $ this ->entry_id , $ this ->data ());
165-
176+
166177 if (!is_int ($ this ->apiResponse ))
167178 {
168179 $ this ->errors = $ this ->apiResponse ;
@@ -182,10 +193,14 @@ public function data()
182193 if (isset ($ data [$ row ->field_name ]) && $ value = $ data [$ row ->field_name ])
183194 {
184195 unset($ data [$ row ->field_name ]);
185-
186- $ data ['field_id_ ' .$ row ->field_id ] = $ value ;
187- $ data ['field_ft_ ' .$ row ->field_id ] = $ row ->field_fmt ;
188196 }
197+ else
198+ {
199+ $ value = NULL ;
200+ }
201+
202+ $ data ['field_id_ ' .$ row ->field_id ] = $ value ;
203+ $ data ['field_ft_ ' .$ row ->field_id ] = $ row ->field_fmt ;
189204 }
190205
191206 return $ data ;
@@ -206,14 +221,32 @@ public function fields()
206221 return $ this ->fields ;
207222 }
208223
209- foreach ( $ this ->channel ()-> fields ()-> items () as $ field )
224+ if ( $ channel = $ this ->channel ())
210225 {
211- $ this ->fields [$ field ->field_id ] = $ field ->field_name ;
226+ foreach ($ channel ->fields ()->items () as $ field )
227+ {
228+ $ this ->fields [$ field ->field_id ] = $ field ->field_name ;
229+ }
230+ }
231+ else
232+ {
233+ foreach (ChannelField::all ()->items () as $ field )
234+ {
235+ $ this ->fields [$ field ->field_id ] = $ field ->field_name ;
236+ }
212237 }
213238
214239 return $ this ->fields ;
215240 }
216241
242+ public static function findOpen ($ entry_id )
243+ {
244+ return self ::where ('channel_data.entry_id ' , '= ' , $ entry_id )
245+ ->where ('status ' , '= ' , 'open ' )
246+ ->get ()
247+ ->first ();
248+ }
249+
217250 public static function findByAuthorId ($ author_id )
218251 {
219252 return self ::where ('author_id ' , '= ' , $ author_id )->get ();
@@ -233,6 +266,11 @@ public static function channelId()
233266 {
234267 $ class = new static ;
235268
269+ if (!$ class ->channel )
270+ {
271+ return FALSE ;
272+ }
273+
236274 return Channel::findByName ($ class ->channel )->id ();
237275 }
238276
@@ -250,28 +288,27 @@ public static function query()
250288
251289 $ query ->from ('channel_titles ' );
252290 $ query ->leftJoin ('channel_data ' , 'channel_titles.entry_id ' , '= ' , 'channel_data.entry_id ' );
253- $ query ->select (array (
254- 'entry_id ' ,
255- 'site_id ' ,
256- 'channel_id ' ,
257- 'author_id ' ,
258- 'title ' ,
259- 'url_title ' ,
260- 'status ' ,
261- 'entry_date ' ,
262- 'expiration_date ' ,
263- ), NULL , 'channel_titles ' );
291+ $ query ->select ('* ' , NULL , 'channel_titles ' );
264292
265293 foreach ($ class ->fields () as $ field_id => $ field_name )
266294 {
267295 $ query ->select ('field_id_ ' .$ field_id , $ field_name , 'channel_data ' );
268296 }
269297
270- if ($ class ->channelId ())
298+ if ($ class ->channelId ())
271299 {
272300 $ query ->where ('channel_data.channel_id ' , '= ' , $ class ->channelId ());
273301 }
274302
275303 return $ query ;
276304 }
277- }
305+
306+ public function entries ()
307+ {
308+ $ parser = new ChannelEntriesParser ();
309+
310+ return $ parser ->entries (array (
311+ 'entry_id ' => $ this ->id ()
312+ ));
313+ }
314+ }
0 commit comments