File:  [Local Repository] / db / prgsrc / drupal / modules / chgk_db / classes / DbPackage.class.php
Revision 1.6: download - view: text, annotated - select for diffs - revision graph
Sun May 23 09:31:39 2010 UTC (14 years, 1 month ago) by roma7
Branches: MAIN
CVS tags: HEAD
Editors changes

    1: <?php
    2: 
    3: require_once(dirname(__FILE__)."/DbDatabase.class.php");
    4: require_once(dirname(__FILE__)."/DbPackage/DbPackageGroup.class.php");
    5: require_once(dirname(__FILE__)."/DbPackage/DbPackageTour.class.php");
    6: require_once(dirname(__FILE__)."/DbPackage/DbPackageChamp.class.php");
    7: require_once(dirname(__FILE__)."/DbPackage/DbPackageRoot.class.php");
    8: 
    9: require_once(dirname(__FILE__)."/DbPackage/DbPackageError.class.php");
   10: 
   11: class DbPackage {
   12: 
   13:   protected $tour;
   14:   protected $db;
   15:   protected $id;
   16:   protected $children = FALSE;
   17:   protected $parent = FALSE;
   18:   const NOSPACES = TRUE;
   19:   private static $tourCache = array();
   20: 
   21:   public function __construct($row, $parent = FALSE) {
   22:     $this->setParent($parent);
   23:     $this->db = new DbDatabase();
   24:     if (is_object($row)) {
   25:       $this->tour = $row;
   26:       $this->setId();
   27:     } else {
   28:       $this->id = $row;
   29:       $this->loadFromDatabase();
   30:     }
   31:   }    
   32: 
   33:   protected function setId() {
   34:     $this->id = $this->tour->FileName;
   35:   }
   36: 
   37:   public static function newRoot() {
   38:       return new DbPackageRoot($row);
   39:   }
   40: 
   41:   public static function newFromRow($row) {
   42:     if (!$row) {
   43:       return new DbPackageError($id);
   44:     } elseif ($row->Type == 'Г' ) {
   45:       return new DbPackageGroup($row);
   46:     } elseif ($row->Type == 'Ч' ) {
   47:       return new DbPackageChamp($row);
   48:     } elseif ($row->Type == 'Т' ) {
   49:       return new DbPackageTour($row);
   50:     }
   51:   }
   52: 
   53:   public static function newFromQuestionRow($row, $prefix) {
   54:         $tour = new stdClass();
   55:         $tour->Id = $row->{"{$prefix}Id"};
   56:         $tour->Title = $row->{"{$prefix}Title"};
   57:         $tour->FileName = $row->{"{$prefix}FileName"};
   58:         $tour->Type = $row->{"{$prefix}Type"};
   59:         return self::newFromRow($tour);
   60:   }
   61:   
   62:   public static function newFromDb($id) { 
   63:     if (self::$tourCache[$id]) return self::$tourCache[$id];    
   64:     $db = new DbDatabase;
   65:     $row = $db->getTournament($id);
   66:     $result = self::newFromRow($row);
   67:      self::$tourCache[$id] = $result;
   68:     return $result;
   69:   }
   70: 
   71:   public function loadFromDatabase() {
   72:     $this->tour = $this->db->getTournament($this->id);
   73:   }
   74:   public function getAll() {
   75:     return false;
   76:   }
   77:   
   78:   protected function getDbId() {
   79:     return $this->tour->Id;
   80:   }
   81:   
   82:   public function getTitle() {
   83:      return $this->tour->Title;
   84:   }
   85: 
   86:   public function getFullTitle() {
   87:      return $this->getTitle();
   88:   }
   89: 
   90:   
   91:   public function getLongTitle() {
   92:     return $this->getTitle();
   93:   }
   94:   public function getInfo() {
   95:     $info = $this->tour->Info;
   96:     $info = preg_replace('/(\s+)-+(\s+)/','\1&mdash;$2', $info);
   97: 
   98:     return $info;
   99:   }
  100:   public function hasEditor() {
  101:     return $this->tour->Editors?TRUE:FALSE;
  102:   }
  103:   public function hasInfo() {
  104:     return $this->tour->Info?TRUE:FALSE;
  105:   }
  106: 
  107:   public function getEditor() {
  108:     return $this->tour->Editors;
  109:   }
  110: 
  111:   public function getEditorHtml() {
  112:     $ed = $this->tour->Editors;
  113:     $ed = preg_replace('/(\s+)-+(\s+)/','\1&mdash;$2', $ed);
  114:     
  115:     if (preg_match('/\,/', $ed))  {
  116:       $ob = 'Редакторы: ';
  117:     } else {
  118:       $ob = 'Редактор: ';
  119:     }
  120:     return $ob.$ed;
  121:   }
  122:   public function getFb2() {
  123:     $this->getAll();
  124:     return theme('chgk_db_fb2', $this);
  125:   }
  126: 
  127:   public function loadTree() {      
  128:       foreach ($this->getChildren() as $child) {
  129:           $child->loadTree();
  130:       }
  131:   }
  132: 
  133:   public function getChildren() {
  134:        if ($this->children === FALSE ) {
  135:            $this->loadChildren();
  136:        }
  137:        return $this->children;
  138:    }
  139: 
  140:   public function loadChildren() {
  141:       $this->children = array();
  142:       $res = $this->db->getChildrenRes($this->getDbId());
  143:       while ($row = $this->db->fetch_row($res)) {
  144:         $this->children[] = DbPackage::newFromRow($row, $this);
  145:       }
  146:     }
  147: 
  148: 
  149:   public function getImagesBinaries() {
  150:       $images=$this->getImages();
  151:       $result = '';
  152:       foreach ($images as $i) {
  153:           $name = "/home/znatoki/chgk-db/public_html/images/db/$i";
  154: 	................
  155:           $result.="<binary content-type='image/jpeg' id='$i'>";			        
  156:           $file = fopen($name,'rb');
  157:           $str_file=fread($file,filesize($name));
  158:           $result.=base64_encode($str_file);
  159:           $result.="</binary>";
  160:           fclose($file);
  161:       }
  162:       return $result;
  163:   }
  164: 
  165:   protected function getEditorsForList() {
  166:     $ed = $this->db->getEditors($this->tour->Id);
  167:     if ($ed) {
  168: 	$result = array();    
  169: 	foreach ($ed as $editor) {
  170: 	    $result[] = $editor->Name." ".$editor->Surname;
  171: 	}
  172: 	return implode(', ',$result);
  173:     } else {
  174:         return '';
  175:     }
  176:   }
  177: 
  178:   public function getHtmlLinkForList() {
  179:       $result = l($this->getTitle(), $this->getLink());    
  180:       return $result;
  181:   }
  182: 
  183:   public function getHtmlLinkForBreadrumb() {
  184:       return l($this->getTitle(), $this->getLink());
  185:   }
  186:  
  187:   public function getLink() {
  188:       return "tour/".$this->id;
  189:   }
  190: 
  191:   public function htmlTree($level = 0) {
  192:       $result = $this->getHtmlLinkForList();
  193:       $children_html = '';
  194:       foreach ($this->getChildren() as $child)  {
  195:           if (!self::NOSPACES) {
  196:               $children_html.=str_repeat(' ',$level*4+4);
  197:           }
  198:           $children_html.= "<li>".$child->htmlTree($level+1)."</li>";
  199:           if (!self::NOSPACES) {
  200:             $children_html .= "\n";
  201:           }
  202:       }
  203:       if ($children_html) {
  204:           if (!self::NOSPACES) {
  205:               $result.="\n".str_repeat(' ',$level*4+2);
  206:           }
  207:           $result.="<ul>";
  208:           if (!self::NOSPACES) {
  209:               $result.="\n";
  210:           }
  211:           $result.=$children_html;
  212:           if (!self::NOSPACES) {
  213:             $result .= str_repeat(' ',$level*4+2);
  214:           }
  215:           $result.="</ul>";
  216:           if (!self::NOSPACES) {        
  217:             $result.="\n".str_repeat(' ',$level*4);
  218:           }
  219:       }
  220:       return $result;
  221:   }
  222: 
  223:   public function setParent($parent = FALSE) {
  224:     if ($parent) {
  225:         $this->parent = $parent;
  226:     } elseif($this->tour->ParentId) {
  227:         $this->parent = DbPackage::newFromDb($this->tour->ParentId);
  228:     } elseif ($this->tour->ParentId === '0') {
  229:         $this->parent = DbPackage::newRoot();
  230:     }
  231:   }
  232:   public function getParent() {
  233:     if ($this->parent === FALSE) {
  234:       $this->setParent();
  235:     }
  236:     return $this->parent;
  237:   }
  238: 
  239:   public function getPrintVersion() {
  240:     $content = $this->getHtmlContent();
  241:     return theme('chgk_db_print', $this->getLongTitle(),
  242:             $content,
  243:             url($this->getLink(), array('absolute'=>TRUE)));
  244:   }
  245:  
  246:   public function getHtmlContent() {
  247:     return '';
  248:   }
  249:   
  250:   public function getBreadcrumb() {
  251:     $this->loadBranch();
  252:     $result = array();
  253:     for ($current=$this->getParent(); $current; $current=$current->getParent()) {
  254:       array_unshift($result,$current->getHtmlLinkForBreadrumb()); 
  255:     }
  256:     return $result;
  257:   }
  258:   
  259:   public function hasPrintVersion() {
  260:     return FALSE;
  261:   }
  262: 
  263:   public function hasFb2() {
  264:     return FALSE;
  265:   }
  266: 
  267:   private function getPlayedAt() {
  268:     return $this->tour->PlayedAt;
  269:   }
  270: 
  271:   public function getYear() {
  272:     $date = $this->getPlayedAt();
  273:     $ar = explode('-', $date);
  274:     return $ar[0];
  275:   }
  276: 
  277:   
  278:   private function loadBranch() {
  279:     $parent = $this->getParent();
  280:     if ($parent) $parent->loadBranch();    
  281:   }
  282: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>