All pastes #583656 Raw Edit

Untitled

public php v1 · immutable
·published 2007-06-22 13:48 UTC
rendered paste body
class Template_Factory {  public static $template_possible_id_tms = array('id_tm','id');  public static $template_fields = array(    'id_tm', 'id_author', 'id_type', 'id_owner', 'state', 'price', 'exc_price',    'inserted-date',    // Other tables    'author', 'type',  );  public static $template_details_fields = array(    'is_hosting', 'is_flash', 'is_unique', 'is_non_unique',    'is_unique_corporate', 'is_full_site', 'is_real_size', 'downloads',    'sources', 'software_required',  );    /**   * Template ( CuteDBQuery [ , array db_args | int id_tm  ] )   * Template ( int id_tm  [ , const how_much_data = BASIC ] )   * Template ( array data )   */  public static function Template() {    /* Prepare variables */    $iargs      = func_num_args();    $templates  = array();    $arg0       = func_get_arg(0);    $arg1       = ($iargs>1) ? func_get_arg(1) : false;    /* Dispatch to the correct factory and add to template list */    if ( ($arg0 instanceof CuteDB_Query)          && ( (is_int($arg1) && $args = array('id' => $arg1))                || (is_array($arg1)) ) ) {      while ($a_db = $arg0->fetch_assoc($arg1)) {        $o_tm         = Template_Factory::TemplateFromArray($a_db);        $templates[]  = $o_tm;        Template_List::add($o_tm, $o_query);      }    } elseif ( is_int($arg0) && ( is_string($arg1) || $arg1 = BASIC )              && list ( $o_tm, $o_query )                  = Template_Factory::TemplateFromScratch($arg0, $arg1) ) {      $templates[] = $o_tm;      Template_List::add($o_tm, $o_query);    } elseif ( !$arg1 && is_array($arg0)                && $o_tm = Template_Factory::TemplateFromArray($arg0)) {      $templates[]  = $o_tm;      Template_List::add($o_tm);    } else {      return false;    }       /* return added templates */    return $templates;  }    public static function TemplateQuerySelect() {    $o_queries  = Shop::getUnique('Template_QueryList');    if ($how_much_data == BASIC) {      /**       * args : ( $id_tm , BASIC )       * Query should select data from template index table       */      $o_query = $o_queries->getQ('template_basic');    } elseif (strin($how_much_data, BIGGEST_SCREENSHOT)              && strin($how_much_data, DETAILS)) {      /**       * args : ( $id_tm , BIGGEST_SCREENSHOT . DETAILS )       * Query should select data from template index & details tables       * and it's largest screenshoot       */      $o_query = $o_queries->getQ('template_with_largest_screenshot');    } elseif (strin($how_much_data, BIGGEST_SCREENSHOT)) {      /**       * args : ( $id_tm , BIGGEST_SCREENSHOT )       * Query should select data from template index table and       * it's largest screenshoot       */      $o_query = $o_queries->getQ('template_detailled_with_largest_screenshot');    } elseif ( (strin($how_much_data, ALL_SCREENSHOTS)                && strin($how_much_data, DETAILS))              || strin($how_much_data, ALL_DATA) ) {      /**       * args : ( $id_tm , ALL_SCREENSHOTS . DETAILS )       * Query should select data from template index, details, pages and       * screenshots tables       */      $o_query = $o_queries->getQ('template_detailled_with_screenshots');    } elseif (strin($how_much_data, ALL_SCREENSHOTS)) {      /**       * args : ( $id_tm , ALL_SCREENSHOTS )       * Query should select data from template index pages and       * screenshots tables       */      $o_query = $o_queries->getQ('template_with_screenshots');    }    return $o_query;  }    public static function TemplateFromScratch($id_tm, $how_much = BASIC) {    if ($o_query = self::TemplateQuerySelect($how_much)        && ($o_tm = self::Template($o_query, $id_tm)) ) {      if ($how_much == ALL_DATA) {        $o_tm->_getTemplateFields('id_style', 'style', $aStyles);        $o_tm->_getTemplateFields('id_category', 'categories', $aCategories);        $o_tm->_getKeywords();      }      return $o_tm;    } else {      Logger::Log(DEBUG, array('tmsg'=>'Could not make Template or get Query',                                'args'=>func_get_args()));        return false;    }  }    public static function TemplateFromArray($array) {    $id_tm = false;    foreach (self::$template_possible_id_tms as $key) {      if (isset($array[$key]) && $array[$key]) {        $id_tm = $array[$key];        break;      }    }    if (!$id_tm) {      Logger::Log(DEBUG, array('tmsg'=>'Could not get id_tm in $array',                                'args'=>$array));      return false;    }    $good_options = $_return = array();    $good_options['id_tm'] = $id_tm;    $template_fields = array_merge(self::$template_fields,                                   self::$template_details_fields);    foreach ($template_fields as $key) {      if (isset($good_options[$key])) {        $a_template[$key] = $good_options[$key];      } elseif (isset($array[$key])) {        $a_template[$key] = $array[$key];      }    }    if ($_return) {      return $_return;    }    if ($o_tm = Template_List::get($id_tm)) {      /* Template exists */    } else {      $o_tm = new Template ($a_template);    }    self::installPages($o_tm, $array);    return $o_tm;  }    public function installPages($o_tm, $array) {    if (isset($array['id_page']) && isset($array['page_description'])) {      $id_page  = $array['id_page'];      $s_page   = $array['page_description'];      if ( ($o_page = $o_tm->getPage($id_page))          || ($o_page = Template_Factory::Page($array)) ) {        $o_tm->addPage($o_page);      } else {          Logger::Log(DEBUG, array('tmsg'=>'Page creation|fetch failure',                                    'args'=>$array));          return false;      }            if ($o_screenshot = Template_Factory::Screenshot($array)) {        $o_page->addScreenshot($o_screenshot);      } else {        Logger::Log(DEBUG, array('tmsg'=>'Could not create screenshot',                                 'args'=>$array));        return false;      }    }  }  /* Page object generator */  public static function Page($options) {    $_return = $good_options = array();    if (defined('DATA_VERIFICATION')) {      if (is_array($options)) {        // test page id        if ( isset($options['id_page'])            && self::validId($options['id_page'])) {          $good_options['id'] = $options['id_page'];        } elseif ( isset($options['id'])                  && self::validId($options['id'])) {          $good_options['id'] = $options['id'];        } else {          $_return[] = 'Invalid id|id_page';        }        // test id_tm        if ( isset($options['id_tm']) && self::validId($options['id_tm']) ) {          $good_options['id_tm'] = $options['id_tm'];        } else {          $_return[] = 'Invalid id_tm';        }        // test label        if ( isset($options['label']) && self::validLabel($options['label']) ) {          $good_options['label'] = $options['label'];        } elseif ( isset($options['page_description'])                  && self::validLabel($options['page_description']) ) {          $good_options['label'] = $options['page_description'];        } elseif ( isset($options['label_en'])                  && self::validLabel($options['label_en']) ) {          $good_options['label'] = $options['label_en'];        } else {          $_return[] = 'Invalid label|page_description|label_en';        }      } else {        $_return[] = 'Expects an array';      }    }    if ($_return) {      return $_return;    }    $o_page = new Template_Page($good_options);    return $o_page;  }    /* Screenshot object generator */  public static function Screenshot($options)  {    $good_options = array();    if (isset($options['id_screenshot'])) {      $good_options['id'] = $options['id_screenshot'];    } elseif (isset($options['id'])) {      $good_options['id'] = $options['id'];    } else {      // screenshots must have id      return false;    }    if (isset($options['id_tm'])) {      $good_options['id_tm'] = $options['id_tm'];    } else {      // screenshots must have an id_tm      return false;    }    if (isset($options['id_page'])) {      $good_options['id_page'] = $options['id_page'];    } else {      // screenshots must have an id_page      return false;    }       $fields = Template_Screenshot::getFields();    foreach ($options as $col => $val) {      if (in_array($col, $fields)) {        $good_options[$col] = $val;      } elseif (strin($col,'label') || strin($col,'screenshot_description')) {        $good_options['label'] = $val;      }    }    if (!isset($good_options['label'])) {      return false;    }    return new Template_Screenshot($good_options);  }      public static function validId($i = null) {    if (is_numeric($i)) {      return true;    } else {      return false;    }  }    public static function validLabel($str = null) {    if (is_string($str) && strlen($str) > 3) {      return true;    } else {      return false;    }  }    public static function beginOptionsFiltering($options, $sid,                                               $must_id_tm = true) {    $good_options = array();    if (isset($options[$sid])) {      $good_options['id'] = $options[$sid];    } elseif (isset($options['id'])) {      $good_options['id'] = $options['id'];    } else {      // screenshots must have id      return false;    }    if (isset($options['id_tm'])) {      $good_options['id_tm'] = $options['id_tm'];    } elseif ($must_id_tm) {      // screenshots must have an id_tm      return false;    }    return $good_options;  }}