PageRenderTime 68ms CodeModel.GetById 2ms app.highlight 58ms RepoModel.GetById 1ms app.codeStats 0ms

/web/concrete/core/libraries/content/importer.php

https://github.com/myconcretelab/concrete5
PHP | 761 lines | 681 code | 61 blank | 19 comment | 132 complexity | 4e6004c2441a635f39900fc3ee064a9d MD5 | raw file
  1<?
  2
  3/**
  4 * @package Core
  5 * @author Andrew Embler <andrew@concrete5.org>
  6 * @copyright  Copyright (c) 2003-2011 Concrete5. (http://www.concrete5.org)
  7 * @license    http://www.concrete5.org/license/     MIT License
  8 *
  9 */
 10
 11/**
 12 * A way to import concrete5 content.
 13 * @package Core
 14 * @author Andrew Embler <andrew@concrete5.org>
 15 * @copyright  Copyright (c) 2003-2011 Concrete5. (http://www.concrete5.org)
 16 * @license    http://www.concrete5.org/license/     MIT License
 17 *
 18 */
 19
 20defined('C5_EXECUTE') or die("Access Denied.");
 21class Concrete5_Library_Content_Importer {
 22	
 23	protected static $mcBlockIDs = array();
 24	protected static $cmpOutputControlIDs = array();
 25
 26	public function importContentFile($file) {
 27		$sx = simplexml_load_file($file);
 28		$this->importSinglePageStructure($sx);
 29		$this->importStacksStructure($sx);
 30		$this->importBlockTypes($sx);
 31		$this->importBlockTypeSets($sx);
 32		$this->importConversationEditors($sx);
 33		$this->importConversationRatingTypes($sx);
 34		$this->importConversationFlagTypes($sx);
 35		$this->importComposerTargetTypes($sx);
 36		$this->importComposerControlTypes($sx);
 37		$this->importBannedWords($sx);
 38		$this->importFeatures($sx);
 39		$this->importFeatureCategories($sx);
 40		$this->importGatheringDataSources($sx);
 41		$this->importGatheringItemTemplateTypes($sx);
 42		$this->importGatheringItemTemplates($sx);
 43		$this->importAttributeCategories($sx);
 44		$this->importAttributeTypes($sx);
 45		$this->importWorkflowTypes($sx);
 46		$this->importWorkflowProgressCategories($sx);
 47		$this->importAttributes($sx);
 48		$this->importAttributeSets($sx);
 49		$this->importThemes($sx);
 50		$this->importPermissionCategories($sx);
 51		$this->importPermissionAccessEntityTypes($sx);
 52		$this->importTaskPermissions($sx);
 53		$this->importPermissions($sx);
 54		$this->importJobs($sx);
 55		$this->importJobSets($sx);
 56		// import bare page types first, then import structure, then page types blocks, attributes and composer settings, then page content, because we need the structure for certain attributes and stuff set in master collections (like composer)
 57		$this->importPageTypesBase($sx);
 58		$this->importPageStructure($sx);
 59		$this->importPageTypeDefaults($sx);
 60		$this->importSinglePageContent($sx);
 61		$this->importStacksContent($sx);
 62		$this->importPageContent($sx);
 63		$this->importPackages($sx);
 64		$this->importConfigValues($sx);
 65		$this->importSystemCaptchaLibraries($sx);
 66		$this->importSystemContentEditorSnippets($sx);
 67		$this->importComposers($sx);
 68	}
 69	
 70	protected static function getPackageObject($pkgHandle) {
 71		$pkg = false;
 72		if ($pkgHandle) {
 73			$pkg = Package::getByHandle($pkgHandle);
 74		}
 75		return $pkg;		
 76	}
 77
 78	protected function importStacksStructure(SimpleXMLElement $sx) {
 79		if (isset($sx->stacks)) {
 80			foreach($sx->stacks->stack as $p) {
 81				if (isset($p['type'])) {
 82					$type = Stack::mapImportTextToType($p['type']);
 83					Stack::addStack($p['name'], $type);
 84				} else {
 85					Stack::addStack($p['name']);
 86				}
 87			}
 88		}
 89	}
 90
 91	protected function importStacksContent(SimpleXMLElement $sx) {
 92		if (isset($sx->stacks)) {
 93			foreach($sx->stacks->stack as $p) {
 94				$stack = Stack::getByName($p['name']);
 95				if (isset($p->area)) {
 96					$this->importPageAreas($stack, $p);
 97				}
 98			}
 99		}
100	}
101	
102	protected function importSinglePageStructure(SimpleXMLElement $sx) {
103		if (isset($sx->singlepages)) {
104			foreach($sx->singlepages->page as $p) {
105				$pkg = ContentImporter::getPackageObject($p['package']);
106				$spl = SinglePage::add($p['path'], $pkg);
107				if (is_object($spl)) { 
108					if (isset($p['root']) && $p['root'] == true) {
109						$spl->moveToRoot();
110					}
111					if ($p['name']) {
112						$spl->update(array('cName' => $p['name'], 'cDescription' => $p['description']));
113					}
114				}
115			}
116		}
117	}
118
119	protected function importSinglePageContent(SimpleXMLElement $sx) {
120		if (isset($sx->singlepages)) {
121			foreach($sx->singlepages->page as $px) {
122				$page = Page::getByPath($px['path'], 'RECENT');
123				if (isset($px->area)) {
124					$this->importPageAreas($page, $px);
125				}
126				if (isset($px->attributes)) {
127					foreach($px->attributes->children() as $attr) {
128						$ak = CollectionAttributeKey::getByHandle($attr['handle']);
129						if (is_object($ak)) { 
130							$page->setAttribute((string) $attr['handle'], $ak->getController()->importValue($attr));
131						}
132					}
133				}
134			}
135		}
136	}
137
138	protected function setupPageNodeOrder($pageNodeA, $pageNodeB) {
139		$pathA = (string) $pageNodeA['path'];
140		$pathB = (string) $pageNodeB['path'];
141		$numA = count(explode('/', $pathA));
142		$numB = count(explode('/', $pathB));
143		if ($numA == $numB) {
144			if (intval($pageNodeA->originalPos) < intval($pageNodeB->originalPos)) {
145				return -1;
146			} else if (intval($pageNodeA->originalPos) > intval($pageNodeB->originalPos)) {
147				return 1;
148			} else {
149				return 0;
150			}
151		} else {
152			return ($numA < $numB) ? -1 : 1;
153		}
154	}
155	
156	protected function importPageContent(SimpleXMLElement $sx) {
157		if (isset($sx->pages)) {
158			foreach($sx->pages->page as $px) {
159				if ($px['path'] != '') {
160					$page = Page::getByPath($px['path'], 'RECENT');
161				} else {
162					$page = Page::getByID(HOME_CID, 'RECENT');
163				}
164				if (isset($px->area)) {
165					$this->importPageAreas($page, $px);
166				}
167				if (isset($px->attributes)) {
168					foreach($px->attributes->children() as $attr) {
169						$ak = CollectionAttributeKey::getByHandle($attr['handle']);
170						if (is_object($ak)) { 
171							$page->setAttribute((string) $attr['handle'], $ak->getController()->importValue($attr));
172						}
173					}
174				}
175				$page->reindex();
176			}
177		}
178	}
179	
180	protected function importPageStructure(SimpleXMLElement $sx) {
181		if (isset($sx->pages)) {
182			$nodes = array();
183			$i = 0;
184			foreach($sx->pages->page as $p) {
185				$p->originalPos = $i;
186				$nodes[] = $p;
187				$i++;
188			}
189			usort($nodes, array('ContentImporter', 'setupPageNodeOrder'));
190			$home = Page::getByID(HOME_CID, 'RECENT');
191
192			foreach($nodes as $px) {
193				$pkg = ContentImporter::getPackageObject($px['package']);
194				$data = array();
195				$user = (string) $px['user'];
196				if ($user != '') {
197					$ui = UserInfo::getByUserName($user);
198					if (is_object($ui)) {
199						$data['uID'] = $ui->getUserID();
200					} else {
201						$data['uID'] = USER_SUPER_ID;
202					}	
203				}
204				$cDatePublic = (string) $px['public-date'];
205				if ($cDatePublic) {
206					$data['cDatePublic'] = $cDatePublic;
207				}
208
209				$data['pkgID'] = 0;
210				if (is_object($pkg)) {
211					$data['pkgID'] = $pkg->getPackageID();
212				}
213				$args = array();
214				$ct = CollectionType::getByHandle($px['pagetype']);
215				if ($px['path'] == '') {
216					// home page
217					$page = $home;
218					$args['ctID'] = $ct->getCollectionTypeID();
219				} else {
220					$page = Page::getByPath($px['path']);
221					if (!is_object($page) || ($page->isError())) {
222						$lastSlash = strrpos((string) $px['path'], '/');
223						$parentPath = substr((string) $px['path'], 0, $lastSlash);
224						$data['cHandle'] = substr((string) $px['path'], $lastSlash + 1);
225						if (!$parentPath) {
226							$parent = $home;
227						} else {
228							$parent = Page::getByPath($parentPath);
229						}
230						$page = $parent->add($ct, $data);
231					}
232				}
233
234				$args['cName'] = $px['name'];
235				$args['cDescription'] = $px['description'];
236				$args['ctID'] = $ct->getCollectionTypeID();
237				$page->update($args);
238			}
239		}
240	}
241	
242	protected function importPageAreas(Page $page, SimpleXMLElement $px) {
243		foreach($px->area as $ax) {
244			if (isset($ax->block)) {
245				foreach($ax->block as $bx) {
246					if ($bx['type'] != '') {
247						// we check this because you might just get a block node with only an mc-block-id, if it's an alias
248						$bt = BlockType::getByHandle($bx['type']);
249						$btc = $bt->getController();
250						$btc->import($page, (string) $ax['name'], $bx);
251					} else if ($bx['mc-block-id'] != '') {
252					
253						// we find that block in the master collection block pool and alias it out
254						$bID = array_search((string) $bx['mc-block-id'], self::$mcBlockIDs);
255						if ($bID) {
256							$mc = Page::getByID($page->getMasterCollectionID(), 'RECENT');
257							$block = Block::getByID($bID, $mc, (string) $ax['name']);
258							$block->alias($page);
259						}
260					}
261				}
262			}
263		}
264	}
265
266	public static function addMasterCollectionBlockID($b, $id) {
267		self::$mcBlockIDs[$b->getBlockID()] = $id;
268	}
269	
270	public static function getMasterCollectionTemporaryBlockID($b) {
271		if (isset(self::$mcBlockIDs[$b->getBlockID()])) {
272			return self::$mcBlockIDs[$b->getBlockID()];
273		}
274	}
275
276	public static function addComposerOutputControlID(ComposerFormLayoutSetControl $control, $id) {
277		self::$cmpOutputControlIDs[$id] = $control->getComposerFormLayoutSetControlID();
278	}
279	
280	public static function getComposerFormLayoutSetControlFromTemporaryID($id) {
281		if (isset(self::$cmpOutputControlIDs[$id])) {
282			return self::$cmpOutputControlIDs[$id];
283		}
284	}
285	
286	protected function importPageTypesBase(SimpleXMLElement $sx) {
287		if (isset($sx->pagetypes)) {
288			foreach($sx->pagetypes->pagetype as $ct) {
289				$pkg = ContentImporter::getPackageObject($ct['package']);
290				$ctt = CollectionType::getByHandle($ct['handle']);
291				if (!is_object($ctt)) { 
292					$ctr = CollectionType::add(array(
293						'ctHandle' => $ct['handle'],
294						'ctName' => $ct['name'],
295						'ctIcon' => $ct['icon'],
296						'ctIsInternal' => (string) $ct['internal']
297					), $pkg);
298				}
299			}
300		}
301	}
302
303	protected function importPageTypeDefaults(SimpleXMLElement $sx) {
304		$db = Loader::db();
305		if (isset($sx->pagetypes)) {
306			foreach($sx->pagetypes->pagetype as $ct) {
307				$ctr = CollectionType::getByHandle((string) $ct['handle']);
308				$mc = Page::getByID($ctr->getMasterCollectionID(), 'RECENT');
309				if (isset($ct->page)) {
310					$this->importPageAreas($mc, $ct->page);
311				}
312			}
313		}
314	}
315
316	protected function importBlockTypes(SimpleXMLElement $sx) {
317		if (isset($sx->blocktypes)) {
318			foreach($sx->blocktypes->blocktype as $bt) {
319				$pkg = ContentImporter::getPackageObject($bt['package']);
320				if (is_object($pkg)) {
321					BlockType::installBlockTypeFromPackage($bt['handle'], $pkg);
322				} else {
323					BlockType::installBlockType($bt['handle']);				
324				}
325			}
326		}
327	}
328
329	protected function importWorkflowTypes(SimpleXMLElement $sx) {
330		if (isset($sx->workflowtypes)) {
331			foreach($sx->workflowtypes->workflowtype as $wt) {
332				$pkg = ContentImporter::getPackageObject($wt['package']);
333				$name = $wt['name'];
334				if (!$name) {
335					$name = Loader::helper('text')->unhandle($wt['handle']);
336				}
337				$type = WorkflowType::add($wt['handle'], $name, $pkg);
338			}
339		}
340	}
341
342	protected function importAttributeTypes(SimpleXMLElement $sx) {
343		if (isset($sx->attributetypes)) {
344			foreach($sx->attributetypes->attributetype as $at) {
345				$pkg = ContentImporter::getPackageObject($at['package']);
346				$name = $at['name'];
347				if (!$name) {
348					$name = Loader::helper('text')->unhandle($at['handle']);
349				}
350				$type = AttributeType::add($at['handle'], $name, $pkg);
351				if (isset($at->categories)) {
352					foreach($at->categories->children() as $cat) {
353						$catobj = AttributeKeyCategory::getByHandle((string) $cat['handle']);
354						$catobj->associateAttributeKeyType($type);
355					}
356				}
357			}
358		}
359	}
360
361	protected function importPermissionAccessEntityTypes(SimpleXMLElement $sx) {
362		if (isset($sx->permissionaccessentitytypes)) {
363			foreach($sx->permissionaccessentitytypes->permissionaccessentitytype as $pt) {
364				$pkg = ContentImporter::getPackageObject($pt['package']);
365				$name = $pt['name'];
366				if (!$name) {
367					$name = Loader::helper('text')->unhandle($pt['handle']);
368				}
369				$type = PermissionAccessEntityType::add($pt['handle'], $name, $pkg);
370				if (isset($pt->categories)) {
371					foreach($pt->categories->children() as $cat) {
372						$catobj = PermissionKeyCategory::getByHandle((string) $cat['handle']);
373						$catobj->associateAccessEntityType($type);
374					}
375				}
376			}
377		}
378	}
379	
380	protected function importPackages(SimpleXMLElement $sx) {
381		if (isset($sx->packages)) {
382			foreach($sx->packages->package as $p) {
383				$pkg = Loader::package((string) $p['handle']);
384				$pkg->install();
385			}
386		}
387	}
388	
389	protected function importThemes(SimpleXMLElement $sx) {
390		if (isset($sx->themes)) {
391			foreach($sx->themes->theme as $th) {
392				$pkg = ContentImporter::getPackageObject($th['package']);
393				$ptHandle = (string) $th['handle'];
394				$pt = PageTheme::getByHandle($ptHandle);
395				if (!is_object($pt)) {
396					$pt = PageTheme::add($ptHandle, $pkg);
397				}
398				if ($th['activated'] == '1') {
399					$pt->applyToSite();
400				}
401			}
402		}
403	}
404
405	protected function importComposerTargetTypes(SimpleXMLElement $sx) {
406		if (isset($sx->composertargettypes)) {
407			foreach($sx->composertargettypes->type as $th) {
408				$pkg = ContentImporter::getPackageObject($th['package']);
409				$ce = ComposerTargetType::add((string) $th['handle'], (string) $th['name'], $pkg);
410			}
411		}
412	}
413
414	protected function importComposerControlTypes(SimpleXMLElement $sx) {
415		if (isset($sx->composercontroltypes)) {
416			foreach($sx->composercontroltypes->type as $th) {
417				$pkg = ContentImporter::getPackageObject($th['package']);
418				$ce = ComposerControlType::add((string) $th['handle'], (string) $th['name'], $pkg);
419			}
420		}
421	}
422
423	protected function importComposers(SimpleXMLElement $sx) {
424		if (isset($sx->composers)) {
425			foreach($sx->composers->composer as $cm) {
426				Composer::import($cm);
427			}
428		}
429	}
430
431	protected function importConversationEditors(SimpleXMLElement $sx) {
432		if (isset($sx->conversationeditors)) {
433			foreach($sx->conversationeditors->editor as $th) {
434				$pkg = ContentImporter::getPackageObject($th['package']);
435				$ce = ConversationEditor::add((string) $th['handle'], (string) $th['name'], $pkg);
436				if ($th['activated'] == '1') {
437					$ce->activate();
438				}
439			}
440		}
441	}
442
443	protected function importConversationRatingTypes(SimpleXMLElement $sx) {
444		if (isset($sx->conversationratingtypes)) {
445			foreach($sx->conversationratingtypes->conversationratingtype as $th) {
446				$pkg = ContentImporter::getPackageObject($th['package']);
447				$ce = ConversationRatingType::add((string) $th['handle'], (string) $th['name'], $th['points'], $pkg);
448			}
449		}
450	}
451
452
453	protected function importBannedWords(SimpleXMLElement $sx) {
454		if (isset($sx->banned_words)) {
455			foreach($sx->banned_words->banned_word as $p) {
456				$bw = BannedWord::add(str_rot13($p));
457			}
458		}
459	}
460
461	protected function importConversationFlagTypes(SimpleXMLElement $sx) {
462		if (isset($sx->flag_types)) {
463			foreach($sx->flag_types->flag_type as $p) {
464				$bw = ConversationFlagType::add($p);
465			}
466		}
467	}
468
469	protected function importSystemCaptchaLibraries(SimpleXMLElement $sx) {
470		if (isset($sx->systemcaptcha)) {
471			Loader::model('system/captcha/library');
472			foreach($sx->systemcaptcha->library as $th) {
473				$pkg = ContentImporter::getPackageObject($th['package']);
474				$scl = SystemCaptchaLibrary::add($th['handle'], $th['name'], $pkg);
475				if ($th['activated'] == '1') {
476					$scl->activate();
477				}
478			}
479		}
480	}
481
482	protected function importSystemContentEditorSnippets(SimpleXMLElement $sx) {
483		if (isset($sx->systemcontenteditorsnippets)) {
484			foreach($sx->systemcontenteditorsnippets->snippet as $th) {
485				$pkg = ContentImporter::getPackageObject($th['package']);
486				$scs = SystemContentEditorSnippet::add($th['handle'], $th['name'], $pkg);
487				if ($th['activated'] == '1') {
488					$scs->activate();
489				}
490			}
491		}
492	}
493
494	protected function importJobs(SimpleXMLElement $sx) {
495		Loader::model('job');
496		if (isset($sx->jobs)) {
497			foreach($sx->jobs->job as $jx) {
498				$pkg = ContentImporter::getPackageObject($jx['package']);
499				if (is_object($pkg)) {
500					Job::installByPackage($jx['handle'], $pkg);
501				} else {
502					Job::installByHandle($jx['handle']);				
503				}
504			}
505		}
506	}
507
508	protected function importJobSets(SimpleXMLElement $sx) {
509		if (isset($sx->jobsets)) {
510			foreach($sx->jobsets->jobset as $js) {
511				$pkg = ContentImporter::getPackageObject($js['package']);
512				$jso = JobSet::getByName((string) $js['name']);
513				if (!is_object($jso)) {
514					$jso = JobSet::add((string) $js['name']);
515				}
516				foreach($js->children() as $jsk) {
517					$j = Job::getByHandle((string) $jsk['handle']);
518					if (is_object($j)) { 	
519						$jso->addJob($j);
520					}
521				}
522			}
523		}
524	}
525
526	protected function importConfigValues(SimpleXMLElement $sx) {
527		if (isset($sx->config)) {
528			$db = Loader::db();
529			$configstore = new ConfigStore($db);
530			foreach($sx->config->children() as $key) {
531				$pkg = ContentImporter::getPackageObject($key['package']);
532				if (is_object($pkg)) {
533					$configstore->set($key->getName(), (string) $key, $pkg->getPackageID());
534				} else {
535					$configstore->set($key->getName(), (string) $key);
536				}
537			}
538		}
539	}
540
541	protected function importTaskPermissions(SimpleXMLElement $sx) {
542		if (isset($sx->taskpermissions)) {
543			foreach($sx->taskpermissions->taskpermission as $tp) {
544				$pkg = ContentImporter::getPackageObject($tp['package']);
545				$tpa = TaskPermission::addTask($tp['handle'], $tp['name'], $tp['description'], $pkg);
546				if (isset($tp->access)) {
547					foreach($tp->access->children() as $ch) {
548						if ($ch->getName() == 'group') {
549							$g = Group::getByName($ch['name']);
550							if (!is_object($g)) {
551								$g = Group::add($ch['name'], $ch['description']);
552							}
553							$tpa->addAccess($g);
554						}
555					}
556				}
557			}
558		}
559	}
560
561	protected function importPermissionCategories(SimpleXMLElement $sx) {
562		if (isset($sx->permissioncategories)) {
563			foreach($sx->permissioncategories->category as $pkc) {
564				$pkg = ContentImporter::getPackageObject($akc['package']);
565				$pkx = PermissionKeyCategory::add((string) $pkc['handle'], $pkg);
566			}
567		}
568	}
569
570	protected function importWorkflowProgressCategories(SimpleXMLElement $sx) {
571		if (isset($sx->workflowprogresscategories)) {
572			foreach($sx->workflowprogresscategories->category as $wpc) {
573				$pkg = ContentImporter::getPackageObject($wpc['package']);
574				$wkx = WorkflowProgressCategory::add((string) $wpc['handle'], $pkg);
575			}
576		}
577	}
578
579	protected function importPermissions(SimpleXMLElement $sx) {
580		if (isset($sx->permissionkeys)) {
581			foreach($sx->permissionkeys->permissionkey as $pk) {
582				$pkc = PermissionKeyCategory::getByHandle((string) $pk['category']);
583				$pkg = ContentImporter::getPackageObject($pk['package']);
584				$txt = Loader::helper('text');
585				$className = $txt->camelcase($pkc->getPermissionKeyCategoryHandle());
586				$c1 = $className . 'PermissionKey';
587				$pkx = call_user_func(array($c1, 'import'), $pk);	
588				if (isset($pk->access)) {
589					foreach($pk->access->children() as $ch) {
590						if ($ch->getName() == 'group') {
591							$g = Group::getByName($ch['name']);
592							if (!is_object($g)) {
593								$g = Group::add($g['name'], $g['description']);
594							}
595							$pae = GroupPermissionAccessEntity::getOrCreate($g);
596							$pa = PermissionAccess::create($pkx);
597							$pa->addListItem($pae);
598							$pt = $pkx->getPermissionAssignmentObject();
599							$pt->assignPermissionAccess($pa);
600						}
601					}
602				}
603			
604			}
605		}
606	}
607
608	protected function importFeatures(SimpleXMLElement $sx) {
609		if (isset($sx->features)) {
610			foreach($sx->features->feature as $fea) {
611				$feHasCustomClass = false;
612				if ($fea['has-custom-class']) {
613					$feHasCustomClass = true;
614				}
615				$pkg = ContentImporter::getPackageObject($fea['package']);
616				$fx = Feature::add((string) $fea['handle'], (string) $fea['score'], $feHasCustomClass, $pkg);
617			}
618		}
619	}
620
621	protected function importFeatureCategories(SimpleXMLElement $sx) {
622		if (isset($sx->featurecategories)) {
623			foreach($sx->featurecategories->featurecategory as $fea) {
624				$pkg = ContentImporter::getPackageObject($fea['package']);
625				$fx = FeatureCategory::add($fea['handle'], $pkg);
626			}
627		}
628	}
629	
630	protected function importAttributeCategories(SimpleXMLElement $sx) {
631		if (isset($sx->attributecategories)) {
632			foreach($sx->attributecategories->category as $akc) {
633				$pkg = ContentImporter::getPackageObject($akc['package']);
634				$akx = AttributeKeyCategory::add($akc['handle'], $akc['allow-sets'], $pkg);
635			}
636		}
637	}
638	
639	protected function importAttributes(SimpleXMLElement $sx) {
640		if (isset($sx->attributekeys)) {
641			foreach($sx->attributekeys->attributekey as $ak) {
642				$akc = AttributeKeyCategory::getByHandle($ak['category']);
643				$pkg = ContentImporter::getPackageObject($ak['package']);
644				$type = AttributeType::getByHandle($ak['type']);
645				$txt = Loader::helper('text');
646				$className = $txt->camelcase($akc->getAttributeKeyCategoryHandle());
647				$c1 = $className . 'AttributeKey';
648				$ak = call_user_func(array($c1, 'import'), $ak);				
649			}
650		}
651	}
652
653	protected function importAttributeSets(SimpleXMLElement $sx) {
654		if (isset($sx->attributesets)) {
655			foreach($sx->attributesets->attributeset as $as) {
656				$akc = AttributeKeyCategory::getByHandle($as['category']);
657				$pkg = ContentImporter::getPackageObject($as['package']);
658				$set = $akc->addSet((string) $as['handle'], (string) $as['name'], $pkg, $as['locked']);
659				foreach($as->children() as $ask) {
660					$ak = $akc->getAttributeKeyByHandle((string) $ask['handle']);
661					if (is_object($ak)) { 	
662						$set->addKey($ak);
663					}
664				}
665			}
666		}
667	}
668
669	protected function importGatheringDataSources(SimpleXMLElement $sx) {
670		if (isset($sx->gatheringsources)) {
671			foreach($sx->gatheringsources->gatheringsource as $ags) {
672				$pkg = ContentImporter::getPackageObject($ags['package']);
673				$source = GatheringDataSource::add((string) $ags['handle'], (string) $ags['name'], $pkg);
674			}
675		}
676	}
677
678	protected function importGatheringItemTemplateTypes(SimpleXMLElement $sx) {
679		if (isset($sx->gatheringitemtemplatetypes)) {
680			foreach($sx->gatheringitemtemplatetypes->gatheringitemtemplatetype as $at) {
681				$pkg = ContentImporter::getPackageObject($wt['package']);
682				$type = GatheringItemTemplateType::add((string) $at['handle'], $pkg);
683			}
684		}
685	}
686
687
688	protected function importGatheringItemTemplates(SimpleXMLElement $sx) {
689		if (isset($sx->gatheringitemtemplates)) {
690			foreach($sx->gatheringitemtemplates->gatheringitemtemplate as $at) {
691				$pkg = ContentImporter::getPackageObject($at['package']);
692				$type = GatheringItemTemplateType::getByHandle((string) $at['type']);
693				$gatHasCustomClass = false;
694				$gatForceDefault = false;
695				$gatFixedSlotWidth = 0;
696				$gatFixedSlotHeight = 0;
697				if ($at['has-custom-class']) {
698					$gatHasCustomClass = true;
699				}
700				if ($at['force-default']) {
701					$gatForceDefault = true;
702				}
703				if ($at['fixed-slot-width']) {
704					$gatFixedSlotWidth = (string) $at['fixed-slot-width'];
705				}
706				if ($at['fixed-slot-height']) {
707					$gatFixedSlotHeight = (string) $at['fixed-slot-height'];
708				}
709				$template = GatheringItemTemplate::add($type, (string) $at['handle'], (string) $at['name'], $gatFixedSlotWidth, $gatFixedSlotHeight, $gatHasCustomClass, $gatForceDefault, $pkg);
710				foreach($at->children() as $fe) {
711					$feo = Feature::getByHandle((string) $fe['handle']);
712					if (is_object($feo)) { 	
713						$template->addGatheringItemTemplateFeature($feo);
714					}
715				}
716			}
717		}
718	}
719
720
721	protected function importBlockTypeSets(SimpleXMLElement $sx) {
722		if (isset($sx->blocktypesets)) {
723			foreach($sx->blocktypesets->blocktypeset as $bts) {
724				$pkg = ContentImporter::getPackageObject($bts['package']);
725				$set = BlockTypeSet::add((string) $bts['handle'], (string) $bts['name'], $pkg);
726				foreach($bts->children() as $btk) {
727					$bt = BlockType::getByHandle((string) $btk['handle']);
728					if (is_object($bt)) { 	
729						$set->addBlockType($bt);
730					}
731				}
732			}
733		}
734	}
735
736	public static function getValue($value) {
737		if (preg_match('/\{ccm:export:page:(.*)\}|\{ccm:export:file:(.*)\}|\{ccm:export:image:(.*)\}|\{ccm:export:pagetype:(.*)\}/i', $value, $matches)) {
738			if ($matches[1]) {
739				$c = Page::getByPath($matches[1]);
740				return $c->getCollectionID();
741			}
742			if ($matches[2]) {
743				$db = Loader::db();
744				$fID = $db->GetOne('select fID from FileVersions where fvFilename = ?', array($matches[2]));
745				return $fID;
746			}
747			if ($matches[3]) {
748				$db = Loader::db();
749				$fID = $db->GetOne('select fID from FileVersions where fvFilename = ?', array($matches[3]));
750				return $fID;
751			}
752			if ($matches[4]) {
753				$ct = CollectionType::getByHandle($matches[4]);
754				return $ct->getCollectionTypeID();
755			}
756		} else {
757			return $value;
758		}
759	}	
760
761}