Issues (238)

src/Template/Localize.php (3 issues)

Labels
Severity
1
<?php
2
3
namespace Translation\Template;
4
5
use View;
6
use Config;
7
8
/**
9
 * Methods to assit in the creation of the localize sidebar UI
10
 * and the locale form field
11
 *
12
 * @todo Translation
13
 */
14
class Localize
15
{
16
    /**
17
     * The model instance being localized
18
     */
19
    protected $item;
20
21
    /**
22
     * The model class being localized
23
     *
24
     * @var string
25
     */
26
    protected $model;
27
28
    /**
29
     * Other localizations for the `$item`
30
     */
31
    protected $other_localizations;
32
33
    /**
34
     * The title of this model, from the controller
35
     *
36
     * @var string
37
     */
38
    protected $title;
39
40
    /**
41
     * Store a model instance
42
     *
43
     * @param  Illuminate\Database\Eloquent\Model $item
0 ignored issues
show
The type Translation\Template\Ill...Database\Eloquent\Model was not found. Did you mean Illuminate\Database\Eloquent\Model? If so, make sure to prefix the type with \.
Loading history...
44
     * @return $this
45
     */
46
    public function item($item)
47
    {
48
        if (!$this->model) {
49
            $this->model = get_class($item);
50
        }
51
52
        $this->item = $item;
53
54
        return $this;
55
    }
56
57
    /**
58
     * Store the model class name
59
     *
60
     * @param  string $model
61
     * @return $this
62
     */
63
    public function model($model)
64
    {
65
        $this->model = $model;
66
67
        return $this;
68
    }
69
70
    /**
71
     * The title of this model, from the controller
72
     *
73
     * @param  string $title
74
     * @return $this
75
     */
76
    public function title($title)
77
    {
78
        $this->title = $title;
79
80
        return $this;
81
    }
82
83
    /**
84
     * Check if the localize UI should be displayed
85
     *
86
     * @return boolean
87
     */
88
    public function hidden()
89
    {
90
        $class = $this->model; // Must be a local var to test
91
92
        // There aren't multiple locales specified
93
        if (count(\Illuminate\Support\Facades\Config::get('site.site.locales')) <= 1 ) { return true;
94
        }
95
96
        // We're editing a model with no locale attribute
97
        if ($this->item && !$this->item->locale) { return true;
98
        }
99
100
        // The model was explicitly disabled
101
        if (!property_exists($class, 'localizable') || $class::$localizable === false ) {
102
            return true;
103
        }
104
105
        // Auto localize is turned on and we're on a child model
106
        if (\Illuminate\Support\Facades\Config::get('painel.core.auto_localize_root_models')
107
            && app('facilitador.wildcard')->detectParent()
0 ignored issues
show
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
            && /** @scrutinizer ignore-call */ app('facilitador.wildcard')->detectParent()
Loading history...
108
        ) { return true;
109
        }
110
111
        // If auto-localizeable is turned off and this model doesn't have it
112
        // turned on
113
        if (!\Illuminate\Support\Facades\Config::get('painel.core.auto_localize_root_models')
114
            && property_exists($class, 'localizable') && !$class::$localizable
115
        ) { return true;
116
        }
117
118
        // Otherwise, allow localization
119
        return false;
120
    }
121
122
    /**
123
     * Get a hash of locales that are available for the item
124
     *
125
     * @return array
126
     */
127
    public function localizableLocales()
128
    {
129
        // Keep only locales that don't exist in ...
130
        return array_diff_key(
131
            Config::get('site.site.locales'),
132
            // ... the locales of other localizations ...
133
            $this->other()->pluck('locale')->flip()->toArray(),
134
            // ... and the model's locale
135
            [$this->item->locale => null]
136
        );
137
    }
138
139
    /**
140
     * Get other localizations, storing them internally for use in multiple places
141
     *
142
     * @return Illuminate\Database\Eloquent\Collection
0 ignored issues
show
The type Translation\Template\Ill...ase\Eloquent\Collection was not found. Did you mean Illuminate\Database\Eloquent\Collection? If so, make sure to prefix the type with \.
Loading history...
143
     */
144
    public function other()
145
    {
146
        if ($this->other_localizations === null) {
147
            $this->other_localizations = $this->item->otherLocalizations()->get();
148
        }
149
150
        return $this->other_localizations;
151
    }
152
153
    /**
154
     * Render the sidebar, "Localize" UI
155
     *
156
     * @return string
157
     */
158
    public function __toString()
159
    {
160
        return View::make(
161
            'pedreiro::shared.form.relationships._localize', [
162
            'model' => $this->model,
163
            'item' => $this->item,
164
            'title' => $this->title,
165
            'localize' => $this,
166
            ]
167
        )->render();
168
    }
169
}
170