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