| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * Karamelo eLearning Platform |
|---|
| 4 | * Chipotle Software 2002-2008 |
|---|
| 5 | * GPLv3 manuel<arroba>mononeurona<punto>org |
|---|
| 6 | * @version 0.3 |
|---|
| 7 | **/ |
|---|
| 8 | //file: app/models/wiki.php |
|---|
| 9 | |
|---|
| 10 | class Wiki extends AppModel { |
|---|
| 11 | public $hasMany = array('Revision' => |
|---|
| 12 | array('className' => 'Revision', |
|---|
| 13 | 'conditions' => '', |
|---|
| 14 | 'order' => 'id DESC', |
|---|
| 15 | 'foreignKey' => 'wiki_id', |
|---|
| 16 | 'limit' => 1 |
|---|
| 17 | ) |
|---|
| 18 | ); |
|---|
| 19 | |
|---|
| 20 | public $belongsTo = array('User' => |
|---|
| 21 | array('className' => 'User', |
|---|
| 22 | 'conditions' => '', |
|---|
| 23 | 'order' => '', |
|---|
| 24 | 'foreignKey' => 'user_id', |
|---|
| 25 | 'fields' =>'id, username, avatar' |
|---|
| 26 | ), |
|---|
| 27 | 'Subject' => |
|---|
| 28 | array('className' => 'Subject', |
|---|
| 29 | 'conditions' => '', |
|---|
| 30 | 'order' => null, |
|---|
| 31 | 'foreignKey' => 'subject_id', |
|---|
| 32 | 'fields' => 'id, title' |
|---|
| 33 | ), |
|---|
| 34 | 'Vclassroom' => |
|---|
| 35 | array('className' => 'Vclassroom', |
|---|
| 36 | 'conditions' => '', |
|---|
| 37 | 'order' => null, |
|---|
| 38 | 'foreignKey' => 'vclassroom_id', |
|---|
| 39 | 'fields' => 'id, name' |
|---|
| 40 | ) |
|---|
| 41 | ); |
|---|
| 42 | |
|---|
| 43 | public $validate = array( |
|---|
| 44 | 'title' => array( |
|---|
| 45 | array( |
|---|
| 46 | 'rule' => 'isUnique', |
|---|
| 47 | 'message' => 'There is already a WikiPage with this title', |
|---|
| 48 | 'on' => 'create', // but not on update |
|---|
| 49 | ), |
|---|
| 50 | array( |
|---|
| 51 | 'rule' => array('minLength', 4), |
|---|
| 52 | 'message' => 'Title must be at least four characters long', |
|---|
| 53 | 'allowEmpty' => false, |
|---|
| 54 | 'required' => true, |
|---|
| 55 | 'on' => 'create', // but not on update |
|---|
| 56 | ) |
|---|
| 57 | ), |
|---|
| 58 | 'user_id' => array( |
|---|
| 59 | 'rule' => 'numeric', |
|---|
| 60 | 'allowEmpty' => false, |
|---|
| 61 | 'on' => 'create', // but not on update |
|---|
| 62 | 'required' => true |
|---|
| 63 | ) |
|---|
| 64 | ); |
|---|
| 65 | /* |
|---|
| 66 | * IBM Copyright Criki |
|---|
| 67 | */ |
|---|
| 68 | public function render($slug) |
|---|
| 69 | { |
|---|
| 70 | $conditions = array('Wiki.slug'=>$slug, 'Wiki.status'=>1); |
|---|
| 71 | |
|---|
| 72 | $this->User->unbindAll(); |
|---|
| 73 | |
|---|
| 74 | $wiki = $this->find($conditions); |
|---|
| 75 | |
|---|
| 76 | #die(debug($wiki)); |
|---|
| 77 | $content = $wiki['Revision'][0]['content']; |
|---|
| 78 | $wiki['Revision'][0]['content'] = ''; |
|---|
| 79 | |
|---|
| 80 | $processMarkup = true; |
|---|
| 81 | #markup array |
|---|
| 82 | #each 'processing =>false' value means tag is not open, true in processing value means tag was open and then must be closed |
|---|
| 83 | # so at the end of render process all values must be set to 'false' == all tag closed or not used |
|---|
| 84 | $markup = array( |
|---|
| 85 | "&&&" => array ( |
|---|
| 86 | 'processing' => false, |
|---|
| 87 | 'open' => '<pre>', |
|---|
| 88 | 'close' => '</pre>', |
|---|
| 89 | ), |
|---|
| 90 | "===" => array ( |
|---|
| 91 | 'processing' => false, |
|---|
| 92 | 'open' => '<h3>', |
|---|
| 93 | 'close' => '</h3>', |
|---|
| 94 | ), |
|---|
| 95 | "'''" => array ( |
|---|
| 96 | 'processing' => false, |
|---|
| 97 | 'open' => '<i>', |
|---|
| 98 | 'close' => '</i>', |
|---|
| 99 | ), |
|---|
| 100 | "!!!" => array ( |
|---|
| 101 | 'processing' => false, |
|---|
| 102 | 'open' => '<b>', |
|---|
| 103 | 'close' => '</b>', |
|---|
| 104 | ), |
|---|
| 105 | "___" => array ( |
|---|
| 106 | 'processing' => false, |
|---|
| 107 | 'open' => '<u>', |
|---|
| 108 | 'close' => '</u>', |
|---|
| 109 | ), |
|---|
| 110 | "[[[" => array ( |
|---|
| 111 | 'processing' => false, |
|---|
| 112 | 'open' => '', |
|---|
| 113 | 'close' => '', |
|---|
| 114 | ), |
|---|
| 115 | "]]]" => array ( |
|---|
| 116 | 'processing' => false, |
|---|
| 117 | 'open' => '', |
|---|
| 118 | 'close' => '', |
|---|
| 119 | ), |
|---|
| 120 | "htt" => array ( |
|---|
| 121 | 'processing' => false, |
|---|
| 122 | 'open' => '', |
|---|
| 123 | 'close' => '', |
|---|
| 124 | ), |
|---|
| 125 | "---" => array ( |
|---|
| 126 | 'processing' => false, |
|---|
| 127 | 'open' => '<hr />', |
|---|
| 128 | 'close' => '<hr />', |
|---|
| 129 | ), |
|---|
| 130 | "~~~" => array ( |
|---|
| 131 | 'processing' => false, |
|---|
| 132 | 'open' => '<br />', |
|---|
| 133 | 'close' => '<br />', |
|---|
| 134 | ), |
|---|
| 135 | |
|---|
| 136 | "*" => array ( |
|---|
| 137 | 'processing' => false, |
|---|
| 138 | 'open' => '<ul>', |
|---|
| 139 | 'close' => '</ul>', |
|---|
| 140 | ), |
|---|
| 141 | "#" => array ( |
|---|
| 142 | 'processing' => false, |
|---|
| 143 | 'open' => '<ol>', |
|---|
| 144 | 'close' => '</ol>', |
|---|
| 145 | ), |
|---|
| 146 | ); |
|---|
| 147 | |
|---|
| 148 | $lines = explode("\n", $content); # break lines |
|---|
| 149 | |
|---|
| 150 | #die(debug($lines)); |
|---|
| 151 | foreach ($lines as $line): |
|---|
| 152 | $line = trim($line); #clean |
|---|
| 153 | $key = substr($line, 0, 1); # first character in line |
|---|
| 154 | |
|---|
| 155 | # looking for # || * |
|---|
| 156 | if ($key && array_key_exists($key, $markup) && $processMarkup): # first character in line is * or # <ul> <ol> |
|---|
| 157 | if (!$markup[$key]['processing']): #processing is false |
|---|
| 158 | $markup[$key]['processing'] = true; # start mark |
|---|
| 159 | $line = $markup[$key]['open'] . '<li> ' . substr($line, 1) . ' </li>'; # remove first char and = <ul><li> Lorem ipsum </li> |
|---|
| 160 | #die($line); |
|---|
| 161 | else: |
|---|
| 162 | $line = '<li> ' . substr($line, 1) . ' </li>'; # other element in list, but not the first |
|---|
| 163 | endif; |
|---|
| 164 | else: |
|---|
| 165 | if ($markup['*']['processing']): # <ul> |
|---|
| 166 | $markup['*']['processing'] = false; |
|---|
| 167 | $line = ' ' . $markup['*']['close'] . ' ' . $line; |
|---|
| 168 | endif; |
|---|
| 169 | |
|---|
| 170 | if ($markup['#']['processing']): # <ol> |
|---|
| 171 | $markup['#']['processing'] = false; |
|---|
| 172 | $line = ' ' . $markup['#']['close'] . ' ' . $line; |
|---|
| 173 | endif; |
|---|
| 174 | |
|---|
| 175 | $line .= " <br /> "; |
|---|
| 176 | endif; |
|---|
| 177 | |
|---|
| 178 | # OPEN TAG ZONE |
|---|
| 179 | # now parse each word |
|---|
| 180 | $words = explode(' ', $line); |
|---|
| 181 | |
|---|
| 182 | foreach ($words as $word) # parse each word |
|---|
| 183 | { |
|---|
| 184 | $word = trim($word); |
|---|
| 185 | $key = substr($word, 0, 3); # 3 firts characters |
|---|
| 186 | |
|---|
| 187 | if ($key && array_key_exists($key, $markup)): # this 3 characters are a key or just a normal word? |
|---|
| 188 | if ($key == '&&&'): #code |
|---|
| 189 | if ($markup[$key]['processing']): # tag code is open |
|---|
| 190 | $markup[$key]['processing'] = false; |
|---|
| 191 | $processMarkup = true; |
|---|
| 192 | $word = $markup[$key]['close'] . substr($word,3); |
|---|
| 193 | else: # tag code is closed |
|---|
| 194 | $markup[$key]['processing'] = true; |
|---|
| 195 | $processMarkup = false; # mark as closed |
|---|
| 196 | $word = $markup[$key]['open'] . substr($word,3); |
|---|
| 197 | endif; |
|---|
| 198 | elseif ($processMarkup): |
|---|
| 199 | if ($markup[$key]['processing']): # tag code is open |
|---|
| 200 | $markup[$key]['processing'] = false; #mark as closed |
|---|
| 201 | $word = $markup[$key]['close'] . substr($word,3); #remove 3 characters |
|---|
| 202 | else: |
|---|
| 203 | if ($key != '---' && $key != 'htt'): #not hr or link |
|---|
| 204 | $markup[$key]['processing'] = true; |
|---|
| 205 | endif; |
|---|
| 206 | |
|---|
| 207 | if ($key == 'htt'): |
|---|
| 208 | if (substr($word, 0,7) == 'http://') : |
|---|
| 209 | $word = "<a href='" . $word . "'>".$word."</a>"; |
|---|
| 210 | endif; |
|---|
| 211 | else: |
|---|
| 212 | if ($key == '[[['): |
|---|
| 213 | $markup["]]]"]['processing'] = substr($word,3); |
|---|
| 214 | endif; |
|---|
| 215 | $word = $markup[$key]['open'] . substr($word,3); |
|---|
| 216 | endif; |
|---|
| 217 | endif; |
|---|
| 218 | else: |
|---|
| 219 | if ($processMarkup): # some tag is open |
|---|
| 220 | if ($markup["]]]"]['processing'] && substr($word, -3, 3) != "]]]"): #close link |
|---|
| 221 | $markup["]]]"]['processing'] .= ' ' . $word; |
|---|
| 222 | $word = ''; |
|---|
| 223 | endif; |
|---|
| 224 | endif; |
|---|
| 225 | endif; |
|---|
| 226 | |
|---|
| 227 | # CLOSE TAG ZONE |
|---|
| 228 | # is close tag?? |
|---|
| 229 | |
|---|
| 230 | $key = substr($word, -3, 3); # close tag |
|---|
| 231 | |
|---|
| 232 | if ($key && array_key_exists($key, $markup)) |
|---|
| 233 | { |
|---|
| 234 | if ($key == '&&&') |
|---|
| 235 | { |
|---|
| 236 | if ($markup[$key]['processing']) |
|---|
| 237 | { |
|---|
| 238 | $markup[$key]['processing'] = false; |
|---|
| 239 | $processMarkup = true; |
|---|
| 240 | $word = substr($word,0,-3) . $markup[$key]['close']; |
|---|
| 241 | } |
|---|
| 242 | else |
|---|
| 243 | { |
|---|
| 244 | $markup[$key]['processing'] = true; |
|---|
| 245 | $processMarkup = false; |
|---|
| 246 | $word = substr($word,0,-3) . $markup[$key]['open']; |
|---|
| 247 | } |
|---|
| 248 | } |
|---|
| 249 | elseif ($processMarkup) |
|---|
| 250 | { |
|---|
| 251 | if ($markup[$key]['processing'] && $key != ']]]') |
|---|
| 252 | { |
|---|
| 253 | $markup[$key]['processing'] = false; |
|---|
| 254 | $word = substr($word,0,-3) . $markup[$key]['close']; |
|---|
| 255 | } |
|---|
| 256 | else |
|---|
| 257 | { |
|---|
| 258 | if ($key != '---' && $key != ']]]') |
|---|
| 259 | { |
|---|
| 260 | $markup[$key]['processing'] = true; |
|---|
| 261 | } |
|---|
| 262 | elseif ($key != 'htt') |
|---|
| 263 | { |
|---|
| 264 | if ($key == '[[[') |
|---|
| 265 | { |
|---|
| 266 | $markup[$key]['processing'] = true; |
|---|
| 267 | $markup[']]]']['processing'] = substr($word,0,-3); |
|---|
| 268 | $word = substr($word,0,-3); |
|---|
| 269 | } |
|---|
| 270 | elseif ($key == ']]]') |
|---|
| 271 | { |
|---|
| 272 | if ($markup[$key]['processing']) |
|---|
| 273 | { |
|---|
| 274 | if (!$markup["[[["]['processing']) |
|---|
| 275 | { |
|---|
| 276 | $markup[$key]['processing'] .= ' ' . substr($word,0,-3); |
|---|
| 277 | } |
|---|
| 278 | else |
|---|
| 279 | { |
|---|
| 280 | $markup[$key]['processing'] = substr($markup[$key]['processing'],0,-3); |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | if (strpos($markup[$key]['processing'], "|")) |
|---|
| 284 | { |
|---|
| 285 | list($alink, $atitle) = explode('|', $markup[$key]['processing']); |
|---|
| 286 | } |
|---|
| 287 | else |
|---|
| 288 | { |
|---|
| 289 | $alink = $markup[$key]['processing']; |
|---|
| 290 | $atitle = false; |
|---|
| 291 | } |
|---|
| 292 | if (strpos($alink, "://")) |
|---|
| 293 | { |
|---|
| 294 | $word = "<a href='" . $alink . "'>"; |
|---|
| 295 | } |
|---|
| 296 | else |
|---|
| 297 | { |
|---|
| 298 | $word = "<a href='/entries/view/" . preg_replace("/[^a-z]/", "", strtolower($alink)) ."'>"; |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | if ($atitle) |
|---|
| 302 | { |
|---|
| 303 | $word .= $atitle; |
|---|
| 304 | } |
|---|
| 305 | else |
|---|
| 306 | { |
|---|
| 307 | $word .= $alink; |
|---|
| 308 | } |
|---|
| 309 | $word .= "</a>"; |
|---|
| 310 | $markup[$key]['processing'] = false; |
|---|
| 311 | $markup['[[[']['processing'] = false; |
|---|
| 312 | } |
|---|
| 313 | } |
|---|
| 314 | else |
|---|
| 315 | { |
|---|
| 316 | $word = substr($word,0,-3) . $markup[$key]['open']; |
|---|
| 317 | } |
|---|
| 318 | } |
|---|
| 319 | } |
|---|
| 320 | } |
|---|
| 321 | } |
|---|
| 322 | else |
|---|
| 323 | { |
|---|
| 324 | if ($processMarkup): |
|---|
| 325 | if ($processMarkup && $markup[']]]']['processing']): |
|---|
| 326 | $word = ''; |
|---|
| 327 | endif; |
|---|
| 328 | $markup["[[["]['processing'] = false; |
|---|
| 329 | endif; |
|---|
| 330 | } |
|---|
| 331 | $wiki['Revision'][0]['content'] .= $word . ' '; |
|---|
| 332 | } |
|---|
| 333 | endforeach; |
|---|
| 334 | |
|---|
| 335 | return $wiki; |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | public function makeSlug($title) |
|---|
| 339 | { |
|---|
| 340 | // Turn names such as "It's wrinkled!" into slugs such as "its-wrinkled" |
|---|
| 341 | $slug = strtolower($title); // Make the string lowercase |
|---|
| 342 | $slug = str_replace('\'', '', $slug); // Take out apostraphes. CakePHP takes care of escaping strings before putting them into SQL queries. This is purely for aesthetic purposes - changing "that-s-cool" into "thats-cool" |
|---|
| 343 | $slug = ereg_replace('[^[:alnum:]]+', '_', $slug); // Turn any group of non-alphanumerics into a single hyphen |
|---|
| 344 | $slug = trim($slug, '_'); // Remove unnecessary hyphens from beginning and end |
|---|
| 345 | |
|---|
| 346 | if (strlen($slug) > 80): |
|---|
| 347 | $slug = substr($slug, 0, 79); |
|---|
| 348 | endif; |
|---|
| 349 | |
|---|
| 350 | return $slug; |
|---|
| 351 | } |
|---|
| 352 | } |
|---|
| 353 | ?> |
|---|