Completed
Push — master ( d59a3b...064224 )
by Ricardo
13:04
created

Localize::item()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
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
 * @todo Translation
12
 */
13
class Localize
14
{
15
    /**
16
     * The model instance being localized
17
     */
18
    protected $item;
19
20
    /**
21
     * The model class being localized
22
     *
23
     * @var string
24
     */
25
    protected $model;
26
27
    /**
28
     * Other localizations for the `$item`
29
     */
30
    protected $other_localizations;
31
32
    /**
33
     * The title of this model, from the controller
34
     *
35
     * @var string
36
     */
37
    protected $title;
38
39
    /**
40
     * Store a model instance
41
     *
42
     * @param  Illuminate\Database\Eloquent\Model $item
0 ignored issues
show
Bug introduced by
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...
43
     * @return $this
44
     */
45
    public function item($item)
46
    {
47
        if (!$this->model) {
48
            $this->model = get_class($item);
49
        }
50
51
        $this->item = $item;
52
53
        return $this;
54
    }
55
56
    /**
57
     * Store the model class name
58
     *
59
     * @param  string $model
60
     * @return $this
61
     */
62
    public function model($model)
63
    {
64
        $this->model = $model;
65
66
        return $this;
67
    }
68
69
    /**
70
     * The title of this model, from the controller
71
     *
72
     * @param  string $title
73
     * @return $this
74
     */
75
    public function title($title)
76
    {
77
        $this->title = $title;
78
79
        return $this;
80
    }
81
82
    /**
83
     * Check if the localize UI should be displayed
84
     *
85
     * @return boolean
86
     */
87
    public function hidden()
88
    {
89
        $class = $this->model; // Must be a local var to test
90
91
        // There aren't multiple locales specified
92
        if (count(\Illuminate\Support\Facades\Config::get('site.site.locales')) <= 1 ) { return true;
93
        }
94
95
        // We're editing a model with no locale attribute
96
        if ($this->item && !$this->item->locale) { return true;
97
        }
98
99
        // The model was explicitly disabled
100
        if (!property_exists($class, 'localizable') || $class::$localizable === false ) {
101
            return true;
102
        }
103
104
        // Auto localize is turned on and we're on a child model
105
        if (\Illuminate\Support\Facades\Config::get('painel.core.auto_localize_root_models')
106
            && app('facilitador.wildcard')->detectParent()
0 ignored issues
show
Bug introduced by
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

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