On branch master
[event_calendar.git] / eventcalendar.seovs.class.php
blobf23b93ec07831acee8b9209c1b651cab0d91aaab
1 <?php
3 /*
4 * Event Calendar for Elxis CMS 2008.x and 2009.x
6 * SEO handler
8 * @version 1.1
9 * @package eventCalendar
10 * @author Apostolos Koutsoulelos <akoutsoulelos@yahoo.gr>
11 * @copyright Copyright (C) 2009-2010 Apostolos Koutsoulelos. All rights reserved.
12 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
13 * @link
16 // Prevent direct inclusion of this file
17 defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
19 // Declare globals
20 global $mainframe;
22 // Include seovs class
23 require_once($mainframe->getCfg('absolute_path').'/administrator/includes/seovs.class.php');
25 /*********************************************************/
26 /* THE CLASS THAT WILL CONTAIN THE SEOVS FUNCTIONALITY */
27 /*********************************************************/
28 class seovsEventCalendar extends seovs {
30 public $option = '';
31 public $title = '';
32 public $seotitle = '';
33 public $ascii = '';
34 public $id = 0;
35 public $catid = 0;
36 public $Itemid = 0;
37 public $message = '';
38 public $code = 0;
39 public $section = '';
41 /********************/
42 /* Generate message */
43 /********************/
44 private function writeMsg($code='0') {
45 global $adminLanguage;
47 $this->code = intval($code);
48 switch ($this->code) {
49 case 1: $this->message = $adminLanguage->A_SEOTLARGER; break;
50 case 2: $this->message = $adminLanguage->A_INVALID; break;
51 case 3: $this->message = $adminLanguage->A_INVALID.', '.$adminLanguage->A_SEOTEXIST; break;
52 case 4: $this->message = $adminLanguage->A_VALID; break;
53 case 5: $this->message = $adminLanguage->A_INVALID.', '.$adminLanguage->A_SEOTEMPTY; break;
54 case 6: $this->message = $adminLanguage->A_INVALID.', '.$adminLanguage->A_PLSSELECTCAT; break;
55 case 7: $this->message = $adminLanguage->A_INVALID.' '.$adminLanguage->A_SECTION; break;
56 default:
57 $this->message = _GEM_UNKN_ERR;
58 break;
62 /********************************/
63 /* Implement suggest function */
64 /********************************/
65 public function suggest() {
66 global $mosConfig_absolute_path;
68 if ($this->title == '') {
69 $this->writeMsg('1');
70 return false;
72 $ascii = strtolower(eUTF::utf8_to_ascii($this->title, ''));
73 $this->ascii = preg_replace("/[^a-zA-Z0-9-_\s]/", '', $ascii);
74 if ($this->ascii == '') {
75 $this->writeMsg('1');
76 return false;
79 $parts = split(' ', $this->ascii);
80 $nparts = array();
81 foreach ($parts as $part) {
82 if (strlen($part) > 1) { array_push($nparts, $part); }
84 if (count($nparts) == 0) {
85 $this->writeMsg('1');
86 return false;
88 $newtitle = implode('-', $nparts);
90 $invalidNames = array('save', 'cancel');
91 if (in_array($newtitle, $invalidNames) || preg_match('/^(com_)/', $newtitle)) {
92 $newtitle = 'evcal-'.$newtitle;
93 } else if (file_exists($mosConfig_absolute_path.'/'.$newtitle.'/')) {
94 $newtitle = 'evcal-'.$newtitle;
97 return $this->suggest_events($newtitle);
100 /***************************************************/
101 /* New suggest_events function for EventCalendar */
102 /***************************************************/
103 protected function suggest_events($ntitle) { //feed manually id and catid before suggest
104 global $database;
106 if (intval($this->catid) < 1) {
107 $this->writeMsg('6');
108 return false;
111 $extra = ($this->id) ? " AND id <> '".$this->id."'" : '';
112 $database->setQuery("SELECT COUNT(*) FROM #__eventcalendar WHERE catid='".$this->catid."' AND seotitle='".$ntitle."'".$extra);
113 $c = intval($database->loadResult());
114 if ($c) {
115 for ($i=2; $i<21; $i++) {
116 $database->setQuery("SELECT COUNT(*) FROM #__eventcalendar WHERE catid='".$this->catid."' AND seotitle='".$ntitle."-".$i."'".$extra);
117 $c2 = intval($database->loadResult());
118 if (!$c2) {
119 $ntitle .= '-'.$i;
120 break;
123 if ($c2) { $ntitle .= '-'.rand(1000,9999); }
126 $this->sugtitle = $ntitle;
127 return $ntitle;
130 /*********************************/
131 /* Implement validate function */
132 /*********************************/
133 public function validate() {
134 global $mosConfig_absolute_path;
136 $invalidNames = array('save', 'cancel');
137 if ( in_array($this->seotitle, $invalidNames) || preg_match('/^(com_)/', $this->seotitle) || file_exists($mosConfig_absolute_path.'/'.$this->seotitle.'/') ) {
138 $this->writeMsg('2');
139 return false;
141 if ($this->seotitle == '') {
142 $this->writeMsg('5');
143 return false;
145 if (!eUTF::utf8_isASCII($this->seotitle)) {
146 $this->writeMsg('2');
147 return false;
149 $seotitle2 = preg_replace("/[^a-z0-9-_]/", '', $this->seotitle);
150 if ($seotitle2 != $this->seotitle) {
151 $this->writeMsg('2');
152 return false;
155 return $this->validate_events();
158 /*********************************************/
159 /* New validate function for EventCalendar */
160 /*********************************************/
161 protected function validate_events() { //feed manually id, catid before validation
162 global $database;
164 if (intval($this->catid) < 1) {
165 $this->writeMsg('6');
166 return false;
169 $extra = ($this->id) ? " AND id <> '".$this->id."'" : '';
170 $database->setQuery("SELECT COUNT(*) FROM #__eventcalendar WHERE catid='".$this->catid."' AND seotitle='".$this->seotitle."'".$extra);
171 $c = intval($database->loadResult());
172 if ($c) {
173 $this->writeMsg('3');
174 return false;
176 $this->writeMsg('4');
177 return true;