Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $template_id = 4; //the id of the template you want to assign to your pages
- $prefix = 'modx'; // your table prefix
- $install = false; // true: will attempt to insert the fields, false: will print out the sql so you can insert it with a tool like phpMyAdmin
- if (isset($_POST['ul'])) {
- echo "<textarea cols='100%' rows='18'>" .
- parseList($_POST['ul']) .
- "</textarea>";
- }
- function insertPage($id,$title,$parent_id,$content = '',$menuindex)
- {
- global $template_id,$prefix,$install;
- $alias = makeAliasFromTitle($title);
- $query = "REPLACE INTO `{$prefix}_site_content`
- (`id`, `type`, `contentType`, `pagetitle`, `longtitle`, `description`,
- `alias`, `link_attributes`, `published`, `pub_date`, `unpub_date`, `parent`,
- `isfolder`, `introtext`, `content`, `richtext`, `template`, `menuindex`, `searchable`,
- `cacheable`, `createdby`, `createdon`, `editedby`, `editedon`, `deleted`, `deletedon`,
- `deletedby`, `publishedon`, `publishedby`, `menutitle`, `donthit`, `haskeywords`,
- `hasmetatags`, `privateweb`, `privatemgr`, `content_dispo`, `hidemenu`) VALUES
- ($id, 'document', 'text/html', '$title', '', '',
- '$alias', '', 1, 0, 0, $parent_id,
- 0, '', '$content', 1, $template_id, $menuindex, 1,
- 1, 1, ".time().", 1, ".time().", 0, 0,
- 0, ".time().", 1, '', 0, 0,
- 0, 0, 0, 0, 0);";
- if ($install) {
- $result = mysql_query($query);
- return(mysql_affected_rows());
- } else {
- return $query."\n";
- }
- }
- function makeAliasFromTitle($title)
- {
- $alias = strtolower($title);
- $alias = strtr($alias, array(chr(196) => 'Ae', chr(214) => 'Oe', chr(220) => 'Ue', chr(228) => 'ae', chr(246) => 'oe', chr(252) => 'ue', chr(223) => 'ss'));
- $alias = strip_tags($alias);
- $alias = preg_replace('/&.+?;/', '', $alias); // kill entities
- $alias = preg_replace('/[^\.%A-Za-z0-9 _-]/', '', $alias);
- $alias = preg_replace('/\s+/', '-', $alias);
- $alias = str_replace('_', '-', $alias);
- $alias = preg_replace('|-+|', '-', $alias);
- $alias = trim($alias, '-');
- return $alias;
- }
- function makeParentAFolder($id) {
- global $prefix;
- $query = "UPDATE `{$prefix}_site_content` SET isfolder=1 WHERE id='$id';";
- if ($install) {
- $result = mysql_query($query);
- return(mysql_affected_rows());
- } else {
- return $query."\n";
- }
- }
- function parseList($ul) {
- global $install;
- if ($install) {
- // make a connection to the database
- $host = 'localhost';
- $user = 'root';
- $pass = 'root';
- $data = 'modx';
- $conn = mysql_connect($host,$user,$pass);
- mysql_select_db($data,$conn);
- }
- //first lets make sure all uls and /uls are on their own line
- $ul = preg_replace("#(</?ul>)#","\n$1\n",$ul);
- // same for lis
- $ul = preg_replace("#(</?li>)#","\n$1\n",$ul);
- // now strip empty lines
- $array = explode("\n",$ul);
- $level = 0;
- $id = 100;
- $parent_id[1] = 0;
- $pages = '';
- $c = array();
- foreach($array as $k=>$v) {
- $v = trim($v);
- if (empty($v)) continue;
- // if we come across a ul then increment the level by 1
- if (strstr("<ul>",$v)) {
- $oldlevel = $level;
- $level += 1;
- $parent_id[$level]=$id;
- $c[$level] = 1;
- continue;
- // if we come across a /ul then decrement the level by 1
- } elseif (strstr("</ul>",$v)) {
- $c[$level] = 1;
- $oldlevel = $level;
- $level -= 1;
- continue;
- } else {
- if (strpos($v,"li>")===false) {
- if ($level > 1 && $oldlevel != $level) {
- $parent = $id;
- } else {
- $parent = 0;
- }
- $id++;
- $parent_id[1] = 0;
- // uncomment next line for debugging
- //print "\n ".$parent_id[$level]." : $id : $level: ".$v;
- if ($parent_id[$level]==$id-1 || $parent_id[$level]==0) {
- $pages .= makeParentAFolder($parent_id[$level]);
- }
- $pages .= insertPage($id,$v,$parent_id[$level],'',$c[$level]);
- $c[$level]++;
- }
- }
- }
- if ($install) mysql_close($conn);
- return $pages;
- }
- $ul = isset($_POST['ul']) ? $_POST['ul'] : '';
- ?>
- <form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
- <textarea cols='100%' rows='18' name='ul'><?php echo $ul?></textarea><br>
- <input type="submit" />
- </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement