GetArticleFromDataBase   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 16
eloc 68
c 3
b 0
f 0
dl 0
loc 116
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A get() 0 19 4
B getjs() 0 55 10
1
<?php
2
3
namespace Sirgrimorum\TransArticles;
4
5
use Exception;
6
use Sirgrimorum\TransArticles\Models\Article;
7
use App;
8
9
class GetArticleFromDataBase {
10
11
    /**
12
     * Actual localization
13
     * 
14
     * @var string 
15
     */
16
    protected $lang;
17
18
    /**
19
     * @var string
20
     */
21
    private $app;
22
23
    /**
24
     * 
25
     * @param string $lang If '' get the current localization
26
     */
27
    function __construct($app, $lang = '') {
28
        $this->app = $app;
29
        if ($lang == '') {
30
            $this->lang = $this->app->getLocale();
31
        } else {
32
            $this->lang = $lang;
33
        }
34
    }
35
    
36
    /**
37
     * Return the translation for the article
38
     * @param String $nickname The article to load using dot notation
39
     * @return String The content of the article localized, if not found, return the first article in a diferent language, if neither, returns de $nickname
40
     */
41
    public static function get($nickname) {
42
        try {
43
            $lang = App::getLocale();
44
            $modelClass = config('sirgrimorum.transarticles.default_articles_model');
45
            $langColumn = config('sirgrimorum.transarticles.default_lang_column');
46
            $findArticle = config('sirgrimorum.transarticles.default_findarticle_function_name');
47
            $article = $modelClass::{$findArticle}($nickname)->where($langColumn, "=", $lang)->first();
48
            if ($article) {
49
                return $article->content;
50
            } else {
51
                $article = $modelClass::{$findArticle}($nickname)->first();
52
                if ($article) {
53
                    return $article->content . "<small><span class='label label-warning'>" . $article->{$langColumn} . "</span></small>";
54
                } else {
55
                    return $nickname;
56
                }
57
            }
58
        } catch (Exception $ex) {
59
            return $nickname . "<pre class='label label-warning'>" . print_r([$ex->getMessage(), $ex->getTraceAsString()], true) . "</pre>";
60
        }
61
    }
62
63
    /**
64
     * return the JavaScript from article table
65
     * 
66
     *
67
     * @param String $scope The scope to load
68
     * 
69
     */
70
    public static function getjs($scope, $basevar = '') {
71
        if ($basevar == '') {
72
            $basevar = config('sirgrimorum.transarticles.default_base_var');
73
        }
74
        $lang = App::getLocale();
75
        $listo = false;
76
        try {
77
            $modelClass = config('sirgrimorum.transarticles.default_articles_model');
78
            $langColumn = config('sirgrimorum.transarticles.default_lang_column');
79
            $findArticles = config('sirgrimorum.transarticles.default_findarticles_function_name');
80
            $findArticle = config('sirgrimorum.transarticles.default_findarticle_function_name');
81
            $articles = $modelClass::{$findArticles}($scope)->where($langColumn, "=", $lang)->get();
82
            if ($articles) {
83
                $listo = true;
84
            } else {
85
                $articles = $modelClass::{$findArticles}($scope)->get();
86
                if ($articles) {
87
                    $listo = true;
88
                } else {
89
                    $articles = $modelClass::{$findArticle}($scope)->where($langColumn, "=", $lang)->first();
90
                    $listo = false;
91
                    if ($articles) {
92
                        $jsarray = [];
93
                        data_fill($jsarray, $scope, $articles->content);
94
                    } else {
95
                        $articles = $modelClass::{$findArticle}($scope)->first();
96
                        if ($articles) {
97
                            $jsarray = [];
98
                            data_fill($jsarray, $scope, $articles->content);
99
                            //$jsarray = $articles->content;
100
                        } else {
101
                            $jsarray = [];
102
                            data_fill($jsarray, $scope, $scope);
103
                        }
104
                    }
105
                }
106
            }
107
        } catch (Exception $ex) {
108
            return $scope . " - Error:" . print_r($ex->getMessage(), true);
109
        }
110
        if ($listo) {
111
            if ($articles) {
112
                $trans = [];
113
                foreach ($articles as $article) {
114
                    $trans[$article->nickname] = $article->content;
115
                }
116
                $jsarray = json_encode($trans);
117
                return "<script>window.{$basevar} = window.{$basevar} || {};{$basevar}.{$scope} = {$jsarray};</script>";
118
            } else {
119
                $jsarray = [];
120
                data_fill($jsarray, $scope, $scope);
121
            }
122
        } 
123
        $jsarray = json_encode($jsarray);
124
        return "<script>window.{$basevar} = window.{$basevar} || {};{$basevar} = {$jsarray};</script>";
125
    }
126
127
}
128