|
@@ -0,0 +1,108 @@
|
|
|
+<?php
|
|
|
+/**
|
|
|
+ * Created by PhpStorm.
|
|
|
+ * User: stanley-king
|
|
|
+ * Date: 16/5/9
|
|
|
+ * Time: 下午9:06
|
|
|
+ */
|
|
|
+
|
|
|
+class category_helper
|
|
|
+{
|
|
|
+ private $file_name = '';
|
|
|
+ private $mCategories = NULL;
|
|
|
+ private $mCids = NULL;
|
|
|
+
|
|
|
+ static private $stInstance = NULL;
|
|
|
+ static public function instance()
|
|
|
+ {
|
|
|
+ if(self::$stInstance == NULL) {
|
|
|
+ self::$stInstance = new category_helper();
|
|
|
+ }
|
|
|
+ return self::$stInstance;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function __construct()
|
|
|
+ {
|
|
|
+ $this->mCids = array();
|
|
|
+ $this->file_name = BASE_DATA_PATH . '/sales/category.xml';
|
|
|
+ self::load_xml($this->file_name);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function load_xml($file)
|
|
|
+ {
|
|
|
+ $xml = simplexml_load_file($file);
|
|
|
+ if($xml === false) {
|
|
|
+ throw new Exception("无法解析XML文件");
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->mCategories = array();
|
|
|
+ foreach($xml->children() as $xitem)
|
|
|
+ {
|
|
|
+ $item = array();
|
|
|
+ $attrs = $xitem->attributes();
|
|
|
+
|
|
|
+ $title = $attrs['title']->__toString();
|
|
|
+ $pid = $attrs['id']->__toString();
|
|
|
+ $cids = $attrs['cids']->__toString();
|
|
|
+ $img = $attrs['img']->__toString();
|
|
|
+
|
|
|
+ $item['title'] = $title;
|
|
|
+ $item['id'] = $pid;
|
|
|
+ $item['img'] = $img;
|
|
|
+
|
|
|
+ $item['subitem'] = array();
|
|
|
+ $this->add_cids($pid,$cids);
|
|
|
+
|
|
|
+ foreach($xitem->children() as $xsubitem) {
|
|
|
+ $subitem = array();
|
|
|
+ $sattrs = $xsubitem->attributes();
|
|
|
+
|
|
|
+ $title = $sattrs['title']->__toString();
|
|
|
+ $sid = $sattrs['id']->__toString();
|
|
|
+ $cids = $sattrs['cids']->__toString();
|
|
|
+ $img = $sattrs['img']->__toString();
|
|
|
+
|
|
|
+ $subitem['title'] = $title;
|
|
|
+ $subitem['id'] = $sid;
|
|
|
+ $subitem['img'] = $img;
|
|
|
+
|
|
|
+ array_push($item['subitem'],$subitem);
|
|
|
+
|
|
|
+ $this->add_cids($sid,$cids);
|
|
|
+ $this->add_cids($pid,$cids);
|
|
|
+ }
|
|
|
+
|
|
|
+ array_push($this->mCategories,$item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function add_cids($id,$cids)
|
|
|
+ {
|
|
|
+ if(!in_array($id,$this->mCids)) {
|
|
|
+ $this->mCids[$id] = array();
|
|
|
+ }
|
|
|
+ $cids = explode(',',$cids);
|
|
|
+ foreach($cids as $val)
|
|
|
+ {
|
|
|
+ $cur_cids = &$this->mCids[$id];
|
|
|
+ if(!empty($val) && !in_array($val,$cur_cids)) {
|
|
|
+ array_push($cur_cids,$val);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public function categories()
|
|
|
+ {
|
|
|
+ return $this->mCategories;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function cids($id)
|
|
|
+ {
|
|
|
+ if(!in_array($id,$this->mCids)) {
|
|
|
+ return array();
|
|
|
+ } else {
|
|
|
+ return $this->mCids[$id];
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|