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—$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—$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 = file_directory_path()."/$i";
154: $result.="<binary content-type='image/jpeg' id='$i'>";
155: $file = fopen($name,'rb');
156: $str_file=fread($file,filesize($name));
157: $result.=base64_encode($str_file);
158: $result.="</binary>";
159: fclose($file);
160: }
161: return $result;
162: }
163:
164: protected function getEditorsForList() {
165: $ed = $this->db->getEditors($this->tour->Id);
166: if ($ed) {
167: $result = array();
168: foreach ($ed as $editor) {
169: $result[] = $editor->Name." ".$editor->Surname;
170: }
171: return implode(', ',$result);
172: } else {
173: return '';
174: }
175: }
176:
177: public function getHtmlLinkForList() {
178: $result = l($this->getTitle(), $this->getLink());
179: return $result;
180: }
181:
182: public function getHtmlLinkForBreadrumb() {
183: return l($this->getTitle(), $this->getLink());
184: }
185:
186: public function getLink() {
187: return "tour/".$this->id;
188: }
189:
190: public function htmlTree($level = 0) {
191: $result = $this->getHtmlLinkForList();
192: $children_html = '';
193: foreach ($this->getChildren() as $child) {
194: if (!self::NOSPACES) {
195: $children_html.=str_repeat(' ',$level*4+4);
196: }
197: $children_html.= "<li>".$child->htmlTree($level+1)."</li>";
198: if (!self::NOSPACES) {
199: $children_html .= "\n";
200: }
201: }
202: if ($children_html) {
203: if (!self::NOSPACES) {
204: $result.="\n".str_repeat(' ',$level*4+2);
205: }
206: $result.="<ul>";
207: if (!self::NOSPACES) {
208: $result.="\n";
209: }
210: $result.=$children_html;
211: if (!self::NOSPACES) {
212: $result .= str_repeat(' ',$level*4+2);
213: }
214: $result.="</ul>";
215: if (!self::NOSPACES) {
216: $result.="\n".str_repeat(' ',$level*4);
217: }
218: }
219: return $result;
220: }
221:
222: public function setParent($parent = FALSE) {
223: if ($parent) {
224: $this->parent = $parent;
225: } elseif($this->tour->ParentId) {
226: $this->parent = DbPackage::newFromDb($this->tour->ParentId);
227: } elseif ($this->tour->ParentId === '0') {
228: $this->parent = DbPackage::newRoot();
229: }
230: }
231: public function getParent() {
232: if ($this->parent === FALSE) {
233: $this->setParent();
234: }
235: return $this->parent;
236: }
237:
238: public function getPrintVersion() {
239: $content = $this->getHtmlContent();
240: return theme('chgk_db_print', $this->getLongTitle(),
241: $content,
242: url($this->getLink(), array('absolute'=>TRUE)));
243: }
244:
245: public function getHtmlContent() {
246: return '';
247: }
248:
249: public function getBreadcrumb() {
250: $this->loadBranch();
251: $result = array();
252: for ($current=$this->getParent(); $current; $current=$current->getParent()) {
253: array_unshift($result,$current->getHtmlLinkForBreadrumb());
254: }
255: return $result;
256: }
257:
258: public function hasPrintVersion() {
259: return FALSE;
260: }
261:
262: public function hasFb2() {
263: return FALSE;
264: }
265:
266:
267: private function loadBranch() {
268: $parent = $this->getParent();
269: if ($parent) $parent->loadBranch();
270: }
271: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>