public
Description: Wilfire CMS - Plugin for the PHP-Wax Framework
Homepage: http://wildfirecms.com/
Clone URL: git://github.com/phpwax/wildfire.git
wildfire / lib / model / CmsContent.php
100755 261 lines (235 sloc) 10.116 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
 
class CmsContent extends WaxModel {
 
public function setup(){
$this->define("title", "CharField", array('maxlength'=>255) );
$this->define("excerpt", "TextField");
$this->define("content", "TextField");
 
$this->define("status", "IntegerField", array('maxlength'=>2, "widget"=>"SelectInput",
"choices"=>array(0=>"Draft",1=>"Published",3=>"Temporary",4=>"Preview")));
$this->define("published", "DateTimeField");
$this->define("expires", "DateTimeField");
$this->define("date_modified", "DateTimeField", array("editable"=>false));
$this->define("date_created", "DateTimeField", array("editable"=>false));
$this->define("sort", "IntegerField", array('maxlength'=>3, "editable"=>false));
$this->define("pageviews", "IntegerField", array('maxlength'=>11, "editable"=>false));
$this->define("url", "CharField", array('maxlength'=>255, "editable"=>false));
 
//images
$this->define("images", "ManyToManyField", array('target_model'=>"WildfireFile", 'editable'=>false, "eager_loading"=>true, "join_model_class"=>"WaxModelOrderedJoin", "join_order"=>"join_order"));
//section
$this->define("section", "ForeignKey", array('target_model'=>'CmsSection'));
//author
$this->define("author", "ForeignKey", array('target_model'=>'WildfireUser', 'col_name'=>"author_id", "identifier"=>"fullname"));
//more_content <-> content
$this->define("more_content", "HasManyField", array('target_model'=>"CmsExtraContent", 'join_field'=>"cms_content_id",'editable'=>false, "eager_loading"=>true));
//comments <-> attached_to
$this->define("comments", "HasManyField", array('target_model'=>"CmsComment", 'join_field'=>"attached_id",'editable'=>false));
//category <-> attached_to
$this->define("categories", "ManyToManyField", array('target_model'=>"CmsCategory",'editable'=>false, "eager_loading"=>true));
//master -> revisions (used for previews and languages)
$this->define("revisions", "HasManyField", array("target_model"=>"CmsContent", "join_field"=>"preview_master_id"));
$this->define("master", "ForeignKey", array("target_model"=>"CmsContent", "col_name"=>"preview_master_id","editable"=>false));
$this->define("language", "IntegerField", array("editable"=>false));
}
 
/**
* Status options:
* 0 = draft, 1 = published, 3 = created but not saved, 4 = preview, 5 = other language draft, 6 = other language published
*
* @return array
*/
public function status_options() {
if($this->status == 4){
if($this->language) return array("5"=>"Draft", "4"=>"Published");
else return array("0"=>"Draft", "4"=>"Published");
    }elseif($this->status == 5 || $this->status == 6) return array("5"=>"Draft", "6"=>"Published");
else return array("0"=>"Draft", "1"=>"Published");
}
 
public function page_status() {
return $this->status_options[$this->status];
}
public function sections() {
$section = new CmsSection;
return $section->all();
}
public function section_name() {
return $this->section->title;
}
 
public function before_save() {
if(!$this->published) $this->published = date("Y-m-d H:i:s");
$this->date_modified = date("Y-m-d H:i:s");
if(!$this->date_created) $this->date_created = date("Y-m-d H:i:s");
$this->content = CmsTextFilter::filter("before_save", $this->content);
if($this->id && ($this->status == 1 || $this->status == 6)) {
$class = get_class($this);
$old_model = new $class($this->id);
if($old_model->status == 0 || $old_model->status == 3 || $old_model->status == 5)
   $this->before_publish();
}
}
public function before_insert() {
    if($this->status == 1) $this->before_publish();
}
 
public function generate_url() {
if((!$this->title) || ($this->status == 4) || ($this->status == 5) || ($this->status == 6)) return false;
//create the url from the title
$this->url = WXInflections::to_url($this->title);
//check to make sure the url does not clash with a section url (this would cause the content to be found as a section)
   $this->avoid_section_url_clash();
//make sure the url is unique
$this->url = $this->avoid_url_clash();
}
 
 
public function after_save() {
$this->save_extra_content();
}
 
public function before_publish() {
$this->generate_url();
   $this->ping_technorati();
   if(strtotime($this->published) < time() && $this->status != 4) {
   $this->published = date("Y-m-d H:i:s");
   }
}
 
public function permalink() {
if(!$this->section) return "/".$this->url;
$section = new CmsSection($this->cms_section_id);
return $section->permalink."/".$this->url;
}
public function date_published(){
return date('d/m/Y', strtotime($this->published));
}
public function is_published() {
if($this->status=="1" && strtotime($this->published) < time() ) return true;
return false;
}
public function date_expires(){
if($this->expires > 0) return date('d/m/Y', strtotime($this->expires));
else return false;
}
public function avoid_section_url_clash() {
$section = new CmsSection;
if($section->filter(array('url'=>$this->url))->first() ) $this->url= $this->url."-info";
}
public function avoid_url_clash(){
$test_url = $original_url = $this->url;
$model = new CmsContent();
if($this->primval) $model->filter($this->primary_key.' <> '.$this->primval);
$count = 0;
while($model->filter(array('url'=>$test_url, 'cms_section_id'=>$this->cms_section_id))->filter('status <> 4')->first() ){
if($count == 0) $test_url = $original_url . '-'.date("Y-m-d");
elseif($count == 1) $test_url = $original_url . '-'.$this->primval;
else $test_url = $original_url . '-'.mt_rand(0,99);
$count++;
}
return $test_url;
}
 
public function find_related_in_section($params=false) {
$ret = clone $this;
return $ret->clear()->scope("published")->filter($this->get_col("section")->col_name . ' = ' . $this->section->primval . " AND id <> " . $this->primval)->order('published DESC')->all();
  }
public function author() {
$this->author->username;
  }
  public function by() {
    return $this->author->fullname;
  }
public function author_options() {
$user = new WildfireUser;
return options_from_collection($user->filter('usergroup > 9')->all(), "id", "fullname");
  }
  /***** Finders for dealing with the extra_content table ******/
public function extra_content($name) {
$model = $this;
if($model->status ==4) $model = $model->master;
    $content = $model->more_content->filter(array('name'=>$name))->first();
    if($content->id) return $content;
    else{
$extra = new CmsExtraContent;
$extra->name = $name;
$extra->cms_content_id = $this->primval;
return $extra;
}
  }
  
public function extra_content_value($name) {
$model = $this;
if($model->status==4) $model = $model->master;
return CmsTextFilter::filter("before_output", $model->more_content->filter(array('name'=>$name))->first()->extra_content);
  }
 
public function save_extra_content() {
$model = $this;
if($model->status ==4) $model = $model->master;
$attributes = $_POST["cms_extra_content"];
if(count($attributes)){
foreach($attributes as $name=>$value){
if(isset($value) && strlen($value)>0){
$model = $this->extra_content($name);
$model->extra_content = $value;
$model = $model->save();
}
}
}
  }
 
public function image($number) {
if($this->images) return $this->images[$number-1];
return false;
}
public function add_pageview() {
$this->pageviews = intval($this->pageviews) + 1;
$this->save();
  }
public function format_content() {
    return CmsTextFilter::filter("before_output", $this->content);
  }
  
  public function scope_published() {
    $this->filter(array("status"=>"1"));
    $this->filter("published", date("Y-m-d H:i:s"), "<=");
    $this->filter("(`expires` <= `published` OR (`expires` >= `published` AND `expires` >= ? ))", date("Y-m-d H:i:s"), "raw");
    $this->order("published DESC");
  }
 
 
 
  /* delete bits form join table -now handled by the field */
public function remove_joins($information, $value){return true;}
/* old version */
public function find_most_commented($section="1", $since="7", $limit="10") {
 
$content = new CmsContent;
$sections = new CmsSection;
if($section && !is_numeric($section)) $section = $sections->filter(array('url'=>$section))->first()->id;
$sql = "SELECT *, count(attached_id) as counter FROM `cms_comment` RIGHT JOIN cms_content ON attached_id=cms_content.id WHERE cms_comment.status=1 AND `time` > date_sub(now(), INTERVAL '$since' DAY)";
if($section) $sql.= " AND cms_section_id=$section";
$sql.= " GROUP BY attached_id ORDER BY counter DESC LIMIT $limit";
return $content->find_by_sql($sql);
 
}
public function fuzzy_category_find($searches = array(), $limit="1", $section=false) {
return false;
}
//this should now be handled by a category model
public function find_by_category($category, $limit="1", $section=false) {
return false;
}
 
public function ping_technorati(){
# Using the XML-RPC extension to format the XML package
$request = '<?xml version="1.0"?>
<methodCall>
<methodName>weblogUpdates.ping</methodName>
<params>
<param>
<value>http://'.$_SERVER["HTTP_HOST"].'</value>
</param>
<param>
<value>http://'.$_SERVER["HTTP_HOST"].'</value>
</param>
</params>
</methodCall>
';
# Using the cURL extension to send it off,
# first creating a custom header block
    $header[] = "Host: rpc.technorati.com";
    $header[] = "Content-type: text/xml";
    $header[] = "Content-length: ".strlen($request) . "\r\n";
    $header[] = $request;
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, "http://rpc.technorati.com/rpc/ping"); # URL to post to
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); # return into a variable
    curl_setopt( $ch, CURLOPT_HTTPHEADER, $header ); # custom headers, see above
    curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' ); # This POST is special, and uses its specified Content-type
    $result = curl_exec( $ch ); # run!
    curl_close($ch);
}
 
}
 
?>