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