Advertisement
Guest User

Untitled

a guest
May 26th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.36 KB | None | 0 0
  1. <?php
  2.  
  3. $template_id = 4;       //the id of the template you want to assign to your pages
  4. $prefix      = 'modx';  // your table prefix
  5. $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
  6.  
  7. if (isset($_POST['ul'])) {
  8.   echo "<textarea cols='100%' rows='18'>" .
  9.        parseList($_POST['ul']) .
  10.        "</textarea>";
  11. }
  12.  
  13. function insertPage($id,$title,$parent_id,$content = '',$menuindex)
  14. {
  15.   global $template_id,$prefix,$install;
  16.   $alias = makeAliasFromTitle($title);
  17.   $query = "REPLACE INTO `{$prefix}_site_content`
  18.           (`id`, `type`, `contentType`, `pagetitle`, `longtitle`, `description`,
  19.           `alias`, `link_attributes`, `published`, `pub_date`, `unpub_date`, `parent`,
  20.           `isfolder`, `introtext`, `content`, `richtext`, `template`, `menuindex`, `searchable`,
  21.           `cacheable`, `createdby`, `createdon`, `editedby`, `editedon`, `deleted`, `deletedon`,
  22.           `deletedby`, `publishedon`, `publishedby`, `menutitle`, `donthit`, `haskeywords`,
  23.           `hasmetatags`, `privateweb`, `privatemgr`, `content_dispo`, `hidemenu`) VALUES
  24.          
  25.           ($id, 'document', 'text/html', '$title', '', '',
  26.           '$alias', '', 1, 0, 0, $parent_id,
  27.           0, '', '$content', 1, $template_id, $menuindex, 1,
  28.           1, 1, ".time().", 1, ".time().", 0, 0,
  29.           0, ".time().", 1, '', 0, 0,
  30.           0, 0, 0, 0, 0);";
  31.   if ($install) {
  32.     $result = mysql_query($query);
  33.     return(mysql_affected_rows());
  34.   } else {
  35.     return $query."\n";
  36.   }
  37. }
  38.  
  39. function makeAliasFromTitle($title)
  40. {
  41.   $alias = strtolower($title);
  42.   $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'));
  43.   $alias = strip_tags($alias);  
  44.   $alias = preg_replace('/&.+?;/', '', $alias); // kill entities
  45.   $alias = preg_replace('/[^\.%A-Za-z0-9 _-]/', '', $alias);
  46.   $alias = preg_replace('/\s+/', '-', $alias);
  47.   $alias = str_replace('_', '-', $alias);
  48.   $alias = preg_replace('|-+|', '-', $alias);
  49.   $alias = trim($alias, '-');
  50.   return $alias;
  51. }
  52.  
  53. function makeParentAFolder($id) {
  54.   global $prefix;
  55.   $query = "UPDATE `{$prefix}_site_content` SET isfolder=1 WHERE id='$id';";
  56.   if ($install) {
  57.   $result = mysql_query($query);
  58.   return(mysql_affected_rows());
  59.   } else {
  60.     return $query."\n";
  61.   }
  62. }
  63.  
  64. function parseList($ul) {
  65.   global $install;
  66.   if ($install) {
  67.     // make a connection to the database
  68.     $host = 'localhost';
  69.     $user = 'root';
  70.     $pass = 'root';
  71.     $data = 'modx';
  72.     $conn = mysql_connect($host,$user,$pass);
  73.     mysql_select_db($data,$conn);
  74.   }
  75.   //first lets make sure all uls and /uls are on their own line
  76.   $ul = preg_replace("#(</?ul>)#","\n$1\n",$ul);
  77.   // same for lis
  78.   $ul = preg_replace("#(</?li>)#","\n$1\n",$ul);
  79.   // now strip empty lines
  80.   $array = explode("\n",$ul);
  81.   $level = 0;
  82.   $id = 100;
  83.   $parent_id[1] = 0;
  84.   $pages = '';
  85.   $c = array();
  86.   foreach($array as $k=>$v) {
  87.     $v = trim($v);
  88.     if (empty($v)) continue;
  89.     // if we come across a ul then increment the level by 1
  90.     if (strstr("<ul>",$v)) {
  91.       $oldlevel = $level;
  92.       $level += 1;
  93.       $parent_id[$level]=$id;
  94.       $c[$level] = 1;
  95.       continue;
  96.     // if we come across a /ul then decrement the level by 1
  97.     } elseif (strstr("</ul>",$v)) {
  98.       $c[$level] = 1;
  99.       $oldlevel = $level;
  100.       $level -= 1;
  101.       continue;
  102.     } else {
  103.       if (strpos($v,"li>")===false) {
  104.         if ($level > 1 && $oldlevel != $level) {
  105.           $parent = $id;
  106.         } else {
  107.           $parent = 0;
  108.         }
  109.         $id++;
  110.         $parent_id[1] = 0;
  111.         // uncomment next line for debugging
  112.         //print "\n ".$parent_id[$level]." : $id : $level: ".$v;
  113.         if ($parent_id[$level]==$id-1 || $parent_id[$level]==0) {
  114.           $pages .= makeParentAFolder($parent_id[$level]);
  115.         }
  116.         $pages .= insertPage($id,$v,$parent_id[$level],'',$c[$level]);
  117.         $c[$level]++;
  118.       }
  119.     }
  120.   }
  121.   if ($install) mysql_close($conn);
  122.   return $pages;
  123. }
  124.  
  125. $ul = isset($_POST['ul']) ? $_POST['ul'] : '';
  126.  
  127. ?>
  128.  
  129. <form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
  130. <textarea cols='100%' rows='18' name='ul'><?php echo $ul?></textarea><br>
  131. <input type="submit" />
  132. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement