#!/usr/local/bin/php -q
<?
    /*
     * File: create_site.php
     *
     * Description:
     *
     * Creator: Chris Ryan <chris@greatbridge.com>
     * Created: 11/01/2000
     *
     * Change Log:
     *     Chris Ryan <chris@greatbridge.com> 11/01/2000
     *         - Intial file creation
     *     Chris Ryan <chris@greatbridge.com> 11/16/2000
     *         - Changed some of the text messages
     *     Chris Ryan <chris@greatbridge.com> 01/04/2001
     *         - Corrected a Typo found by/submitted by Matt V.
     *     Chris Ryan <chris@greatbridge.com> 01/15/2001
     *         - added some code to make sure the pages go into the
     *           dabase with the correct group_id value on page_info.
     */

    # include the file that has alot of our functions for creating
    # records and uh stuff...
    require_once("./defaults.php");
    require_once("./creation_functions.php");
    require_once("./page_data.php");

    # first we must make sure there is an all encompassin group...
    # this would be a site_group record with an id of 0
    checkDefaultSiteGroup();

    # now that we have verified that have a default site_group record or
    # have created one we are ready to select an existing site_group or
    # create a new one for the site we are currently creating

    # lets see if we have any existing site_group records (besdies the default
    # one we just created. Sites can't be assigned to the default group
    $query = "  select group_id, description ".
             "    from site_group ".
             "   where group_id > 0 ".
             "order by group_id ";
    $res   = pg_exec($conn,$query);

    if(pg_numrows($res) > 0) {
        echo "There are already some site_group records on the database\n";
        echo "that can be used for this new site or you can create a new\n";
        echo "site group for this site.\n\n";

        $r = 0;
        while($row = @pg_fetch_array($res,$r++)) {
            $site_groups[$row["group_id"]] = $row["description"];
        }

        # this section of code while loop will loop until the user
        # selects a _valid_ option
        $valid_option = false;
        while($valid_option != true) {
            reset($site_groups);

            # output a line for each option in our list
            while(list($id,$name) = @each($site_groups)) {
                printf("    %3d) %s\n",$id,$name);
            }
            reset($site_groups);

            # output an option to create a new site_group
            echo "      N) Create a new site group record\n";

            $line = readline("Choice: ");

            # convert the string to lower case so we don't have
            # to be concerned if the user entered N or n
            $line = strtolower($line);

            if($line == "n" || !empty($site_groups[$line])) {
                $valid_option = true;
            }
        }
    } else {
        $line = "n";
    }

    echo "\n";

    # if line == "n" then we need to create a new site_group record
    # other wise line is the id of the site_group we want to use.
    if($line == "n") {
        $valid_option = false;
        while($valid_option != true) {
            $line = readline("New site group name: "); 
            $site_group_id = createSiteGroup($line);
            if($site_group_id < 1) {
                echo "There was an error creating the new group.\n";
                echo "Please try again.\n";
            } else {
                $valid_option = true;
            }
        }
        echo "\n";
    } else {
       $site_group_id = $line;

       echo "Using site_group $site_group_id: ". $site_groups[$site_group_id] . "\n\n";
    }

    # now that we have all the site_group stuff worked out we need to create
    # a site record.
    echo "Now we will setup the site info record. A few questions need\n";
    echo "to be answered. Please enter a value for all of the options\n";
    echo "as they are required but all of them EXCEPT the SiteID can\n";
    echo "be Changed at a later time through the site admin tools.\n";
    echo "\n";

    echo "You need specify the site id for this new site.\n";
    echo "This should be the domain of your site.\n";
    echo "example: greatbridge.org\n";
    echo "example: www.greatbridge.org\n";
    $site_id = readline("SiteID: ");

    echo "\n";

    echo "Please Enter the official name of the site.\n";
    echo "This name is used in some of the navigation and templates\n";
    echo "as the name of the site.\n";
    $official_name = readline("Official Name: ");

    echo "\n";

    echo "Please specify the location of the template directory on your\n";
    echo "system. If you installed the files into the default location\n";
    echo "this would be /usr/local/gbsite/templates/gborg\n";
    echo "NOTE: this must be an absolute path\n";
    $template_path = readline("Template Path: ");

    echo "\n";

    echo "Setting name_prefix  = gborg\n";
    echo "Setting image_path   = /images/gborg\n";
    echo "Setting default_site = false\n";
    echo "\n";
    echo "These values can be changed later through the site admin tools\n";
    echo "If you change the name_prefix or image_path you will have to move\n";
    echo "and rename files to get the site to work properly.\n";
    echo "\n\n";

    $site_id = sqlText($site_id);
    $official_name = sqlText($official_name);
    $template_path = sqlText($template_path);

    $query = "insert into site_info ".
             "       (http_host,name_prefix,official_name,template_path, ".
             "        image_path,group_id,default_page) ".
             "values ($site_id,'gborg',$official_name,$template_path, ".
             "        '/images/gborg',$site_group_id,false) ";
    $res   = pg_exec($conn,$query);
    pg_freeresult($res);

    echo "Creating Default fonts and colors: These can be changed later\n";
    echo "in the site admin tools.\n";

    while(list(,$color) = @each($DEFAULT_COLORS)) {
        $query = "insert into color ".
                 "       (http_host,number,value) ".
                 "values ($site_id,${color["number"]},'${color["value"]}') ";
        $res   = pg_exec($conn,$query);
        pg_freeresult($res);
    }

    while(list(,$font) = @each($DEFAULT_FONTS)) {
        $query = "insert into font ".
                 "       (http_host,number,value) ".
                 "values ($site_id,${font["number"]},'${font["value"]}') ";
        $res   = pg_exec($conn,$query);
        pg_freeresult($res);
    }

    echo "\n";

    # Ok Here we are at the pages for the site.
    # loop through the variable that has the pertinent page info
    # and then we are _almost_ done.
    echo "Adding the pages to the database. The pages can be tailored\n";
    echo "afterwards through the site admin tools.\n";
    while(list(,$page) = @each($PAGE_INFO_DATA)) {
        $web_page = sqlText($page[0]);
        $group_id = sqlText($page[1]);
        $page_url = sqlText($page[2]);
        $link_name = sqlText($page[3]);
        $order_num = $page[4];
        $active = sqlText($page[5]);
        $page_template = "'gborg_navpage.php'";
        $page_title = $official_name;

        # hmmm should I really have done this. I guess if the data
        # were as good as it should I shouldn't, however, there is
        # one record that I know of that breaks the rule so i
        # put this here to make sure that nothing breaks if there is more
        if($link_name == "null") $link_name = "''";

        $query = "insert into page_info ".
                 "       (http_host,web_page,page_title,page_template, ".
                 "        group_id,page_url,link_name,order_number,active) ".
                 "values ($site_id,$web_page,$page_title,$page_template, ".
                 "        $group_id,$page_url,$link_name,$order_num, ".
                 "        $active)";
        $res   = pg_exec($conn,$query);
        pg_freeresult($res);
    }

    echo "\n";

    # one last thing to do!!! build the group
    echo "Adding the groups for the pages\n";
    while(list(,$group) = @each($GROUP_INFO_DATA)) {
        $group_id = sqlText($group[0]);
        $web_page = sqlText($group[1]);
        $name     = sqlText($group[2]);
        $desc     = sqlText($group[3]);
        $is_content = sqlText($group[4]);
        $order_num  = $group[5];

        $query = "insert into group_info ".
                 "       (http_host,group_id,web_page,name,description, ".
                 "        is_content,order_number) ".
                 "values ($site_id,$group_id,$web_page,$name,$desc, ".
                 "        $is_content,$order_num) ";
        $res   = pg_exec($conn,$query);
        pg_freeresult($res);
        $query = "update page_info set group_id = '' where http_host = $site_id and web_page = $web_page";
        $res   = pg_exec($conn,$query);
        pg_freeresult($res);
    }

    echo "\n";
    echo "The process is complete.\n\n";
 ?>
