1: <?php
2:
3: 4: 5: 6: 7: 8: 9:
10: if (!defined('NEWS_POSITION_NORMAL')) {
11: define('NEWS_POSITION_NORMAL', 0);
12: define('NEWS_POSITION_STICKY', 1);
13: define('NEWS_POSITION_STICK_TO_TOP', 9);
14: }
15:
16: class ZenpageNews extends ZenpageItems {
17:
18: var $manage_rights = MANAGE_ALL_NEWS_RIGHTS;
19: var $manage_some_rights = ZENPAGE_NEWS_RIGHTS;
20: var $view_rights = ALL_NEWS_RIGHTS;
21: var $categories = NULL;
22: var $index = NULL;
23:
24: function __construct($titlelink, $allowCreate = NULL) {
25: if (is_array($titlelink)) {
26: $titlelink = $titlelink['titlelink'];
27: }
28: $new = $this->instantiate('news', array('titlelink' => $titlelink), 'titlelink', true, empty($titlelink), $allowCreate);
29: $this->exists = $this->loaded;
30: }
31:
32: 33: 34: 35: 36: 37:
38: function getCategories() {
39: if (is_null($this->categories)) {
40: $this->categories = query_full_array("SELECT * FROM " . prefix('news_categories') . " as cat," . prefix('news2cat') . " as newscat WHERE newscat.cat_id = cat.id AND newscat.news_id = " . $this->getID() . " ORDER BY cat.titlelink", false, 'title');
41: if (!$this->categories) {
42: $this->categories = array();
43: }
44: }
45: return $this->categories;
46: }
47:
48: function setCategories($categories) {
49: $result = query('DELETE FROM ' . prefix('news2cat') . ' WHERE `news_id`=' . $this->getID());
50: $result = query_full_array("SELECT * FROM " . prefix('news_categories') . " ORDER BY titlelink");
51: foreach ($result as $cat) {
52: if (in_array($cat['titlelink'], $categories)) {
53: query("INSERT INTO " . prefix('news2cat') . " (cat_id, news_id) VALUES ('" . $cat['id'] . "', '" . $this->getID() . "')");
54: }
55: }
56: }
57:
58: 59: 60: 61: 62:
63: function getSticky() {
64: return $this->get('sticky');
65: }
66:
67: function setSticky($v) {
68: $this->set('sticky', $v);
69: }
70:
71: function getTruncation() {
72: return $this->get('truncation');
73: }
74:
75: function setTruncation($v) {
76: $this->set('truncation', $v);
77: }
78:
79: 80: 81: 82:
83: function copy($newtitle) {
84: $newID = $newtitle;
85: $id = parent::copy(array('titlelink' => $newID));
86: if (!$id) {
87: $newID = $newtitle . ':' . seoFriendly(date('Y-m-d_H-i-s'));
88: $id = parent::copy(array('titlelink' => $newID));
89: }
90: if ($id) {
91: $newobj = new ZenpageNews($newID);
92: $newobj->setTitle($newtitle);
93: $newobj->setTags($this->getTags());
94: $newobj->setShow(0);
95: $newobj->setDateTime(date('Y-m-d H:i:s'));
96: $newobj->save();
97: $categories = array();
98: foreach ($this->getCategories() as $cat) {
99: $categories[] = $cat['cat_id'];
100: }
101: $result = query_full_array("SELECT * FROM " . prefix('news_categories') . " ORDER BY titlelink");
102: foreach ($result as $cat) {
103: if (in_array($cat['id'], $categories)) {
104: query("INSERT INTO " . prefix('news2cat') . " (cat_id, news_id) VALUES ('" . $cat['id'] . "', '" . $id . "')");
105: }
106: }
107: return $newobj;
108: }
109: return false;
110: }
111:
112: 113: 114: 115:
116: function remove() {
117: if ($success = parent::remove()) {
118: if ($this->id) {
119: $success = query("DELETE FROM " . prefix('obj_to_tag') . "WHERE `type`='news' AND `objectid`=" . $this->getID());
120: $success = $success && query("DELETE FROM " . prefix('news2cat') . " WHERE news_id = " . $this->getID());
121: $success = $success && query("DELETE FROM " . prefix('comments') . " WHERE ownerid = " . $this->getID() . ' AND type="news"');
122: }
123: }
124: return $success;
125: }
126:
127: 128: 129: 130: 131: 132: 133: 134:
135: function inProtectedCategory($only = false) {
136: $categories = $this->getCategories();
137: if (!empty($categories)) {
138: foreach ($categories as $cat) {
139: $catobj = new ZenpageCategory($cat['titlelink']);
140: $password = $catobj->getPassword();
141: if (!empty($password)) {
142: if (!$only)
143: return true;
144: } else {
145: if ($only)
146: return false;
147: }
148: }
149: return $only;
150: }
151: return false;
152: }
153:
154: 155: 156:
157: function isProtected() {
158: return $this->inProtectedCategory(true);
159: }
160:
161: 162: 163: 164:
165: function categoryIsVisible() {
166: if (zp_loggedin(ALL_NEWS_RIGHTS))
167: return true;
168: global $_zp_zenpage;
169: $categories = $this->getCategories(false);
170: if (count($categories) > 0) {
171: foreach ($categories as $cat) {
172: if ($_zp_zenpage->visibleCategory($cat)) {
173: return true;
174: }
175: }
176: return false;
177: }
178: return true;
179: }
180:
181:
182: 183: 184: 185: 186: 187:
188: function checkforGuest(&$hint = NULL, &$show = NULL) {
189: if (!parent::checkForGuest()) {
190: return false;
191: }
192: $categories = $this->getCategories();
193: if (empty($categories)) {
194: return 'zp_public_access';
195: } else {
196: $access = array();
197: foreach ($categories as $cat) {
198: $catobj = new ZenpageCategory($cat['titlelink']);
199: $guestaccess = $catobj->checkforGuest($hint, $show);
200: if($guestaccess) {
201: $access[] = 1;
202: } else {
203: $access[] = 0;
204: }
205: }
206: if(in_array(0, $access)) {
207: return false;
208: } else {
209: return 'zp_public_access';
210: }
211: }
212: return false;
213: }
214:
215: 216: 217: 218: 219: 220:
221: function isMyItem($action) {
222: global $_zp_current_admin_obj;
223: if (parent::isMyItem($action)) {
224: return true;
225: }
226: if (zp_loggedin($action)) {
227: if (GALLERY_SECURITY != 'public' && $this->getShow() && $action == LIST_RIGHTS) {
228: return LIST_RIGHTS;
229: }
230: if ($_zp_current_admin_obj->getUser() == $this->getAuthor()) {
231: return true;
232: }
233: if ($this->getShow() && $action == LIST_RIGHTS && !$this->isProtected() && $this->categoryIsVisible()) {
234: return true;
235: }
236: $mycategories = $_zp_current_admin_obj->getObjects('news');
237: if (!empty($mycategories)) {
238: foreach ($this->getCategories() as $category) {
239: $cat = new ZenpageCategory($category['titlelink']);
240: if ($cat->isMyItem(ZENPAGE_NEWS_RIGHTS)) {
241: return true;
242: }
243: }
244: }
245: }
246: return false;
247: }
248:
249: 250: 251: 252: 253: 254:
255: function inNewsCategory($catlink) {
256: if (!empty($catlink)) {
257: $categories = $this->getCategories();
258: $count = 0;
259: foreach ($categories as $cat) {
260: if ($catlink == $cat['titlelink']) {
261: $count = 1;
262: break;
263: }
264: }
265: return $count == 1;
266: } else {
267: return false;
268: }
269: }
270:
271: 272: 273: 274: 275: 276:
277: function inSubNewsCategoryOf($catlink) {
278: if (!empty($catlink)) {
279: $categories = $this->getCategories();
280: $count = 0;
281: foreach ($categories as $cat) {
282: $catobj = new ZenpageCategory($cat['titlelink']);
283: $parentid = $catobj->getParentID();
284: $parentcats = $catobj->getParents();
285: foreach ($parentcats as $parentcat) {
286: if ($catlink == $parentcat) {
287: $count = 1;
288: break;
289: }
290: }
291: }
292: return $count == 1;
293: } else {
294: return false;
295: }
296: }
297:
298: 299: 300: 301: 302: 303:
304: function getLink() {
305: return zp_apply_filter('getLink', rewrite_path(_NEWS_ . '/' . $this->getTitlelink() . '/', '/index.php?p=news&title=' . $this->getTitlelink()), $this, NULL);
306: }
307:
308: 309: 310: 311: 312: 313: 314:
315: function getNewsURL() {
316: Zenpage_internal_deprecations::getNewsURL();
317: return $this->getLink();
318: }
319:
320: 321: 322: 323: 324: 325: 326: 327:
328: function getIndex() {
329: global $_zp_zenpage, $_zp_current_zenpage_news;
330: if (func_num_args() != 0) {
331: Zenpage_internal_deprecations::getIndex();
332: }
333: if ($this->index == NULL) {
334: $articles = $_zp_zenpage->getArticles(0, NULL, true);
335: for ($i = 0; $i < count($articles); $i++) {
336: $article = $articles[$i];
337: if ($this->getTitlelink() == $article['titlelink']) {
338: $this->index = $i;
339: break;
340: }
341: }
342: }
343: return $this->index;
344: }
345:
346: 347: 348: 349: 350:
351: function getPrevArticle() {
352: global $_zp_zenpage, $_zp_current_zenpage_news;
353: if (func_num_args() != 0) {
354: Zenpage_internal_deprecations::getPrevArticle();
355: }
356: $index = $this->getIndex();
357: $article = $_zp_zenpage->getArticle($index - 1);
358: return $article;
359: }
360:
361: 362: 363: 364: 365:
366: function getNextArticle() {
367: global $_zp_zenpage, $_zp_current_zenpage_news;
368: if (func_num_args() != 0) {
369: Zenpage_internal_deprecations::getNextArticle();
370: }
371: $index = $this->getIndex();
372: $article = $_zp_zenpage->getArticle($index + 1);
373: return $article;
374: }
375:
376: 377: 378: 379: 380:
381: function getNewsLoopPage() {
382: $index = $this->getIndex();
383: return floor(($index / ZP_ARTICLES_PER_PAGE) + 1);
384: }
385:
386: }
387:
388: ?>