1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
15:
16: 17: 18: 19: 20: 21:
22: class cApiArticleLanguageVersionCollection extends cApiArticleLanguageCollection {
23:
24: 25: 26: 27: 28: 29: 30:
31: public function __construct($select = false) {
32:
33: $sTable = cRegistry::getDbTableName('art_lang_version');
34: $sPrimaryKey = 'idartlangversion';
35: ItemCollection::__construct($sTable, $sPrimaryKey);
36:
37: $this->_setItemClass('cApiArticleLanguageVersion');
38:
39:
40: $this->_setJoinPartner('cApiArticleCollection');
41: $this->_setJoinPartner('cApiLanguageCollection');
42: $this->_setJoinPartner('cApiTemplateConfigurationCollection');
43:
44: if ($select !== false) {
45: $this->select($select);
46: }
47: }
48:
49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90:
91: public function create(array $parameters) {
92: global $auth;
93:
94: if (empty($parameters['author'])) {
95: $parameters['author'] = $auth->auth['uname'];
96: }
97: if (empty($parameters['created'])) {
98: $parameters['created'] = date('Y-m-d H:i:s');
99: }
100: if (empty($parameters['lastmodified'])) {
101: $parameters['lastmodified'] = date('Y-m-d H:i:s');
102: }
103:
104: $parameters['urlname'] = (trim($parameters['urlname']) == '') ? trim($parameters['title']) : trim($parameters['urlname']);
105:
106:
107: $parameters['version'] = 1;
108: $sql = 'SELECT MAX(version) AS maxversion FROM ' . cRegistry::getDbTableName('art_lang_version') . ' WHERE idartlang = %d;';
109: $sql = $this->db->prepare($sql, $parameters['idartlang']);
110: $this->db->query($sql);
111: if ($this->db->nextRecord()) {
112: $parameters['version'] = $this->db->f('maxversion');
113: ++$parameters['version'];
114: }
115:
116: $item = $this->createNewItem();
117:
118:
119: foreach (array_keys($parameters) as $key) {
120:
121: if ($key == 'iscurrentversion') {
122: continue;
123: }
124: $item->set($key, $parameters[$key]);
125: }
126: $item->markAsCurrentVersion($parameters['iscurrentversion']);
127: $item->store();
128:
129: return $item;
130: }
131:
132: 133: 134: 135: 136: 137: 138: 139:
140: public function getIdByArticleIdAndLanguageId($idArtLang, $version) {
141:
142: $id = NULL;
143:
144: $where = 'idartlang = ' . $idArtLang . ' AND version = ' . $version;
145:
146: $artLangVersionColl = new cApiArticleLanguageVersionCollection();
147: $artLangVersionColl->select($where);
148:
149: while($item = $artLangVersionColl->next()){
150: $id = $item->get('idartlangversion');
151: }
152:
153: return isset($id) ? $id : 0;
154:
155: }
156: }
157:
158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245:
246: class cApiArticleLanguageVersion extends cApiArticleLanguage {
247:
248: 249: 250: 251: 252:
253: public $tab;
254:
255: 256: 257: 258: 259:
260: public $content = NULL;
261:
262: 263: 264: 265: 266: 267: 268: 269:
270: public function __construct($id = false, $fetchContent = false) {
271:
272: $sTable = cRegistry::getDbTableName('art_lang_version');
273: $sPrimaryKey = 'idartlangversion';
274: Item::__construct($sTable, $sPrimaryKey);
275:
276: $this->setFilters(array(), array());
277: if ($id !== false) {
278: $this->loadByPrimaryKey($id);
279: if (true === $fetchContent) {
280: $this->_getArticleVersionContent();
281: }
282: }
283: }
284:
285: 286: 287: 288: 289: 290:
291: public function markAsCurrentVersion($isCurrentVersion){
292: $attributes = array(
293: 'idartlang' => $this->get('idartlang'),
294: 'iscurrentversion' => $isCurrentVersion
295: );
296: if ($isCurrentVersion == 1) {
297: $artLangVersion = new cApiArticleLanguageVersion();
298: if ($artLangVersion->loadByMany($attributes)) {
299: $artLangVersion->set('iscurrentversion', 0);
300: $artLangVersion->store();
301: }
302: $this->set('iscurrentversion', 1);
303: } else {
304: $this->set('iscurrentversion', 0);
305: }
306: $this->store();
307:
308: }
309:
310: 311: 312: 313: 314: 315: 316: 317: 318: 319:
320: public function markAsCurrent($type = ''){
321:
322: if ($type == 'complete') {
323:
324: $parameters = $this->toArray();
325: $artLang = new cApiArticleLanguage($parameters['idartlang']);
326: unset($parameters['idartlang']);
327: unset($parameters['idartlangversion']);
328: unset($parameters['iscurrentversion']);
329: unset($parameters['version']);
330: foreach ($parameters as $key => $value) {
331: $artLang->set($key, $value);
332: }
333: $artLang->store();
334: }
335:
336: if ($type == 'content' || $type == 'complete') {
337:
338: $where = 'idartlang = ' . $this->get('idartlang');
339: $contentVersionColl = new cApiContentVersionCollection();
340:
341:
342: $contents = $contentVersionColl->getIdsByWhereClause($where);
343: if (isset($contents)) {
344: $sql = 'SELECT a.idcontent
345: FROM `%s` AS a
346: WHERE a.idartlang = %d AND a.idcontent NOT IN
347: (SELECT DISTINCT b.idcontent
348: FROM `%s` AS b
349: WHERE (b.deleted < 1 OR b.deleted IS NULL)
350: AND (b.idtype, b.typeid, b.version) IN
351: (SELECT idtype, typeid, max(version)
352: FROM `%s`
353: WHERE idartlang = %d AND version <= %d
354: GROUP BY idtype, typeid))';
355: $this->db->query(
356: $sql,
357: cRegistry::getDbTableName('content'),
358: $this->get('idartlang'),
359: cRegistry::getDbTableName('content_version'),
360: cRegistry::getDbTableName('content_version'),
361: $this->get('idartlang'), $this->get('version')
362: );
363: $contentColl = new cApiContentCollection();
364: while ($this->db->nextRecord()) {
365: $contentColl->delete($this->db->f('idcontent'));
366: }
367: $contentVersion = new cApiContentVersion();
368: $ctype = new cApiType();
369: $this->_getArticleVersionContent();
370: foreach ($this->content AS $typeName => $typeids) {
371: foreach ($typeids AS $typeid => $value) {
372: $ctype->loadByType($typeName);
373: $contentParameters = array(
374: 'idartlang' => $this->get('idartlang'),
375: 'idtype' => $ctype->get('idtype'),
376: 'typeid' => $typeid,
377: 'version' => $this->get('version')
378: );
379: $contentVersion->loadByArticleLanguageIdTypeTypeIdAndVersion($contentParameters);
380: $contentVersion->markAsCurrent();
381: }
382: }
383: }
384: }
385:
386: if ($type == 'meta' || $type == 'complete') {
387:
388:
389: $metaTagVersion = new cApiMetaTagVersion();
390: $sql = 'SELECT idmetatagversion AS id
391: FROM `%s`
392: WHERE idartlang = %d AND version IN (
393: SELECT max(version)
394: FROM `%s`
395: WHERE idartlang = %d AND version <= %d)';
396: $this->db->query(
397: $sql,
398: cRegistry::getDbTableName('meta_tag_version'),
399: $this->get('idartlang'),
400: cRegistry::getDbTableName('meta_tag_version'),
401: $this->get('idartlang'),
402: $this->get('version')
403: );
404: while ($this->db->nextRecord()) {
405: $metaTagVersionIds[] = $this->db->f('id');
406: }
407: if (isset($metaTagVersionIds)) {
408: foreach ($metaTagVersionIds AS $id) {
409: $metaTagVersion->loadBy('idmetatagversion', $id);
410: $metaTagVersion->markAsCurrent();
411: }
412: }
413: }
414:
415:
416: $this->markAsCurrentVersion(1);
417: conMakeArticleIndex($this->get('idartlang'), $this->get('idart'));
418: $purge = new cSystemPurge();
419: $purge->clearArticleCache($this->get('idartlang'));
420: }
421:
422: 423: 424: 425: 426: 427: 428:
429: public function markAsEditable($type = '') {
430:
431:
432: $parameters = $this->toArray();
433: $parameters['lastmodified'] = date('Y-m-d H:i:s');
434: unset($parameters['idartlangversion']);
435: $artLangVersionColl = new cApiArticleLanguageVersionCollection();
436: $artLangVersion = $artLangVersionColl->create($parameters);
437:
438: if ($type == 'content' || $type == 'complete') {
439:
440: $artLangVersion->loadByArticleLanguageIdAndVersion($artLangVersion->get('idartlang'), $artLangVersion->get('version'), true);
441: $contentVersion = new cApiContentVersion();
442: $apiType = new cApiType();
443: $this->_getArticleVersionContent();
444:
445:
446: $mergedContent = array();
447: foreach ($this->content AS $typeName => $typeids) {
448: foreach ($typeids AS $typeid => $value) {
449: $mergedContent[$typeName][$typeid] = '';
450: }
451: }
452: foreach ($artLangVersion->content AS $typeName => $typeids) {
453: foreach ($typeids AS $typeid => $value) {
454: $mergedContent[$typeName][$typeid] = '';
455: }
456: }
457:
458: foreach ($mergedContent AS $typeName => $typeids) {
459: foreach ($typeids AS $typeid => $value) {
460: $apiType->loadByType($typeName);
461: if (isset($this->content[$typeName][$typeid])) {
462: $contentParameters = array(
463: 'idartlang' => $this->get('idartlang'),
464: 'idtype' => $apiType->get('idtype'),
465: 'typeid' => $typeid,
466: 'version' => $this->get('version')
467: );
468: $contentVersion->loadByArticleLanguageIdTypeTypeIdAndVersion($contentParameters);
469:
470: if (isset($contentVersion)) {
471: $contentVersion->markAsEditable($artLangVersion->get('version'), 0);
472: }
473: } else {
474:
475: $contentParameters = array(
476: 'idartlang' => $artLangVersion->get('idartlang'),
477: 'idtype' => $apiType->get('idtype'),
478: 'typeid' => $typeid,
479: 'version' => $artLangVersion->get('version'),
480: 'author' => $this->get('author'),
481: 'deleted' => 1
482: );
483: $contentVersionColl = new cApiContentVersionCollection();
484: $contentVersionColl->create($contentParameters);
485: }
486: }
487: }
488: }
489:
490: if ($type == 'meta' || $type == 'complete') {
491:
492:
493: $metaTagVersion = new cApiMetaTagVersion();
494: $sql = 'SELECT idmetatagversion AS id
495: FROM `%s`
496: WHERE idartlang = %d AND version IN (
497: SELECT max(version)
498: FROM `%s`
499: WHERE idartlang = %d AND version <= %d);';
500: $this->db->query(
501: $sql,
502: cRegistry::getDbTableName('meta_tag_version'),
503: $this->get('idartlang'),
504: cRegistry::getDbTableName('meta_tag_version'),
505: $this->get('idartlang'),
506: $this->get('version')
507: );
508: while ($this->db->nextRecord()) {
509: $metaTagVersionIds[] = $this->db->f('id');
510: }
511: if (!empty($metaTagVersionIds)) {
512: foreach ($metaTagVersionIds AS $id) {
513: $metaTagVersion->loadBy('idmetatagversion', $id);
514: $metaTagVersion->markAsEditable($artLangVersion->get('version'));
515: }
516: } else {
517: $metaTagColl = new cApiMetaTagCollection();
518: $metaTag = new cApiMetaTag();
519: $ids = $metaTagColl->getIdsByWhereClause('idartlang = ' . $this->get('idartlang'));
520: foreach ($ids AS $id) {
521: $metaTag->loadByPrimaryKey($id);
522: $metaTag->markAsEditable($artLangVersion->get('version'));
523: }
524: }
525: }
526:
527: }
528:
529: 530: 531: 532: 533: 534: 535: 536: 537: 538: 539: 540:
541: public function loadByArticleLanguageIdAndVersion($idArtLang, $version, $fetchContent = false) {
542: $result = true;
543: if (!$this->isLoaded()) {
544: $props = array(
545: 'idartlang' => $idArtLang,
546: 'version' => $version
547: );
548: $recordSet = $this->_oCache->getItemByProperties($props);
549: if ($recordSet) {
550:
551: $this->loadByRecordSet($recordSet);
552: } else {
553: $idArtLangVersion = $this->_getIdArtLangVersion($idArtLang, $version);
554: $result = $this->loadByPrimaryKey($idArtLangVersion);
555: }
556: }
557:
558: if (true === $fetchContent) {
559: $this->_getArticleVersionContent();
560: }
561:
562: return $result;
563: }
564:
565: 566: 567: 568: 569: 570: 571: 572: 573: 574:
575: protected function _getIdArtLangVersion($idArtLang, $version) {
576:
577: $id = NULL;
578:
579: $where = 'idartlang = ' . $idArtLang . ' AND version = ' . $version;
580:
581: $artLangVersionColl = new cApiArticleLanguageVersionCollection();
582: $artLangVersionColl->select($where);
583:
584: while($item = $artLangVersionColl->next()){
585: $id = $item->get('idartlangversion');
586: }
587:
588: return isset($id) ? $id : 0;
589:
590: }
591:
592: 593: 594: 595: 596:
597: protected function _getArticleVersionContent() {
598:
599: if (NULL !== $this->content) {
600: return;
601: }
602:
603: $sql = 'SELECT b.type as type, a.typeid as typeid, a.value as value, a.version as version
604: FROM `%s` AS a
605: INNER JOIN `%s` as b
606: ON b.idtype = a.idtype
607: WHERE (a.idtype, a.typeid, a.version) IN
608: (SELECT idtype, typeid, max(version)
609: FROM %s
610: WHERE idartlang = %d AND version <= %d
611: GROUP BY idtype, typeid)
612: AND a.idartlang = %d
613: AND (a.deleted < 1 OR a.deleted IS NULL)
614: ORDER BY a.idtype, a.typeid;';
615:
616: $this->db->query(
617: $sql,
618: cRegistry::getDbTableName('content_version'),
619: cRegistry::getDbTableName('type'),
620: cRegistry::getDbTableName('content_version'),
621: $this->get('idartlang'), $this->get('version'),
622: $this->get('idartlang')
623: );
624:
625: $this->content = array();
626: while ($this->db->nextRecord()) {
627: $this->content[strtolower($this->db->f('type'))][$this->db->f('typeid')] = $this->db->f('value');
628: }
629:
630: }
631:
632: 633: 634: 635: 636: 637: 638: 639: 640: 641: 642: 643: 644: 645: 646: 647: 648: 649: 650: 651: 652: 653: 654: 655: 656: 657: 658: 659: 660: 661: 662: 663:
664: public function getContent($type = '', $id = NULL) {
665: if (NULL === $this->content) {
666:
667: $this->_getArticleVersionContent();
668: }
669:
670: return parent::getContent($type, $id);
671: }
672:
673: }
674: