00001 <?php
00002
00016 import('file.JournalFileManager');
00017
00018 define('CONTENT_FILE_HEADER', '<html xmlns="http://www.w3.org/1999/xhtml"><body>');
00019 define('CONTENT_FILE_FOOTER', '</body></html>');
00020
00021 class ContentManager {
00022
00023
00024 var $filePath;
00025
00026
00027 var $fileContent;
00028
00029
00030 var $defaultHeading;
00031
00032
00033
00034
00035 function ContentManager() {
00036 $journal = &Request::getJournal();
00037 $journalFileManager =& new JournalFileManager($journal);
00038
00039
00040 $lang = Locale::getLocale();
00041 $this->filePath = $journalFileManager->filesDir.'content/'.$lang.'/content.xhtml';
00042
00043 $this->loadContents();
00044 }
00045
00046 function loadContents() {
00047 $journal = &Request::getJournal();
00048 $journalFileManager =& new JournalFileManager($journal);
00049
00050 $filePath = $this->filePath;
00051
00052
00053 if ( !$journalFileManager->fileExists($this->filePath) ) {
00054 if ( $journal->getPrimaryLocale() )
00055 $lang = $journal->getPrimaryLocale();
00056 else
00057 $lang = 'en_US';
00058
00059 $filePath = $journalFileManager->filesDir.'content/'.$lang.'/content.xhtml';
00060 }
00061
00062
00063
00064 $this->fileContent = $journalFileManager->readFile($filePath );
00065 $this->fileContent = preg_replace("/(\r\n|\r|\n)+/","",$this->fileContent);
00066
00067
00068 preg_match('/<h([1-3])>(.*)<\/h[1-3]>/U', $this->fileContent, $matches);
00069 if ( count($matches) > 0 )
00070 $this->defaultHeading = array( $matches[1], $this->webSafe($matches[2]), $matches[2]);
00071 else
00072 $this->defaultHeading = array( 0, null, null );
00073 }
00074
00075
00076 function saveContents( &$content ) {
00077 $journal = &Request::getJournal();
00078 $journalFileManager =& new JournalFileManager($journal);
00079
00080 $content = CONTENT_FILE_HEADER.$content.CONTENT_FILE_FOOTER;
00081
00082 $journalFileManager->writeFile($this->filePath, $content );
00083
00084 $this->loadContents();
00085 }
00086
00087
00088
00089
00090
00091
00092
00093 function parseContents ( &$headings, &$content, $current = null ) {
00094 $fileContent = $this->fileContent;
00095
00096 preg_match_all('/<h([1-3])>(.*)<\/h[1-3]>/U', $fileContent, $headMatches);
00097 preg_match_all('/<\/h([1-3])>(.*)<(h[1-3]|\/body)>/U', $fileContent, $contentMatches);
00098
00099 $i = -1;
00100 $count = count($headMatches[2]);
00101 $current = explode(":", $current);
00102
00103 while ( $i + 1 < $count ) {
00104 $i++;
00105
00106
00107 if ( $headMatches[1][$i] == 1 ) {
00108
00109 $webSafe = $this->websafe($headMatches[2][$i]);
00110 $headings[$i] = array(1, $webSafe, $headMatches[2][$i]);
00111 $content[$webSafe] = $contentMatches[2][$i];
00112
00113
00114 if ( $webSafe == $current[0] ) {
00115 while ( $i + 1 < $count && $headMatches[1][$i + 1] > 1 ) {
00116 $i++;
00117
00118
00119 if ( $headMatches[1][$i] == 2 ) {
00120
00121 $webSafe = $this->websafe($headMatches[2][$i]);
00122 $headings[$i] = array(2, $current[0].":".$webSafe, $headMatches[2][$i]);
00123 $content[$current[0].":".$webSafe] = $contentMatches[2][$i];
00124
00125
00126 if ( count($current) >= 2 && $webSafe == $current[1] ) {
00127
00128 while ( $i + 1 < $count && $headMatches[1][$i+1] > 2 ) {
00129 $i++;
00130
00131 $webSafe = $this->websafe($headMatches[2][$i]);
00132 $headings[$i] = array(3, $current[0].":".$current[1].":".$webSafe, $headMatches[2][$i]);
00133 $content[$current[0].":".$current[1].":".$webSafe] = $contentMatches[2][$i];
00134 }
00135 }
00136 }
00137 }
00138 }
00139 }
00140 }
00141
00142 return true;
00143 }
00144
00145
00146
00147
00148
00149
00150
00151
00152 function insertContent ($content, &$strCurrent) {
00153
00154 if ( $strCurrent == '' ) {
00155 $strCurrent = $this->defaultHeading[1];
00156 }
00157
00158 $fileContent = $this->fileContent;
00159
00160
00161 preg_match_all('/<h([1-3])>(.*)<\/h[1-3]>/U', $fileContent, $headFileMatches);
00162 preg_match_all('/<\/h([1-3])>(.*)<(h[1-3]|\/body)>/U', $fileContent, $contentFileMatches);
00163
00164 preg_match_all('/<h([1-3])>(.*)<\/h[1-3]>/U', $content, $headCurrentMatches);
00165 preg_match_all('/<\/h([1-3])>(.*)<(h[1-3]|\/body)>/U', $content."</body>", $contentCurrentMatches);
00166
00167
00168
00169
00170
00171 $uniquifier = 0;
00172 for ( $i = 0; $i < count( $headFileMatches[2] ); $i++ ) {
00173 $headFileMatches[3][$i] = $this->webSafe($headFileMatches[2][$i]);
00174 preg_match('/.*_duplicate_([0-9]+)$/', $headFileMatches[3][$i], $dup);
00175 if ( count($dup) > 0 && (int) $dup[1] > $uniquifier )
00176 $uniquifier = (int) $dup[1];
00177 }
00178 for ( $i = 0; $i < count( $headCurrentMatches[2] ); $i++ ) {
00179 $headCurrentMatches[3][$i] = $this->webSafe($headCurrentMatches[2][$i]);
00180 preg_match('/.*_duplicate_([0-9]+)$/', $headCurrentMatches[3][$i], $dup);
00181 if ( count($dup) > 0 && (int) $dup[1] > $uniquifier )
00182 $uniquifier = (int) $dup[1];
00183 }
00184 $uniquifier++;
00185
00186 $i = -1;
00187 $toWrite = "";
00188
00189
00190 $count = count($headFileMatches[2]);
00191
00192 $current = explode(":", $strCurrent);
00193
00194
00195 $addedHeadings = array(1 => array(), 2 => array(), 3 => array() );
00196 $prev = 1;
00197
00198
00199
00200
00201
00202
00203
00204 if ( $count == 0 ) {
00205
00206 if ( $headCurrentMatches[1][0] != 1 ) {
00207 $headCurrentMatches[1][0] = 1;
00208 }
00209
00210 for ( $i = 0; $i < count($headCurrentMatches[0]); $i++ ) {
00211
00212 if ( $headCurrentMatches[1][$i] > $prev + 1 )
00213 $headCurrentMatches[1][$i] = $prev + 1;
00214
00215
00216 $prev = $headCurrentMatches[1][$i];
00217
00218
00219 if ( in_array($headCurrentMatches[3][$i], $addedHeadings[$headCurrentMatches[1][$i]]) ) {
00220 $headCurrentMatches[2][$i] = preg_replace('/^(.*)(_duplicate_[0-9]+)?$/', '$0_duplicate_'.$uniquifier,$headCurrentMatches[2][$i]);
00221 $headCurrentMatches[3][$i] = preg_replace('/^(.*)(_duplicate_[0-9]+)?$/', '$0_duplicate_'.$uniquifier, $headCurrentMatches[3][$i]);
00222 $uniquifier++;
00223
00224 }
00225
00226 $toWrite .= "\n<h".$headCurrentMatches[1][$i].">".$headCurrentMatches[2][$i]."</h".$headCurrentMatches[1][$i].">\n";
00227 $toWrite .= $contentCurrentMatches[2][$i]."\n";
00228
00229 array_push($addedHeadings[$headCurrentMatches[1][$i]], $headCurrentMatches[3][$i]);
00230 if ( $headCurrentMatches[1][$i] < 3 )
00231 $addedHeadings[3] = array();
00232 if ( $headCurrentMatches[1][$i] < 2 )
00233 $addedHeadings[2] = array();
00234
00235 }
00236
00237 }
00238
00239
00240
00241
00242 while ( $i + 1 < $count ) {
00243 $i++;
00244
00245
00246
00247
00248
00249 if ( $this->webSafe($headFileMatches[2][$i]) == $current[count($current) - 1]
00250 && $headFileMatches[1][$i] == count($current) ) {
00251
00252 if ( count( $headCurrentMatches[0]) > 0 ) {
00253
00254 if ( in_array($headCurrentMatches[3][0], $addedHeadings[$headCurrentMatches[1][0]]) ) {
00255 $headCurrentMatches[2][0] = preg_replace('/^(.*)(_duplicate_[0-9]+)?$/', '$0_duplicate_'.$uniquifier,$headCurrentMatches[2][0]);
00256 $headCurrentMatches[3][0] = preg_replace('/^(.*)(_duplicate_[0-9]+)?$/', '$0_duplicate_'.$uniquifier,$headCurrentMatches[3][0]);
00257 $uniquifier++;
00258 }
00259 $toWrite .= "\n<h".$headCurrentMatches[1][0].">".$headCurrentMatches[2][0]."</h".$headCurrentMatches[1][0].">\n";
00260 $toWrite .= $contentCurrentMatches[2][0]."\n";
00261 array_push($addedHeadings[$headCurrentMatches[1][0]], $headCurrentMatches[3][0]);
00262 if ( $headCurrentMatches[1][0] < 3 )
00263 $addedHeadings[3] = array();
00264 if ( $headCurrentMatches[1][0] < 2 )
00265 $addedHeadings[2] = array();
00266
00267
00268
00269
00270
00271 while ( $i + 1 < $count && count($headCurrentMatches[1]) > 1 && $headFileMatches[1][$i + 1] > $headCurrentMatches[1][1] ) {
00272 $i++;
00273
00274
00275 if ( in_array($headFileMatches[3][$i], $addedHeadings[$headFileMatches[1][$i]]) ) {
00276 $headFileMatches[2][$i] = preg_replace('/^(.*)(_duplicate_[0-9]+)?$/', '$0_duplicate_'.$uniquifier,$headFileMatches[2][$i]);
00277 $headFileMatches[3][$i] = preg_replace('/^(.*)(_duplicate_[0-9]+)?$/', '$0_duplicate_'.$uniquifier,$headFileMatches[3][$i]);
00278 $uniquifier++;
00279 }
00280 $toWrite .= "\n<h".$headFileMatches[1][$i].">".$headFileMatches[2][$i]."</h".$headFileMatches[1][$i].">\n";
00281 $toWrite .= $contentFileMatches[2][$i]."\n";
00282 array_push($addedHeadings[$headFileMatches[1][$i]], $headFileMatches[3][$i]);
00283 if ( $headFileMatches[1][$i] < 3 )
00284 $addedHeadings[3] = array();
00285 if ( $headFileMatches[1][$i] < 2 )
00286 $addedHeadings[2] = array();
00287 }
00288
00289
00290 for ( $j = 1; $j < count($headCurrentMatches[0]); $j++ ) {
00291
00292 if ( in_array($headCurrentMatches[3][$j], $addedHeadings[$headCurrentMatches[1][$j]]) ) {
00293 $headCurrentMatches[2][$j] = preg_replace('/^(.*)(_duplicate_[0-9]+)?$/', '$1_duplicate_'.$uniquifier,$headCurrentMatches[2][$j]);
00294 $headCurrentMatches[3][$j] = preg_replace('/^(.*)(_duplicate_[0-9]+)?$/', '$1_duplicate_'.$uniquifier,$headCurrentMatches[3][$j]);
00295 $uniquifier++;
00296 }
00297 $toWrite .= "\n<h".$headCurrentMatches[1][$j].">".$headCurrentMatches[2][$j]."</h".$headCurrentMatches[1][$j].">\n";
00298 $toWrite .= $contentCurrentMatches[2][$j]."\n";
00299 array_push($addedHeadings[$headCurrentMatches[1][$j]], $headCurrentMatches[3][$j]);
00300 if ( $headCurrentMatches[1][$j] < 3 )
00301 $addedHeadings[3] = array();
00302 if ( $headCurrentMatches[1][$j] < 2 )
00303 $addedHeadings[2] = array();
00304
00305 }
00306 }
00307 } else {
00308
00309
00310 if ( in_array($headFileMatches[3][$i], $addedHeadings[$headFileMatches[1][$i]]) ) {
00311 $headFileMatches[2][$i] = preg_replace('/^(.*)(_duplicate_[0-9]+)?$/', '$0_duplicate_'.$uniquifier,$headFileMatches[2][$i]);
00312 $headFileMatches[3][$i] = preg_replace('/^(.*)(_duplicate_[0-9]+)?$/', '$0_duplicate_'.$uniquifier,$headFileMatches[3][$i]);
00313 $uniquifier++;
00314 }
00315
00316
00317 $toWrite .= "\n<h".$headFileMatches[1][$i].">".$headFileMatches[2][$i]."</h".$headFileMatches[1][$i].">\n";
00318 $toWrite .= $contentFileMatches[2][$i]."\n";
00319 array_push($addedHeadings[$headFileMatches[1][$i]], $headFileMatches[3][$i]);
00320 if ( $headFileMatches[1][$i] < 3 )
00321 $addedHeadings[3] = array();
00322 if ( $headFileMatches[1][$i] < 2 )
00323 $addedHeadings[2] = array();
00324
00325
00326 }
00327 }
00328
00329
00330
00331
00332
00333
00334
00335 if ( count( $headCurrentMatches[0]) > 0 ) {
00336
00337 $newCurrent = explode(":", $strCurrent);
00338 $newCurrent[count($newCurrent) - 1] = $this->webSafe($headCurrentMatches[2][0]);
00339 $strCurrent = implode(":", $newCurrent);
00340 } else {
00341
00342 $newCurrent = explode(":", $strCurrent);
00343 unset($newCurrent[count($newCurrent) - 1]);
00344 $strCurrent = implode(":", $newCurrent);
00345 }
00346
00347 $this->saveContents( $toWrite );
00348
00349
00350 return true;
00351 }
00352
00353
00354 function websafe ( $str ) {
00355 return urlencode(strtolower(str_replace(' ', '_', str_replace(':', '', $str))));
00356 }
00357
00358 function cleanurl ( $str ) {
00359 return str_replace("%3A", ":", urlencode(strtolower(str_replace(' ', '_', $str))));
00360 }
00361
00362 }
00363
00364 ?>