Location: PHPKode > scripts > phpwsListings > phpwslistings/class/Listing.php
<?php
/**
* SEILER_Listing for phpwsListings
*
* SEILER_Listing represents a listing item
*
* @author Don Seiler <hide@address.com>
* @version $Id: Listing.php,v 1.40 2003/04/23 04:58:19 dtseiler Exp $
* @package phpwsListings
*/

class SEILER_Listing {

    var $_id = NULL;
    var $_class_id = NULL;
    var $_agent_id = NULL;
    var $_title = NULL;
    var $_notes = NULL;
    var $_creationdate = 0;
    var $_lastmodified = 0;
    var $_active = 0;
    var $_hits = 0;
    var $_sold = 0;
    var $_solddate = 0;
    var $_price = 0;
    var $_feature = 1;
    var $_listelements = array();
    var $_formelements;
    var $_images = array();

    var $_new = FALSE;
    var $_class = NULL;

    function SEILER_Listing($LISTING_id = NULL) {
        $this->_formelements =& $_SESSION["SES_LISTING_MANAGER"]->getFormElements();

        if($LISTING_id === NULL) {
            $this->_new = TRUE;

            $this->_solddate = date("Y-m-d");
            $this->_agent_id = $_SESSION["OBJ_user"]->user_id;
        } else {
            $result = $GLOBALS["core"]->sqlSelect("mod_listings", "id", $LISTING_id);

            $this->_new = FALSE;
            $this->_id = $result[0]["id"];
            $this->_class_id = $result[0]["class_id"];
            $this->_agent_id = $result[0]["agent_id"];
            $this->_title = $result[0]["title"];
            $this->_notes = $result[0]["notes"];
            $this->_creationdate = $result[0]["creationdate"];
            $this->_lastmodified = $result[0]["lastmodified"];
            $this->_active = $result[0]["active"];
            $this->_hits = $result[0]["hits"];
            $this->_price = $result[0]["price"];
            $this->_feature = $result[0]["feature"];
            $this->_sold = $result[0]["sold"];
            $this->_solddate = $result[0]["solddate"];

            $this->_class = new SEILER_ListingClass($this->_class_id);

            $lmtsmonth  = substr($this->_lastmodified,4,2);
            $lmtsday    = substr($this->_lastmodified,6,2);
            $lmtsyear   = substr($this->_lastmodified,0,4);
            $lmtshour   = substr($this->_lastmodified,8,2);
            $lmtsmin    = substr($this->_lastmodified,10,2);
            $lmtssec    = substr($this->_lastmodified,12,2);
            $lmtsepoch  = mktime($lmtshour,$lmtsmin,$lmtssec,$lmtsmonth,$lmtsday,$lmtsyear);
            $this->_lastmodified   = date ('Y-m-d H:m:s T', $lmtsepoch);

            if(!$this->loadListElements()) {
                $this->_error("load_list_elements");
                return;
            }

            if(!$this->loadImages()) {
                $this->_error("load_images");
                return;
            }
        }
    }


    function setClassId($CLASS_id = NULL) {
        if(!empty($CLASS_id)) {
            $this->_class_id = $CLASS_id;
            $this->_class = new SEILER_ListingClass($this->_class_id);
        } else {
            $this->_error("no_class");
            return;
        }
    }


    function view($type) {
        if(!$this->_active && !$_SESSION["OBJ_user"]->allow_access("phpwslistings") && ($this->_agent_id != $_SESSION["OBJ_user"]->user_id))
            return;

        if($_SESSION["OBJ_user"]->allow_access("phpwslistings") || ($this->_agent_id == $_SESSION["OBJ_user"]->user_id))
            $tags["EDITLINK"] = "<a href=\"index.php?module=phpwslistings&amp;LISTINGS_op=" . $_SESSION["translate"]->it("Edit") . "&amp;LISTING_id=" . $this->_id . "\">" . $_SESSION["translate"]->it("Edit Listing") . "</a>";

        $tags["AGENT_ID"] = $this->_agent_id;
        $agentUser = new PHPWS_User($this->_agent_id);
        $tags["AGENT_EMAIL"] = $agentUser->email;
        $tags["AGENT_LABEL"] = $_SESSION["translate"]->it("Agent");
        $tags["LISTEDON_LABEL"] = $_SESSION["translate"]->it("Listed on");
        $tags["UPDATEDON_LABEL"] = $_SESSION["translate"]->it("Updated on");
        $agentFName = $agentUser->getUserVar("first_name",$agentUser->user_id,"phpwslistings");
        $agentLName = $agentUser->getUserVar("last_name",$agentUser->user_id,"phpwslistings");
        if(empty($agentFName) || empty($agentLName))
            $agentName = $agentUser->username;
        else
            $agentName = $agentFName . " " . $agentLName;

        $tags["AGENTLINK"] = "<a href=\"index.php?module=phpwslistings&amp;AGENT_id=" . $agentUser->user_id . "&amp;LISTINGS_user_op=viewagent\">" . $agentName . "</a>";  
        $tags["TITLE"] = $this->_title;
        if(!empty($this->_notes)) {
            $tags["NOTES_LABEL"] = $_SESSION["translate"]->it("Notes");
            $tags["NOTES"] = $this->_notes;
        }
        $tags["CREATIONDATE"] = $this->_creationdate;
        $tags["LASTMODIFIED"] = $this->_lastmodified;
        $tags["HITS"] = $this->_hits;

        if($this->_class->_usePrice) {
            $tags["PRICE_LABEL"] = $_SESSION["translate"]->it("Price");
            $tags["PRICE_SIGN"] = $_SESSION["translate"]->it("$");
            $tags["PRICE"] = number_format($this->_price);
        }

        if($this->_feature)
            $tags["FEATURE"] = $_SESSION["translate"]->it("Featured");

        if($this->_sold) {
            $tags["TITLE"] = $this->_title . " - " . $_SESSION["translate"]->it("SOLD!");
            $tags["SOLD"] = $_SESSION["translate"]->it("Sold on ") . $this->_solddate;
        }


        $print_icon = "<a href=\"index.php?module=phpwslistings&amp;LISTINGS_user_op=view_printable&amp;LISTING_id=" . $this->_id . "&amp;lay_quiet=1\" target=\"_blank\"><img src=\"mod/phpwslistings/img/icon_print.png\" border=\"0\" alt=\"" . $_SESSION["translate"]->it("Printable Version") . "\"/>" . $_SESSION["translate"]->it("Printable Version") . "</a>";
        $tags["PRINTABLE"] = $print_icon;
        $email_icon = "<a href=\"index.php?module=phpwslistings&amp;LISTINGS_user_op=email&amp;EMAIL_type=friend&amp;LISTING_id=" . $this->_id . "\"><img src=\"mod/phpwslistings/img/icon_email.png\" border=\"0\" alt=\"" . $_SESSION["translate"]->it("Send to a Friend") . "\"/>" . $_SESSION["translate"]->it("Send to a Friend") . "</a>";
        $tags["EMAIL_FRIEND"] = $email_icon;
        $email_icon = "<a href=\"index.php?module=phpwslistings&amp;LISTINGS_user_op=email&amp;EMAIL_type=agent&amp;LISTING_id=" . $this->_id . "\"><img src=\"mod/phpwslistings/img/icon_email.png\" border=\"0\" alt=\"" . $_SESSION["translate"]->it("Contact Agent") . "\"/>" . $_SESSION["translate"]->it("Contact Agent") . "</a>";
        $tags["EMAIL_AGENT"] = $email_icon;

        if($type == "printable") {
            $formelements = "";
            for($i=0; $i<count($this->_formelements); $i++) {
                if(isset($this->_listelements[$this->_formelements[$i]->_field_name])) {
                    $field_value = $this->_listelements[$this->_formelements[$i]->_field_name];
                    $formelements .= $this->_formelements[$i]->displayFormElement($field_value);
                }
            }
            $tags["FORMELEMENTS"] = $formelements;
   
            $main_image = "";
            $thumb_images = "";
            for($i=0; $i<count($this->_images); $i++) {
                if($this->_images[$i]->_rank == 1)
                    $main_image = $this->_images[$i]->renderFull();
                else
                    $thumb_images .= $this->_images[$i]->renderThumb();
            }
            $tags["MAINIMAGE"] = $main_image;
            $tags["THUMBIMAGES"] = $thumb_images;
            echo $GLOBALS["core"]->processTemplate($tags, "phpwslistings", "view_full.tpl");
        } else {
            /* Full view of the listing */
            if($type == "full") {
                if($this->_class->_useMortCalc) {
                    $mortcalcurl = "mod/phpwslistings/mortgage_calculator.php?listing_price=" . $this->_price;
                    $tags["MORTCALC"] = "<font size=\"-2\"><a href=\"$mortcalcurl\" target=\"_BLANK\" onClick=\"window.open('$mortcalcurl','_BLANK','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=550,height=600');return false;\">" . $_SESSION["translate"]->it("Mortgage Calculator") . "</a><br />(" . $_SESSION["translate"]->it("Opens New Window") . ")</font>";
                }

                $formelements = "";
                for($i=0; $i<count($this->_formelements); $i++) {
                    if(isset($this->_listelements[$this->_formelements[$i]->_field_name])) {
                        $field_value = $this->_listelements[$this->_formelements[$i]->_field_name];
                        $formelements .= $this->_formelements[$i]->displayFormElement($field_value);
                    }
                }
                $tags["FORMELEMENTS"] = $formelements;
    
                $main_image = "";
                $thumb_images = "";
                for($i=0; $i<count($this->_images); $i++) {
                    if($this->_images[$i]->_rank == 1)
                        $main_image = $this->_images[$i]->renderFull();
                    else
                        $thumb_images .= $this->_images[$i]->renderThumb();
                }
                $tags["MAINIMAGE"] = $main_image;
                $tags["THUMBIMAGES"] = $thumb_images;
        
                $content = $GLOBALS["core"]->processTemplate($tags, "phpwslistings", "view_full.tpl");
    
    
            } elseif ($type = "small") {
                if(!$this->_active)
	                return;
    
                /* Summarized view of listings, user on home page */
                $main_image = "";
                for($i=0; $i<count($this->_images); $i++) {
                    if($this->_images[$i]->_rank == 1)
                        $main_image = $this->_images[$i]->renderThumb();
                }
                $tags["MAINIMAGE"] = $main_image;
    
                $formelements = "";
                for($i=0; $i<count($this->_formelements); $i++) {
                    if(isset($this->_listelements[$this->_formelements[$i]->_field_name])) {
                        $field_value = $this->_listelements[$this->_formelements[$i]->_field_name];
                        $formelements .= $this->_formelements[$i]->displayBrowseElement($field_value);
                    }
                }
                $tags["FORMELEMENTS"] = $formelements;
                $tags["READ_MORE"] = "<a href=\"index.php?module=phpwslistings&amp;LISTINGS_user_op=view&amp;LISTING_id=" . $this->_id .  "\">See details</a>";
                $content = $GLOBALS["core"]->processTemplate($tags, "phpwslistings", "view_small.tpl");
            }
            $title = NULL;
            $_SESSION["OBJ_layout"]->popbox($title, $content, NULL, "CNT_phpwslistings");
        }
    }


    function renderBlock() {
        $content = "";
        if((count($this->_images) > 0) || (!$_SESSION["SES_LISTING_MANAGER"]->_blockImagesOnly)) {
            $content .= $this->getMainThumb();
            $content .= "<b>" . $this->_title . "</b><br />";
            if($this->_class->_usePrice)
                $content .= $_SESSION["translate"]->it("Price") . ": " . $_SESSION["translate"]->it("$") . " " . number_format($this->_price,2) . "<br />";
            $content .= "<a href=\"index.php?module=phpwslistings&amp;LISTINGS_user_op=view&amp;LISTING_id=" . $this->_id .  "\">" . $_SESSION["translate"]->it("See details!") . "</a>";
        }
        return $content;
    }
    

    function formatDate($in_date) {
        return substr(preg_replace("/[^0-9]/", "", $in_date), 0, 8);
    }

    function edit() {
        if(is_null($this->_class))
            return;

        $tags["CLASS_LABEL"] = $_SESSION["translate"]->it("Class");
        $tags["CLASS"] = PHPWS_Core::formHidden("LISTING_class_id", $this->_class_id) . "<b>" . $this->_class->_name . "</b>";
        $tags["AGENTID_LABEL"] = $_SESSION["translate"]->it("Agent");
        $agents = $this->_getUsers();
        if($_SESSION["OBJ_user"]->allow_access("phpwslistings", "edit_listing") && $_SESSION["OBJ_user"]->allow_access("phpwslistings", "create_listing"))
            $tags["AGENTID"] = PHPWS_Core::formSelect("LISTING_agent_id", $agents, $this->_agent_id, FALSE, TRUE);
        else
            $tags["AGENTID"] = PHPWS_Core::formHidden("LISTING_agent_id", $this->_agent_id) . "<b>" . $agents[$this->_agent_id] . "</b>";
        $tags["TITLE_LABEL"] = $_SESSION["translate"]->it("Title");
        $tags["TITLE"] = PHPWS_Core::formTextField("LISTING_title", $this->_title, 33, 80);
        $tags["NOTES_LABEL"] = $_SESSION["translate"]->it("Notes");
        $tags["NOTES"] = $GLOBALS["core"]->js_insert("wysiwyg", "phpwslistings_edit", "LISTING_notes") . PHPWS_Core::formTextArea("LISTING_notes", $this->_notes, 5, 40);

        if($this->_class->_usePrice) {
            $tags["PRICE_LABEL"] = $_SESSION["translate"]->it("Price");
            $tags["PRICE_SIGN"] = $_SESSION["translate"]->it("$");
            $tags["PRICE"] = PHPWS_Core::formTextField("LISTING_price", $this->_price, 10, 20);
        }

        $soldtrlabel = $_SESSION["translate"]->it("Sold");
        $tags["SOLD"] = PHPWS_Core::formCheckbox("LISTING_sold", 1, $this->_sold, NULL, $soldtrlabel);
        if($_SESSION["OBJ_user"]->allow_access("phpwslistings","feature_listing")){
            $featuretrlabel = $_SESSION["translate"]->it("Feature");
            $tags["FEATURE"] = PHPWS_Core::formCheckbox("LISTING_feature", 1, $this->_feature, NULL, $featuretrlabel);
        }

        if(!$this->_new) {
            if(count($this->_images) < $_SESSION["SES_LISTING_MANAGER"]->_listingImageLimit)
                $tags["ADDIMAGELINK"] = "<a href=\"./index.php?module=phpwslistings&LISTINGS_op=addimage&LISTING_id=" . $this->_id . "&LISTING_agent_id=" . $this->_agent_id . "\">" . $_SESSION["translate"]->it("Add Image") . "</a>";

            if(count($this->_images))
                $tags["EDITIMAGELABEL"] = $_SESSION["translate"]->it("Click image to edit");
            $thumb_images = "";
            for($i=0; $i<count($this->_images); $i++) {
                $thumb_images .= $this->_images[$i]->renderThumb(TRUE);
            }
            $tags["EDITIMAGES"] = $thumb_images;
        }

        $tags["SOLDDATE_LABEL"] = $_SESSION["translate"]->it("Date Sold");
        $tags["SOLDDATE"] = $GLOBALS["core"]->formDate("LISTING_solddate", $this->formatDate($this->_solddate));


        // Magic Time
        $formelements = "";
        for($i=0; $i<count($this->_formelements); $i++) {
            if($this->_formelements[$i]->_class_id == $this->_class_id) {
                $value = "";
                if(!$this->_new)
                    if(isset($this->_listelements[$this->_formelements[$i]->_field_name]))
                        $value = $this->_listelements[$this->_formelements[$i]->_field_name];
    
                $formelements .= $this->_formelements[$i]->renderFormElement($value);
            }
        }

        $tags["FORMELEMENTS"] = $formelements;

        $tags["SUBMIT_BUTTON"] = PHPWS_Core::formSubmit($_SESSION["translate"]->it("Save"), "LISTINGS_op");

        $elements[0] = PHPWS_Core::formHidden("module", "phpwslistings");
        $elements[0] .= $GLOBALS["core"]->processTemplate($tags, "phpwslistings", "edit.tpl");

        if($this->_new) $title = $_SESSION["translate"]->it("Create Listing");
        else $title = $_SESSION["translate"]->it("Edit Listing");

        $content = PHPWS_Core::makeForm("phpwslistings_edit", "index.php", $elements, "post", FALSE, TRUE);
        $_SESSION["OBJ_layout"]->popbox($title, $content, NULL, "CNT_phpwslistings");
    }

    function save() {
        if($_REQUEST["LISTINGS_op"] == $_SESSION["translate"]->it("Save") && !$_SESSION["OBJ_user"]->allow_access("phpwslistings", "edit_listing") && !$_SESSION["OBJ_user"]->allow_access("phpwslistings", "create_listing") && ($this->_agent_id != $_SESSION["OBJ_user"]->user_id)) {
            $this->_error("access_denied");
            return;
        }

        $this->_class_id = PHPWS_Core::parseInput($_POST["LISTING_class_id"]);
        $data["class_id"] = $this->_class_id;
        $this->_agent_id = PHPWS_Core::parseInput($_POST["LISTING_agent_id"]);
        $data["agent_id"] = $this->_agent_id;
        $this->_notes = PHPWS_Core::parseInput($_POST["LISTING_notes"]);
        $data["notes"] = $this->_notes;
        $this->_lastmodified = date('Y-m-d H:m:s');
        $data["lastmodified"] = $this->_lastmodified;
        $this->_price = PHPWS_Core::parseInput($_POST["LISTING_price"]);

        // Need to handle number formatting
        $lastperiod = strpos($this->_price, ".");
        $lastcomma = strpos($this->_price, ",");
        if($lastperiod && $lastcomma) {
            if($lastperiod > $lastcomma) {
                // period is decimal point
                // strip out commas
                $this->_price = preg_replace("/,/", "", $this->_price);
            } else {
                // comma is decimal point
                // strip out periods, then replace comma with period
                $this->_price = preg_replace("/\./", "", $this->_price);
                $this->_price = preg_replace("/,/", ".", $this->_price);
            }
        } elseif($lastperiod) {
            // period is decimal point or there is no decimal point
            // don't need to do anything
        } elseif($lastcomma) {
            // comma is decimal point or there is no decimal point
            // replace comma with period
            $this->_price = preg_replace("/,/", ".", $this->_price);
        }

        $data["price"] = $this->_price;

        if(isset($_POST["LISTING_sold"])) {
            $this->_sold = 1;
            $this->_solddate = $_POST["LISTING_solddate_year"] . "-" . $_POST["LISTING_solddate_month"] . "-" . $_POST["LISTING_solddate_day"] . " 00:00:00";
        } else {
            $this->_sold = 0;
            $this->_solddate = NULL;
        }

        $data["sold"] = $this->_sold;
        $data["solddate"] = $this->_solddate;

        if($_SESSION["OBJ_user"]->allow_access("phpwslistings","feature_listing")){
            if(isset($_POST["LISTING_feature"]))
                $this->_feature = 1;
            else 
                $this->_feature = 0;
            $data["feature"] = $this->_feature;
        }

        if($_POST["LISTING_title"]) {
            $this->_title = PHPWS_Core::parseInput($_POST["LISTING_title"]);
            $data["title"] = $this->_title;
        } else {
            $this->_error("no_title");
            $this->edit();
            return;
        }

        if($this->_new) {
            if($this->add($data)) {
	            $title = $_SESSION["translate"]->it("Listing Saved");
	            $content = $_SESSION["translate"]->it("Your listing was successfully saved.");
                $content .= "  <a href=\"./index.php?module=phpwslistings&LISTINGS_op=" . $_SESSION["translate"]->it("Edit") . "&LISTING_id=" . $this->_id . "\">" . $_SESSION["translate"]->it("Go back to your listing.") . "</a>";

            } else {
	            $this->_error("save_failed");
	            $this->edit();
	            return;
            }
        } elseif ($this->_id) {
            if($this->update($data)) {
	            $title = $_SESSION["translate"]->it("Listing Updated");
	            $content = $_SESSION["translate"]->it("Your listing was successfully updated.");
                $content .= "  <a href=\"./index.php?module=phpwslistings&LISTINGS_op=" . $_SESSION["translate"]->it("Edit") . "&LISTING_id=" . $this->_id . "\">" . $_SESSION["translate"]->it("Go back to your listing.") . "</a>";
            } else {
	            $this->_error("update_failed");
	            $this->edit();
	            return;
            }
        }

        if(!$this->saveListElements()) {
            $this->_new = FALSE;
            $this->edit();
	        return;
        }

        $_SESSION["OBJ_layout"]->popbox($title, $content, NULL, "CNT_phpwslistings");
    }

    function delete() {
        if(!$_SESSION["OBJ_user"]->allow_access("phpwslistings", "delete_listing") && ($this->_agent_id != $_SESSION["OBJ_user"]->user_id)) {
            $this->_error("access_denied");
            return;
        }

        if(isset($_POST["yes"])){
            for($i=0; $i<count($this->_images); $i++) {
                $this->_images[$i]->delete(TRUE);
            }
            $this->dropListElements();
            $GLOBALS["core"]->sqlDelete("mod_listings", "id", $this->_id);
            $title = $_SESSION["translate"]->it("Listing Deleted!");
            $content = $_SESSION["translate"]->it("The listing was successfully <b>deleted</b>.");
        } elseif (isset($_POST["no"])) {
            $title = $_SESSION["translate"]->it("Listing Kept!");
            $content = $_SESSION["translate"]->it("You have chosen <b>not</b> to delete the listing.");
        } else {
            $elements[0] = PHPWS_Core::formHidden("module", "phpwslistings");
            $elements[0] .= PHPWS_Core::formHidden("LISTINGS_op", "Delete");
            $elements[0] .= PHPWS_Core::formHidden("LISTING_id", $this->_id);
            $elements[0] .= PHPWS_Core::formSubmit($_SESSION["translate"]->it("Yes"), "yes");
            $elements[0] .= PHPWS_Core::formSubmit($_SESSION["translate"]->it("No"), "no");

            $title = $_SESSION["translate"]->it("Delete Listing?");
            $content = $_SESSION["translate"]->it("Are you sure you wish to delete this listing?");
            $content .= PHPWS_Core::makeForm("phpwslistings_delete", "index.php", $elements);
        }
        $_SESSION["OBJ_layout"]->popbox($title, $content, NULL, "CNT_phpwslistings");
    }

  function add($data) {
    if($_REQUEST["LISTINGS_op"] == $_SESSION["translate"]->it("Save") && !$_SESSION["OBJ_user"]->allow_access("phpwslistings", "create_listing") && ($this->_agent_id != $_SESSION["OBJ_user"]->user_id)) {
      $this->_error("access_denied");
      return;
    }

    $this->_creationdate = date('Y-m-d H:m:s');
    $data["creationdate"] = $this->_creationdate;

    $this->_id = $GLOBALS["core"]->sqlInsert($data, "mod_listings", FALSE, TRUE);


    if($this->_id)
      return TRUE;
    else
      return FALSE;
  }

  function update($data) {
    if(!$_SESSION["OBJ_user"]->allow_access("phpwslistings", "edit_listing") && !isset($data["hits"]) && ($this->_agent_id != $_SESSION["OBJ_user"]->user_id)) {
      $this->_error("access_denied");
      return;
    }

    if($GLOBALS["core"]->sqlUpdate($data, "mod_listings", "id", $this->_id))
      return TRUE;
    else
      return FALSE;
  }

  function hit() {
    $this->_hits++;
    $data["hits"] = $this->_hits;
    $this->update($data);
  }

  function showHide() {
    if(!$_SESSION["OBJ_user"]->allow_access("phpwslistings", "activate_listing") && ($_SESSION["OBJ_user"]->user_id != $this->_agent_id)) {
      $this->_error("access_denied");
      return;
    }

    PHPWS_Core::toggle($this->_active);
    $data["active"] = $this->_active;
    $this->update($data);
  }

  /**
   * Returns an indexed array of all the current groups in the database
   *
   * @return array $users An array of all groups
   * @access private
   * @see    edit()
   */
  function _getUsers() {
    /* Grab all users from database */
    $result = $GLOBALS["core"]->sqlSelect("mod_users", NULL, NULL, "username");

    /* Add blank user */
    $users[] = " ";

    /* Create user array */
    if($result)
        foreach($result as $resultRow) {
            $agentUser = new PHPWS_User($resultRow["user_id"]);
            $agentFName = $agentUser->getUserVar("first_name",$agentUser->user_id,"phpwslistings");
            $agentLName = $agentUser->getUserVar("last_name",$agentUser->user_id,"phpwslistings");
            if(empty($agentFName) || empty($agentLName))
                $users[$resultRow["user_id"]] = $resultRow["username"];
            else
                $users[$resultRow["user_id"]] = $agentFName . " " . $agentLName;
        }

    return $users;
  }// END FUNC _getUsers()



    function _error($type) {
        $title = "<font class=\"errortext\">" . $_SESSION["translate"]->it("ERROR!") . "</font>";

        switch($type) {
            case "no_class":
                $content = $_SESSION["translate"]->it("You did not select a class for your listing.");
                break;

            case "no_title":
                $content = $_SESSION["translate"]->it("You did not provide a title for your listing.");
                break;

            case "no_required_formelement":
                $content = $_SESSION["translate"]->it("You are missing a required field.  Please make sure all required fields (marked with a \"*\") are completed.");
                break;
    
            case "save_failed":
                $content = $_SESSION["translate"]->it("There was a problem saving your listing.") . " " . $_SESSION["translate"]->it("If the problem persists contact your system administrator.");
                break;

            case "update_failed":
                $content = $_SESSION["translate"]->it("There was a problem updating your listing.") . " " . $_SESSION["translate"]->it("If the problem persists contact your system administrator.");
                break;

            case "access_denied":
                $content = "<b>" . $_SESSION["translate"]->it("ACCESS DENIED!") . "</b>";
                break;
            case "load_form_elements":
                $content = "<b>" . $_SESSION["translate"]->it("Unable to load form elements!") . "</b>";
                break;
            case "load_list_elements":
                $content = "<b>" . $_SESSION["translate"]->it("Unable to load list item elements!") . "</b>";
                break;
            case "load_images":
                $content = "<b>" . $_SESSION["translate"]->it("Unable to load listing images!") . "</b>";
                break;
        }

        $_SESSION["OBJ_layout"]->popbox($title, $content, NULL, "CNT_phpwslistings");
    }// END FUNC _error()


    function loadListElements() {
        $this->_listelements = array();
        if(($tmp_elements = $GLOBALS["core"]->sqlSelect("mod_listings_listelements","listing_id",$this->_id)) == NULL) {
            return TRUE;
        } elseif(($tmp_elements = $GLOBALS["core"]->sqlSelect("mod_listings_listelements","listing_id",$this->_id)) == FALSE) {
            return FALSE;
        }
    
        foreach ($tmp_elements as $info) {
            extract($info);

            // Why was I doing this?
            /*
            $testArray = unserialize($field_value);
            if(is_array($testArray))
                $field_value = $testArray;
            */
    
            $this->_listelements[$field_name] = $field_value;
        }
        return TRUE;
    }


    function loadImages() {
        $this->_images = array();
        if(($tmp_images = $GLOBALS["core"]->sqlSelect("mod_listings_images","listing_id",$this->_id,"rank",NULL,NULL,NULL,NULL,FALSE)) == NULL) {
            return TRUE;
        } elseif(($tmp_images = $GLOBALS["core"]->sqlSelect("mod_listings_images","listing_id",$this->_id,"rank",NULL,NULL,NULL,NULL,FALSE)) == FALSE) {
            return FALSE;
        }


        for($i=0; $i<count($tmp_images); $i++)
            array_push($this->_images, new SEILER_ListingImage($tmp_images[$i]["id"]));

        return TRUE;
    }


    function saveListElements() {
        $this->dropListElements();
        for($i=0; $i<count($this->_formelements); $i++) {
            if($this->_formelements[$i]->_class_id == $this->_class_id) {
                $field_name = $this->_formelements[$i]->_field_name;
                $form_field_name = "LISTING_" . $field_name;
                $insert["field_name"] = $field_name;
                $field_value = $_POST[$form_field_name];  
                if($this->_formelements[$i]->_required) {
                    if(!isset($field_value) || empty($field_value)) {
                        $this->_error("no_required_formelement");
                        return FALSE;
                    }
                }
                if(is_array($field_value)) {
                    $value = "";
                    while(list($foo,$bar) = each($field_value)) {
                        $value = "$value||$bar";
                    }
                    $value = substr($value,2);
                    $insert["field_value"] = PHPWS_Core::parseInput($value);
                } else {
                    $field_value = PHPWS_Core::parseInput($field_value);  
                    $insert["field_value"] = $field_value;
                }
                $insert["listing_id"] = $this->_id;

                $GLOBALS["core"]->sqlInsert($insert, "mod_listings_listelements",TRUE,TRUE);
            }
        }

        return TRUE;
    }


    function dropListElements() {
        $where["listing_id"] = $this->_id;
        $GLOBALS["core"]->sqlDelete("mod_listings_listelements", $where);
    }


    function getRandomThumb() {
        $this->loadImages();
        if(count($this->_images)) {
            $i = rand(0,count($this->_images) - 1);
            return $this->_images[$i]->renderThumb();
        }
    }

    function getMainThumb() {
        $this->loadImages();
        if(count($this->_images)) {
            return $this->_images[0]->renderThumb();
        }
    }


    function match($criteria) {
        // Compare elements in $criteria against elements of this listing.
        // Return true if match, else false

        if(array_key_exists("agent_id", $criteria)) {

            if($criteria["agent_id"] != $this->_agent_id)
                return FALSE;
        }
        if(array_key_exists("agency_id", $criteria)) {
            $agentUser = new PHPWS_User($this->_agent_id);
            $agency_id = $agentUser->getUserVar("agency_id",$agentUser->user_id,"phpwslistings");
            if($criteria["agency_id"] != $agency_id)
                return FALSE;
        }
        if(array_key_exists("title", $criteria)) {

            if(!substr_count(strtolower($this->_title), strtolower($criteria["title"])))
                return FALSE;
        }
        if(array_key_exists("minprice", $criteria)) {
            if($criteria["minprice"] > $this->_price)
                return FALSE;
        }
        if(array_key_exists("maxprice", $criteria)) {
            if($criteria["maxprice"] < $this->_price)
                return FALSE;
        }

        for($i=0; $i<count($this->_formelements); $i++) {
            $name = $this->_formelements[$i]->_field_name;
            if(array_key_exists($name, $criteria)) {
                $value = $this->_listelements[$this->_formelements[$i]->_field_name];
                if($this->_formelements[$i]->_field_type == "text") {
                    // If text field, need to determine if value is supposed to be a number
                    if(is_numeric($criteria[$name])) {
                        if(is_numeric($value)) {
                            if($criteria[$name] < $value)
                                return FALSE;
                        } else {
                            // if one is numeric but other is not
                            return FALSE;
                        }
                    } else {
                        if(!substr_count(strtolower($value), strtolower($criteria[$name])))
                            return FALSE;
                    }
                } elseif(($this->_formelements[$i]->_field_type == "multiple") ||
                        ($this->_formelements[$i]->_field_type == "checkbox")) {
                    $hasMatch = FALSE;
                    foreach ($criteria[$name] as $v) {
                        $values = explode("||", $value);
                        foreach ($values as $val) {
                            // echo "<h3>criteria is $v, value is $val</h3>";
                            if($v == $val) {
                                $hasMatch = TRUE;
                                break;
                            }
                        }
                        if($hasMatch)
                            break;
                    }
                    if(!$hasMatch)
                        return FALSE;
                } else {
                    // Need exact match
                    if($criteria[$name] != $value)
                        return FALSE;
                }
            }

        }

        return TRUE;
    }


    function searchForm() {

        $tags["AGENTID_LABEL"] = $_SESSION["translate"]->it("Agent");
        $tags["AGENTID"] = PHPWS_Core::formSelect("LISTING_agent_id", $this->_getUsers(), NULL, FALSE, TRUE);

        $tags["AGENCY_LABEL"] = $_SESSION["translate"]->it("Agency");
        // Eventually have an ListingAgency::showSelect() function
        $tags["AGENCY"] = PHPWS_Core::formSelect("LISTING_agency_id", $this->_getAgencies(), NULL, FALSE, TRUE);

        $tags["TITLE_LABEL"] = $_SESSION["translate"]->it("Title");
        $tags["TITLE"] = PHPWS_Core::formTextField("LISTING_title", NULL, 33, 255);
        /*
        $tags["NOTES_LABEL"] = $_SESSION["translate"]->it("Notes");
        $tags["NOTES"] = $GLOBALS["core"]->js_insert("wysiwyg", "phpwslistings_edit", "LISTING_notes") . PHPWS_Core::formTextArea("LISTING_notes", $this->_notes, 5, 40);
        */

        if($this->_class->_usePrice) {
            $tags["MAX_PRICE_LABEL"] = $_SESSION["translate"]->it("Maximum Price");
            $tags["MIN_PRICE_LABEL"] = $_SESSION["translate"]->it("Minimum Price");
            $tags["PRICE_SIGN"] = $_SESSION["translate"]->it("$");
            $tags["MAX_PRICE"] = PHPWS_Core::formTextField("LISTING_maxprice", NULL, 10, 255);
            $tags["MIN_PRICE"] = PHPWS_Core::formTextField("LISTING_minprice", NULL, 10, 255);
        }

        /*
        $soldtrlabel = $_SESSION["translate"]->it("Sold");
        $tags["SOLD"] = PHPWS_Core::formCheckbox("LISTING_sold", 1, NULL, NULL, $soldtrlabel);

        $tags["SOLDDATE_LABEL"] = $_SESSION["translate"]->it("Date Sold");
        $tags["SOLDDATE"] = $GLOBALS["core"]->formDate("LISTING_solddate", $this->formatDate($this->_solddate));
        */


        // Magic Time
        $formelements = "";
        for($i=0; $i<count($this->_formelements); $i++) {
            if($this->_formelements[$i]->_class_id == $this->_class_id)
                $formelements .= $this->_formelements[$i]->renderFormElement(NULL);
        }

        $tags["FORMELEMENTS"] = $formelements;

        $tags["SORT_LABEL"] = $_SESSION["translate"]->it("Sort By");
        $sortelements = array(
            "" => "",
            "title" => $_SESSION["translate"]->it("Title"),
            "creationdate" => $_SESSION["translate"]->it("Listing Date"),
            "price" => $_SESSION["translate"]->it("Price")
        );
        for($i=0; $i<count($this->_formelements); $i++) {
            // Don't show agent only fields unless user is an agent
            if($this->_formelements[$i]->_field_type != "blank")
                if((!$this->_formelements[$i]->_agentonly))
                    $sortelements[$this->_formelements[$i]->_field_name] = $this->_formelements[$i]->_field_caption;
        }
        $tags["SORT_BY"] = PHPWS_Core::formSelect("LISTING_sortby", $sortelements);
        $sortorders = array(
            "ASC" => "Ascending",
            "DESC" => "Descending"
        );
        $tags["SORT_ORDER"] = PHPWS_Core::formSelect("LISTING_sortorder", $sortorders,"ASC");


        $tags["SUBMIT_BUTTON"] = PHPWS_Core::formSubmit($_SESSION["translate"]->it("Search Listings"), "LISTINGS_user_op");

        $elements[0] = PHPWS_Core::formHidden("module", "phpwslistings");
        $elements[0] .= $GLOBALS["core"]->processTemplate($tags, "phpwslistings", "searchform.tpl");

        $title = $_SESSION["translate"]->it("Search Listings");

        $content = PHPWS_Core::makeForm("phpwslistings_search", "index.php", $elements, "post", FALSE, TRUE);
        $_SESSION["OBJ_layout"]->popbox($title, $content, NULL, "CNT_phpwslistings");
    }


  /**
   * Returns an indexed array of all the current agencies in the database
   *
   * @return array $agencies An array of all agencies
   * @access private
   * @see    edit()
   */
    function _getAgencies() {
        /* Grab all users from database */
        $result = $GLOBALS["core"]->sqlSelect("mod_listings_agencies", NULL, NULL, "name");

        /* Add blank group */
        $agencies[] = " ";

        /* Create groups array */
        if($result)
            foreach($result as $resultRow)
                $agencies[$resultRow["id"]] = $resultRow["name"];

        return $agencies;
    }// END FUNC _getAgencies()


}// END CLASS PHPWS_Listing

?>
Return current item: phpwsListings