Пример #1
0
function importLines($lines, $fields, $class_name, $parent)
{
	$count = 0;
	foreach ($lines as $line)
	{
		$line = trim($line);
	
		if (empty($line))
			continue;
	
		$array = explode(";", $line);
		
		$newobject = new mObject();
		$newobject->setClassName($class_name);
		$newobject->loadVars();

		$newobject->name = trim($array[$fields['name']]);
		$newobject->language = $_SESSION['murrix']['language'];
		$newobject->rights = $parent->getMeta("initial_rights", $parent->getRights());

		$vars = $newobject->getVars();

		foreach ($vars as $var)
		{
			if (!isset($fields[$var->name]))
				continue;
				
			$value = $array[$fields[$var->name]];
			$newobject->setVarValue($var->name, $value);
		}

		if ($newobject->save())
		{
			guessObjectType($newobject);
			clearNodeFileCache($parent->getNodeId());
			$newobject->linkWithNode($parent->getNodeId());
			$count++;
		}
		else
		{
			return mMsg::add("importLines", $object->getLastError(), true);
		}
	}
	
	return $count;
}
Пример #2
0
	function parseXML($args)
	{
		// Instantiate the serializer
		$Unserializer = &new XML_Unserializer();
		
		// Serialize the data structure
		$status = $Unserializer->unserialize($args['data']);
		
		// Check whether serialization worked
		if (PEAR::isError($status))
			die($status->getMessage());
		
		// Display the PHP data structure
		$import_data = $Unserializer->getUnserializedData();
		
		$parent = new mObject($args['node_id']);
		
		$logtext = "";
	
		if (isset($import_data['version']))
			$logtext .= "Version: ".$import_data['version']."<br/>";
		
		if (isset($import_data['name']))
			$logtext .= "Created: ".$import_data['created']."<br/>";
		
		if (isset($import_data['name']))
			$logtext .= "Name: ".$import_data['name']."<br/>";
			
		if (isset($import_data['description']))
			$logtext .= "Description: ".$import_data['description']."<br/>";
		
		$imported_classes = array();
		$imported_nodes = array();
		$imported_links = array();
		$imported_users = array();
		$imported_groups = array();
		
		if (isset($import_data['container']['xmldata']))
			$import_data['container'] = array($import_data['container']);
		
		foreach ($import_data['container'] as $container)
		{
			switch ($container['xmldata'])
			{
				case "node":
				$imported_nodes[] = $container;
				break;
				
				case "class":
				$imported_classes[] = $container;
				break;
				
				case "link":
				$imported_links[] = $container;
				break;
				
				case "user":
				$imported_users[] = $container;
				break;
				
				case "group":
				$imported_groups[] = $container;
				break;
			}
			
		}
		
		if (count($imported_classes) > 0)
			$logtext .= "Found ".count($imported_classes)." classes<br/>";
		if (count($imported_nodes) > 0)
			$logtext .= "Found ".count($imported_nodes)." nodes<br/>";
		if (count($imported_links) > 0)
			$logtext .= "Found ".count($imported_links)." links<br/>";
		if (count($imported_users) > 0)
			$logtext .= "Found ".count($imported_users)." users<br/>";
		if (count($imported_groups) > 0)
			$logtext .= "Found ".count($imported_groups)." groups<br/>";
		
		$class_table = new mTable("classes");
		$vars_table = new mTable("vars");
		$initial_meta_table = new mTable("initial_meta");
		
		foreach ($imported_classes as $class)
		{
			$logtext .= "Checking database for existing class ".$class['name']."<br/>";
			
			$existing_class = $class_table->get("`name`='".$class['name']."'");
			
			if (count($existing_class) == 0)
			{
				$logtext .= "No existing class named ".$class['name']." found, creating<br/>";
				
				$class_table->insert(array("name"=>$class['name'], "default_icon"=>$class['icon']));
				
				if (count($class['variables']) > 0)
				{
					foreach ($class['variables'] as $varname => $properties)
						$vars_table->insert(array("class_name"=>$class['name'], "name"=>$varname, "priority"=>$properties['priority'], "required"=>$properties['required'], "type"=>$properties['type'], "extra"=>$properties['extra'], "comment"=>$properties['comment']));
				}
				
				if (count($class['metadata']) > 0)
				{
					foreach ($class['metadata'] as $name => $value)
						$initial_meta_table->insert(array("class_name"=>$class['name'], "name"=>$name, "value"=>$value));
				}
				
				continue;
			}
			else
				$logtext .= "Class named ".$class['name']." found, checking variables<br/>";
			
			$class_object = new mObject();
			$class_object->setClassName($class['name']);
			$class_object->loadVars();
			
			foreach ($class['variables'] as $varname => $properties)
			{
				if ($class_object->checkVarExistance($varname) == 0)
				{
					$logtext .= "Could not resolve - $varname, skipping, this data will be ignored!<br/>";
					continue;
				}
			}
		}
		
		$nodes_to_link = array();
		$id_conversion = array();
		$added_node_ids = array();
		$linked_node_ids = array();
		
		$node_table = new mTable("nodes");
		
		foreach ($imported_nodes as $container)
		{
			if (!isset($container['created']))
				$container['created'] = date("Y-m-d H:i:s");
		
			if (isset($container['rights']))
				$rights = $container['rights'];
			else
				$rights = $parent->getRights();
		
			$node_id = $node_table->insert(array("created" => $container['created'], "rights" => $rights));
			
			$node = new mObject();
			$node->node_id = $node_id;
			
			$id_conversion[$container['id']] = $node_id;
			$added_node_ids[] = $node_id;
			
			if (is_array($container['metadata']))
			{
				foreach ($container['metadata'] as $key => $value)
				{
					$node->setMeta($key, $value);
					$logtext .= "Created metadata name=".$key.", value=".$value.", node_id=".$node_id."<br/>";
				}
			}
			
			foreach ($container['objects'] as $object_array)
			{
				$object = new mObject();
				$object->setClassName($object_array['class_name']);
				$object->loadVars();
				
				if (!isset($object_array['created']))
					$object_array['created'] = date("Y-m-d H:i:s");
				
				$object->node_id = $node_id;
				
				if (!empty($object_array['created']))
					$object->created = $object_array['created'];
				else
					$object->created = date("Y-m-d H:i:s");
					
				if (!empty($object_array['version']))
					$object->version = $object_array['version'];
				else
					$object->version = 1;
					
				$object->class_name = $object_array['class_name'];
				$object->name = $object_array['name'];
				$object->icon = $object_array['icon'];
				$object->language = $object_array['language'];
				
				if (isset($object_array['user']))
				{
					$user = new mUser();
					$user->setByUsername($object_array['user']);
					$object->user_id = $user->id;
				}
				else
					$object->user_id = $_SESSION['murrix']['user']->id;
				
				if (is_array($object_array['variables']))
				{
					foreach ($object_array['variables'] as $key => $value)
					{
						if ($object->checkVarExistance($key) == 0)
						{
							$logtext .= "Could not resolve - $key, skipping<br/>";
							continue;
						}
						
						if (!is_array($value['value']))
							$value['value'] = html_entity_decode($value['value']);
							
						if ($value['type'] == "file")
						{
							$extension = strtolower(pathinfo($value['value'], PATHINFO_EXTENSION));
							$oldfile = $args['filepath']."/".$value['fileid'].".$extension";
							
							if (!file_exists($oldfile))
								$logtext .= "Could not find file - $oldfile, skipping<br/>";
							else
								$object->setVarValue($key, $value['value'].":$oldfile");
						}
						else if ($value['type'] == "thumbnail")
						{
							if (!empty($value['thumb_id']))
							{
								$oldfile = $args['thumbpath']."/".$value['fileid'].".jpg";
								
								if (!file_exists($oldfile))
									$logtext .= "Could not find thumbfile - $oldfile, skipping<br/>";
								else
									$object->setVarValue($key, $value['fileid'].".jpg:$oldfile");
							}
						}
						else
							$object->setVarValue($key, $value['value']);
					}
				}
				
				$object->save(true);
				guessObjectType($object);
				$logtext .= "Created object name=".$object->name.", node_id=$node_id, id=".$object->id.",version=".$object->version.",language=".$object->language."<br/>";
			}
		}
		
		$link_table = new mTable("links");
		
		foreach ($imported_links as $container)
		{
			$link_array = array();
			$link_array['node_top'] = $id_conversion[$container['node_top']];
			$link_array['node_bottom'] = $id_conversion[$container['node_bottom']];
			$link_array['type'] = $container['type'];
			
			$link_table->insert($link_array);
			$logtext .= "Linked node_top=".$link_array['node_top']." to node_bottom=".$link_array['node_bottom'].", type=".$link_array['type']."<br/>";
			
			if ($link_array['type'] == "sub")
				$linked_node_ids[] = $id_conversion[$container['node_bottom']];
		}
		
		$added_node_ids = array_unique($added_node_ids);
		$linked_node_ids = array_unique($linked_node_ids);
		
		$node_ids_to_link = array_diff($added_node_ids, $linked_node_ids);
		
		foreach ($node_ids_to_link as $node_id)
		{
			$link_array = array();
			$link_array['node_top'] = $args['node_id'];
			$link_array['node_bottom'] = $node_id;
			$link_array['type'] = "sub";
			
			$link_table->insert($link_array);
			$logtext .= "Linked node_top=".$link_array['node_top']." to node_bottom=".$link_array['node_bottom'].", type=".$link_array['type']."<br/>";
		}
		
		return mMsg::add("mXml::parseXML", $logtext, false);
	}