Completed
Push — master ( 01b1a5...81f493 )
by Michael
04:03
created

SmartPersistableMlObjectHandler::getObjects()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 34
Code Lines 23

Duplication

Lines 10
Ratio 29.41 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
cc 3
eloc 23
c 5
b 1
f 0
nc 3
nop 5
dl 10
loc 34
rs 8.8571
1
<?php
2
3
/**
4
 * Contains the basis classes for managing any objects derived from SmartObjects
5
 *
6
 * @license    GNU
7
 * @author     marcan <[email protected]>
8
 * @link       http://smartfactory.ca The SmartFactory
9
 * @package    SmartObject
10
 * @subpackage SmartObjectCore
11
 */
12
13
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
14
include_once XOOPS_ROOT_PATH . '/modules/smartobject/class/smartobject.php';
15
16
/**
17
 * SmartObject base Multilanguage-enabled class
18
 *
19
 * Base class representing a single SmartObject with multilanguages capabilities
20
 *
21
 * @package SmartObject
22
 * @author  marcan <[email protected]>
23
 * @link    http://smartfactory.ca The SmartFactory
24
 */
25
class SmartMlObject extends SmartObject
26
{
27
    /**
28
     * SmartMlObject constructor.
29
     */
30
    public function __construct()
31
    {
32
        $this->initVar('language', XOBJ_DTYPE_TXTBOX, 'english', false, null, '', true, _CO_SOBJECT_LANGUAGE_CAPTION, _CO_SOBJECT_LANGUAGE_DSC, true, true);
33
        $this->setControl('language', 'language');
34
    }
35
36
    /**
37
     * If object is not new, change the control of the not-multilanguage fields
38
     *
39
     * We need to intercept this function from SmartObject because if the object is not new...
40
     */
41
    // function getForm() {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
42
43
    //}
44
45
    /**
46
     * Strip Multilanguage Fields
47
     *
48
     * Get rid of all the multilanguage fields to have an object with only global fields.
49
     * This will be usefull when creating the ML object for the first time. Then we will be able
50
     * to create translations.
51
     */
52 View Code Duplication
    public function stripMultilanguageFields()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        $objectVars    =& $this->getVars();
55
        $newObjectVars = array();
56
        foreach ($objectVars as $key => $var) {
57
            if (!$var['multilingual']) {
58
                $newObjectVars[$key] = $var;
59
            }
60
        }
61
        $this->vars = $newObjectVars;
62
    }
63
64 View Code Duplication
    public function stripNonMultilanguageFields()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
    {
66
        $objectVars    =& $this->getVars();
67
        $newObjectVars = array();
68
        foreach ($objectVars as $key => $var) {
69
            if ($var['multilingual'] || $key == $this->handler->keyName) {
70
                $newObjectVars[$key] = $var;
71
            }
72
        }
73
        $this->vars = $newObjectVars;
74
    }
75
76
    /**
77
     * Make non multilanguage fields read only
78
     *
79
     * This is used when we are creating/editing a translation.
80
     * We only want to edit the multilanguag fields, not the global one.
81
     */
82
    public function makeNonMLFieldReadOnly()
83
    {
84
        foreach ($this->getVars() as $key => $var) {
85
            //if (($key == 'language') || (!$var['multilingual'] && $key <> $this->handler->keyName)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
86
            if (!$var['multilingual'] && $key != $this->handler->keyName) {
87
                $this->setControl($key, 'label');
88
            }
89
        }
90
    }
91
92
    /**
93
     * @param  bool   $onlyUrl
94
     * @param  bool   $withimage
95
     * @return string
96
     */
97
    public function getEditLanguageLink($onlyUrl = false, $withimage = true)
98
    {
99
        $controller = new SmartObjectController($this->handler);
100
101
        return $controller->getEditLanguageLink($this, $onlyUrl, $withimage);
102
    }
103
}
104
105
/**
106
 * Class SmartPersistableMlObjectHandler
107
 */
108
class SmartPersistableMlObjectHandler extends SmartPersistableObjectHandler
109
{
110
    /**
111
     * @param  null  $criteria
112
     * @param  bool  $id_as_key
113
     * @param  bool  $as_object
114
     * @param  bool  $debug
115
     * @param  bool  $language
116
     * @return array
117
     */
118
    public function getObjects($criteria = null, $id_as_key = false, $as_object = true, $debug = false, $language = false)
119
    {
120
        // Create the first part of the SQL query to join the "_text" table
121
        $sql = 'SELECT * FROM ' .
122
               $this->table .
123
               ' AS ' .
124
               $this->_itemname .
125
               ' INNER JOIN ' .
126
               $this->table .
127
               '_text AS ' .
128
               $this->_itemname .
129
               '_text ON ' .
130
               $this->_itemname .
131
               '.' .
132
               $this->keyName .
133
               '=' .
134
               $this->_itemname .
135
               '_text.' .
136
               $this->keyName;
137
138 View Code Duplication
        if ($language) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
            // If a language was specified, then let's create a WHERE clause to only return the objects associated with this language
140
141
            // if no criteria was previously created, let's create it
142
            if (!$criteria) {
143
                $criteria = new CriteriaCompo();
144
            }
145
            $criteria->add(new Criteria('language', $language));
146
147
            return parent::getObjects($criteria, $id_as_key, $as_object, $debug, $sql);
148
        }
149
150
        return parent::getObjects($criteria, $id_as_key, $as_object, $debug, $sql);
151
    }
152
153
    /**
154
     * @param  mixed $id
155
     * @param  bool  $language
156
     * @param  bool  $as_object
157
     * @param  bool  $debug
158
     * @return mixed
159
     */
160
    public function &get($id, $language = false, $as_object = true, $debug = false)
161
    {
162 View Code Duplication
        if (!$language) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
163
            return parent::get($id, $as_object, $debug);
164
        } else {
165
            $criteria = new CriteriaCompo();
166
            $criteria->add(new Criteria('language', $language));
167
168
            return parent::get($id, $as_object, $debug, $criteria);
169
        }
170
    }
171
172
    public function changeTableNameForML()
173
    {
174
        $this->table = $this->db->prefix($this->_moduleName . '_' . $this->_itemname . '_text');
175
    }
176
}
177