00001 <?php
00002
00012 class CM3 {
00015 protected $listSections;
00018 protected $XmlName;
00019
00022 public function __construct($XmlType) {
00023 $this->XmlName = $XmlType;
00024 $this->listSections = array('sectionNumber'=>0, 'subSections'=>array());
00025 }
00026
00036 public function addSection($mapping, $attributes=array(), $parent = array()) {
00037 $return = true;
00038 switch($mapping) {
00039 case 'category':
00040 $return = $this->addCategory($attributes, $parent);
00041 break;
00042 case 'item':
00043 $return = $this->addItem($attributes, $parent);
00044 break;
00045 default:
00046 $return = FALSE;
00047 }
00048 return $return;
00049 }
00050
00056 private function addCategory($attributes, $parent) {
00057 $sectionArray = array('mapping'=>'category', 'subSections'=>array(),
00058 'sectionNumber' => 0);
00059 $sectionArray = array_merge($sectionArray, $attributes);
00060 $return = $this->addToParent($sectionArray, $parent);
00061 return $return;
00062 }
00063
00069 private function addItem($attributes, $parent) {
00070 $sectionArray = array('mapping'=>'item', 'subSections'=>array(),
00071 'sectionNumber' => 0);
00072 $sectionArray = array_merge($sectionArray, $attributes);
00073 $return = $this->addToParent($sectionArray, $parent);
00074 return $return;
00075 }
00076
00083 private function addToParent($array, $parentIds) {
00084 $arrayReturn = $this->prepareParent($array, $parentIds);
00085 if($arrayReturn) {
00086 $this->listSections = $arrayReturn[0];
00087 return $arrayReturn[1];
00088 } else
00089 return FALSE;
00090 }
00091
00099 private function prepareParent($array, $parentIds=array(), $parentArray=array()) {
00100 if($parentArray == array())
00101 $parentArray = $this->listSections;
00102 if($parentIds == array()) {
00103 $parentArray['subSections'][$parentArray['sectionNumber']] = $array;
00104 $parentArray['sectionNumber']++;
00105 return array($parentArray, array($parentArray['sectionNumber'] - 1));
00106 } else {
00107 $nextId = array_shift($parentIds);
00108 if(isset($parentArray['subSections'][$nextId])) {
00109 $nextArray = $parentArray['subSections'][$nextId];
00110 } else
00111 return FALSE;
00112 $arrayReturn = $this->prepareParent($array, $parentIds, $nextArray);
00113 if($arrayReturn) {
00114 $parentArray['subSections'][$nextId] = $arrayReturn[0];
00115 $idArray = array($nextId);
00116 while($arrayReturn[1] != array())
00117 $idArray[] = array_shift($arrayReturn[1]);
00118 return array($parentArray, $idArray);
00119 } else
00120 return FALSE;
00121 }
00122 }
00123
00124
00130 public function sortSections($recursive=false, $parent=array()) {
00131 $arrayReturn = $this->sortSectionHelper($recursive, $parent);
00132 if($arrayReturn) {
00133 $this->listSections = $arrayReturn;
00134 return TRUE;
00135 } else
00136 return FALSE;
00137 }
00138
00146 private function sortSectionHelper($recursive=false, $parentIds=array(), $parentArray=array()) {
00147 if($parentArray == array())
00148 $parentArray = $this->listSections;
00149 if($parentIds == array()) {
00150 $parentArray['subSections'] = $this->sortSubsections($parentArray['subSections'], $recursive);
00151 return $parentArray;
00152 } else {
00153 $nextId = array_shift($parentIds);
00154 if(isset($parentArray['subSections'][$nextId])) {
00155 $nextArray = $parentArray['subSections'][$nextId];
00156 } else
00157 return FALSE;
00158 $arrayReturn = $this->sortSectionHelper($recursive, $parentIds, $nextArray);
00159 if($arrayReturn) {
00160 $parentArray['subSections'][$nextId] = $arrayReturn;
00161 return $parentArray;
00162 } else
00163 return FALSE;
00164 }
00165 }
00166
00172 private function sortSubSections($subSections = array(), $recursive = false) {
00173 $newArray = $subSections;
00174 $i = 1;
00175 if(count($newArray) > 1)
00176 while($i < count($newArray)) {
00177 if($this->strcomp_mine($newArray[$i]['name'], $newArray[$i-1]['name']) < 0) {
00178 $temp = $newArray[$i];
00179 $newArray[$i] = $newArray[$i-1];
00180 $newArray[$i-1] = $temp;
00181 if($i > 1)
00182 $i--;
00183 } else
00184 $i++;
00185 }
00186 if($recursive)
00187 for($i=0; $i<count($newArray); $i++) {
00188 $newArray[$i]['subSections'] = $this->sortSubSections($newArray[$i]['subSections'], $recursive);
00189 }
00190 return $newArray;
00191 }
00192
00193 function strcomp_mine($str1, $str2)
00194 {
00195 $abc = "0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
00196 $len = min(strlen($str1), strlen($str2));
00197 for ($i = 0; $i < $len; $i++)
00198 {
00199 $s1 = strlen($abc);
00200 $s2 = strlen($abc);
00201 for ($j = 0; $j < strlen($abc); $j++) if ($str1[$i] == $abc[$j]) $s1 = $j;
00202 for ($k = 0; $k < strlen($abc); $k++) if ($str2[$i] == $abc[$k]) $s2 = $k;
00203 if ($s1 < $s2) return -1;
00204 else if ($s1 > $s2) return 1;
00205 }
00206 return 0;
00207 }
00208
00209
00210
00211
00212
00213
00217 public function generateXML() {
00218 $retXml = <<<END_RET_XML
00219 <$this->XmlName>
00220
00221 END_RET_XML;
00222
00223 foreach($this->listSections['subSections'] as $subSection) {
00224 $retXml .= $this->generateSection($subSection);
00225 }
00226
00227 $retXml .= <<<END_RET_XML
00228 </$this->XmlName>
00229 END_RET_XML;
00230
00231 return $retXml;
00232
00233 }
00234
00241 private function generateSection($sectionItem) {
00242 $sectionXml = "<section mapping='$sectionItem[mapping]'>\n";
00243 if(isset($sectionItem['name']))
00244 $sectionXml .= "<name> $sectionItem[name] </name>\n";
00245 $sectionXml .= " <shortName />\n";
00246
00247 switch($sectionItem['mapping']) {
00248 case 'category':
00249 $sectionXml .= $this->generateCategory($sectionItem, true);
00250 break;
00251 case 'item':
00252 $sectionXml .= $this->generateItem($sectionItem, true);
00253 break;
00254 default:
00255 break;
00256 }
00257
00258 foreach($sectionItem['subSections'] as $subSection) {
00259 $sectionXml .= $this->generateSection($subSection);
00260 }
00261
00262 switch($sectionItem['mapping']) {
00263 case 'category':
00264 $sectionXml .= $this->generateCategory($sectionItem, false);
00265 break;
00266 case 'item':
00267 $sectionXml .= $this->generateItem($sectionItem, false);
00268 break;
00269 default:
00270 break;
00271 }
00272
00273 $sectionXml .= <<<END_SECTION
00274 </section>
00275
00276 END_SECTION;
00277 return $sectionXml;
00278 }
00279
00285 private function generateCategory($sectionItem, $top){ }
00286
00292 private function generateItem($sectionItem, $top) {
00293 if($top) {
00294 $sectionXml = '';
00295 if(isset($sectionItem['photo']) || isset($sectionItem['link'])) {
00296 $sectionXml .= " <sectionData>\n";
00297 if(isset($sectionItem['photo']))
00298 $sectionXml .= " <media snapid=\"$sectionItem[photo]\" />\n";
00299 if(isset($sectionItem['link']))
00300 $sectionXml .= " <link href=\"$sectionItem[link]\" />\n";
00301
00302 $sectionXml .= " </sectionData>\n";
00303 }
00304
00305 if(isset($sectionItem['content']))
00306 $sectionXml .= " <p>\n $sectionItem[content]\n </p>\n";
00307
00308 return $sectionXml;
00309 }
00310 }
00311 }
00312 ?>