Example #1
0
 public static function load($path)
 {
     if (isset(self::$sections[$path]) and self::$sections[$path] instanceof Section) {
         return self::$sections[$path];
     }
     $section = new self();
     $section->handle = preg_replace('/\\.xml$/', NULL, basename($path));
     $section->path = dirname($path);
     if (!file_exists($path)) {
         throw new SectionException(__('Section `%s` could not be found.', array(basename($path))), self::ERROR_SECTION_NOT_FOUND);
     }
     $doc = @simplexml_load_file($path);
     if (!$doc instanceof SimpleXMLElement) {
         throw new SectionException(__('Failed to load section configuration file: %s', array($path)), self::ERROR_FAILED_TO_LOAD);
     }
     foreach ($doc as $name => $value) {
         if ($name == 'fields' && isset($value->field)) {
             foreach ($value->field as $field) {
                 try {
                     $section->appendField(Field::loadFromXMLDefinition($field));
                 } catch (Exception $e) {
                     // Couldnt find the field. Ignore it for now
                     // TODO: Might need to more than just ignore it
                 }
             }
         } elseif ($name == 'layout' && isset($value->column)) {
             $data = array();
             foreach ($value->column as $column) {
                 if (!isset($column->size)) {
                     $size = Layout::LARGE;
                 } else {
                     $size = (string) $column->size;
                 }
                 $data_column = (object) array('size' => $size, 'fieldsets' => array());
                 foreach ($column->fieldset as $fieldset) {
                     if (isset($fieldset->name) or trim((string) $fieldset->name) == '') {
                         $name = (string) $fieldset->name;
                     }
                     $data_fieldset = (object) array('name' => $name, 'collapsed' => isset($fieldset->collapsed) ? (string) $fieldset->collapsed : 'no', 'fields' => array());
                     foreach ($fieldset->field as $field) {
                         $data_fieldset->fields[] = (string) $field;
                     }
                     $data_column->fieldsets[] = $data_fieldset;
                 }
                 $data[] = $data_column;
             }
             $section->layout = $data;
         } elseif (isset($value->item)) {
             $stack = array();
             foreach ($value->item as $item) {
                 array_push($stack, (string) $item);
             }
             $section->{$name} = $stack;
         } else {
             $section->{$name} = (string) $value;
         }
     }
     $section->sanitizeLayout(true);
     if (isset($doc->attributes()->guid)) {
         $section->guid = (string) $doc->attributes()->guid;
     }
     self::$sections[$path] = $section;
     return $section;
 }