Passed
Pull Request — master (#2)
by Michael
07:08 queued 02:34
created

SonglistRequestsHandler   A

Complexity

Total Complexity 39

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 117
dl 0
loc 158
rs 9.28
c 0
b 0
f 0
wmc 39

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getURL() 0 6 2
D insert() 0 105 16
A __construct() 0 3 1
A filterFields() 0 2 1
A getFilterForm() 0 6 2
C getFilterCriteria() 0 24 17
1
<?php
2
3
if (!defined('XOOPS_ROOT_PATH')) {
4
	exit();
5
}
6
7
include_once(dirname(dirname(__FILE__)).'/include/songlist.object.php');
8
include_once(dirname(dirname(__FILE__)).'/include/songlist.form.php');
9
10
class SonglistRequests extends XoopsObject
11
{
12
13
    function SonglistRequests($fid = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Unused Code introduced by
The parameter $fid is not used and could be removed. ( Ignorable by Annotation )

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

13
    function SonglistRequests(/** @scrutinizer ignore-unused */ $fid = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
14
    {
15
        $this->initVar('rid', XOBJ_DTYPE_INT, 0, false);
16
        $this->initVar('aid', XOBJ_DTYPE_INT, 0, false);
17
		$this->initVar('artist', XOBJ_DTYPE_TXTBOX, null, false, 128);
18
		$this->initVar('album', XOBJ_DTYPE_TXTBOX, null, false, 128);
19
		$this->initVar('title', XOBJ_DTYPE_TXTBOX, null, false, 128);
20
		$this->initVar('lyrics', XOBJ_DTYPE_OTHER, null, false);
21
		$this->initVar('uid', XOBJ_DTYPE_INT, 0, false);
22
		$this->initVar('name', XOBJ_DTYPE_TXTBOX, null, false, 128);
23
		$this->initVar('email', XOBJ_DTYPE_TXTBOX, null, false, 255);
24
		$this->initVar('songid', XOBJ_DTYPE_TXTBOX, null, false, 32);
25
		$this->initVar('sid', XOBJ_DTYPE_INT, 0, false);
26
		$this->initVar('created', XOBJ_DTYPE_INT, 0, false);
27
		$this->initVar('updated', XOBJ_DTYPE_INT, 0, false);
28
	}
29
30
	function getForm($as_array = false) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
31
		return songlist_requests_get_form($this, $as_array);
32
	}
33
	
34
	function toArray() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
35
		$ret = parent::toArray();
36
		$form = $this->getForm(true);
37
		$form['songid'] = new XoopsFormText('', $this->getVar('rid').'[songid]', 11, 32);
38
		foreach($form as $key => $element) {
39
			$ret['form'][$key] = $form[$key]->render();	
40
		}
41
		foreach(array('created', 'updated') as $key) {
42
			if ($this->getVar($key)>0) {
43
				$ret['form'][$key] = date(_DATESTRING, $this->getVar($key)); 
44
				$ret[$key] = date(_DATESTRING, $this->getVar($key));
45
			}
46
		}
47
		return $ret;
48
	}
49
    
50
}
51
52
53
class SonglistRequestsHandler extends XoopsPersistableObjectHandler
54
{
55
    function __construct(&$db) 
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
56
    {
57
        parent::__construct($db, "songlist_requests", 'SonglistRequests', "rid", "name");
58
    }
59
60
	function filterFields() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
61
		return array('rid', 'artist', 'album', 'title', 'lyrics', 'uid', 'name', 'email', 'songid', 'sid', 'created', 'updated');
62
	}
63
	
64
    function getFilterCriteria($filter) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
65
    	$parts = explode('|', $filter);
66
    	$criteria = new CriteriaCompo();
67
    	foreach($parts as $part) {
68
    		$var = explode(',', $part);
69
    		if (!empty($var[1])&&!is_numeric($var[0])) {
70
    			$object = $this->create();
71
    			if (		$object->vars[$var[0]]['data_type']==XOBJ_DTYPE_TXTBOX || 
72
    						$object->vars[$var[0]]['data_type']==XOBJ_DTYPE_TXTAREA) 	{
73
    				$criteria->add(new Criteria('`'.$var[0].'`', '%'.$var[1].'%', (isset($var[2])?$var[2]:'LIKE')));
74
    			} elseif (	$object->vars[$var[0]]['data_type']==XOBJ_DTYPE_INT || 
75
    						$object->vars[$var[0]]['data_type']==XOBJ_DTYPE_DECIMAL || 
76
    						$object->vars[$var[0]]['data_type']==XOBJ_DTYPE_FLOAT ) 	{
77
    				$criteria->add(new Criteria('`'.$var[0].'`', $var[1], (isset($var[2])?$var[2]:'=')));			
78
				} elseif (	$object->vars[$var[0]]['data_type']==XOBJ_DTYPE_ENUM ) 	{
79
    				$criteria->add(new Criteria('`'.$var[0].'`', $var[1], (isset($var[2])?$var[2]:'=')));    				
80
				} elseif (	$object->vars[$var[0]]['data_type']==XOBJ_DTYPE_ARRAY ) 	{
81
    				$criteria->add(new Criteria('`'.$var[0].'`', '%"'.$var[1].'";%', (isset($var[2])?$var[2]:'LIKE')));    				
82
				}
83
    		} elseif (!empty($var[1])&&is_numeric($var[0])) {
84
    			$criteria->add(new Criteria($var[0], $var[1]));
85
    		}
86
    	}
87
    	return $criteria;
88
    }
89
        
90
    function getFilterForm($filter, $field, $sort='created', $op = 'dashboard', $fct='list') {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
91
    	$ele = songlist_getFilterElement($filter, $field, $sort, $op, $fct);
92
    	if (is_object($ele))
93
    		return $ele->render();
94
    	else 
95
    		return '&nbsp;';
96
    }
97
    
98
	function insert($obj, $force=true) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
99
    	if ($obj->isNew()) {
100
    		$obj->setVar('created', time());	
101
    		$new = true;
102
    		$sendmail = true;
103
       	} else {
104
    		$obj->setVar('updated', time());
105
    		$new = false;
106
    		if ($obj->vars['songid']['changed']==true) {
107
    			$songs_handler = xoops_getmodulehandler('songs', 'songlist');
108
    			$criteria = new Criteria('songid', $obj->getVar('songid'));
109
    			$songs = $songs_handler->getObjects($criteria, false);
0 ignored issues
show
Bug introduced by
The method getObjects() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of said class. However, the method does not exist in XoopsRankHandler or XoUserHandler. Are you sure you never get one of those? ( Ignorable by Annotation )

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

109
    			/** @scrutinizer ignore-call */ 
110
       $songs = $songs_handler->getObjects($criteria, false);
Loading history...
110
    			if (is_object($songs[0])) {
111
    				foreach($songs[0]->getVar('aids') as $aid)
112
    					$ad[] = $aid;
113
    				$obj->setVar('sid', $songs[0]->getVar('sid'));
114
    				$obj->setVar('aid', $ad[0]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ad seems to be defined by a foreach iteration on line 111. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
115
    				$sendmail = true;
116
    			}
117
    		}
118
    	}
119
    	if ($rid = parent::insert($obj, $force)) {
120
    		if ($sendmail==true) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sendmail does not seem to be defined for all execution paths leading up to this point.
Loading history...
121
    			if ($new==true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
122
    				xoops_loadLanguage('email', 'songlist');
123
    				$xoopsMailer =& getMailer();
124
					$xoopsMailer->setHTML(true);
125
					$xoopsMailer->setTemplateDir($GLOBALS['xoops']->path('/modules/songlist/language/'.$GLOBALS['xoopsConfig']['language'].'/mail_templates/'));
126
					$xoopsMailer->setTemplate('songlist_request_created.html');
127
					$xoopsMailer->setSubject(sprintf(_MN_SONGLIST_SUBJECT_REQUESTMADE, $rid));
128
					
129
					foreach(explode('|', $GLOBALS['songlistModuleConfig']['email']) as $email)
130
						$xoopsMailer->setToEmails($email);
131
					
132
					$xoopsMailer->setToEmails($obj->getVar('email'));
133
					
134
					$xoopsMailer->assign("SITEURL", XOOPS_URL);
135
					$xoopsMailer->assign("SITENAME", $GLOBALS['xoopsConfig']['sitename']);
136
					$xoopsMailer->assign("RID", $rid);
137
					$xoopsMailer->assign("TITLE", $obj->getVar('title'));
138
					$xoopsMailer->assign("ALBUM", $obj->getVar('album'));
139
					$xoopsMailer->assign("ARTIST", $obj->getVar('artist'));
140
					$xoopsMailer->assign("EMAIL", $obj->getVar('email'));	
141
					$xoopsMailer->assign("NAME", $obj->getVar('name'));
142
					
143
					if(!$xoopsMailer->send() ){
144
						xoops_error($xoopsMailer->getErrors(true), 'Email Send Error');
145
					}
146
    			} else {
147
    				xoops_loadLanguage('email', 'songlist');
148
    				$songs_handler = xoops_getmodulehandler('songs', 'songlist');
149
    				$artists_handler = xoops_getmodulehandler('artists', 'songlist');
150
    				$albums_handler = xoops_getmodulehandler('albums', 'songlist');
0 ignored issues
show
Unused Code introduced by
The assignment to $albums_handler is dead and can be removed.
Loading history...
151
    				$genre_handler = xoops_getmodulehandler('genre', 'songlist');
152
    				
153
    				$song = $songs_handler->get($obj->getVar('sid'));
154
    				if (is_object($song)) {
155
    					$sng = $genre->getVar('title');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $genre seems to be never defined.
Loading history...
156
    				}
157
    				$album = $album_handler->get($song->getVar('abid'));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $album_handler does not exist. Did you maybe mean $albums_handler?
Loading history...
158
    				if (is_object($album)) {
159
    					$alb = $genre->getVar('title');
160
    					$alb_img = $genre->getImage();
0 ignored issues
show
Unused Code introduced by
The assignment to $alb_img is dead and can be removed.
Loading history...
161
    				}
162
    				$genre = $genre_handler->get($song->getVar('abid'));
163
    				if (is_object($genre)) {
164
    					$gen = $genre->getVar('name');
165
    				}
166
    				$artists = $artists_handler->getObjects(new Criteria('aid', '('.implode(',', $song->getVar('aid')).')', 'IN'), false);
0 ignored issues
show
Bug introduced by
It seems like $song->getVar('aid') can also be of type boolean and null and string; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

166
    				$artists = $artists_handler->getObjects(new Criteria('aid', '('.implode(',', /** @scrutinizer ignore-type */ $song->getVar('aid')).')', 'IN'), false);
Loading history...
167
    				$art = '';
168
    				foreach($artists as $id => $artist) {
169
    					$art .= $artist->getVar('name') . ($id<sizeof($artists)-1?', ':'');
170
    				}
171
    				$xoopsMailer =& getMailer();
172
					$xoopsMailer->setHTML(true);
173
					$xoopsMailer->setTemplateDir($GLOBALS['xoops']->path('/modules/songlist/language/'.$GLOBALS['xoopsConfig']['language'].'/mail_templates/'));
174
					$xoopsMailer->setTemplate('songlist_request_updated.html');
175
					$xoopsMailer->setSubject(sprintf(_MN_SONGLIST_SUBJECT_REQUESTFOUND, $rid));
176
					
177
					$xoopsMailer->setToEmails($obj->getVar('email'));
178
					
179
					$xoopsMailer->assign("SITEURL", XOOPS_URL);
180
					$xoopsMailer->assign("SITENAME", $GLOBALS['xoopsConfig']['sitename']);
181
					$xoopsMailer->assign("RID", $rid);
182
					$xoopsMailer->assign("TITLE", $obj->getVar('title'));
183
					$xoopsMailer->assign("ALBUM", $obj->getVar('album'));
184
					$xoopsMailer->assign("ARTIST", $obj->getVar('artist'));
185
					$xoopsMailer->assign("EMAIL", $obj->getVar('email'));	
186
					$xoopsMailer->assign("NAME", $obj->getVar('name'));
187
					$xoopsMailer->assign("SONGID", $song->getVar('songid'));
188
					$xoopsMailer->assign("SONGURL", $song->getURL());
0 ignored issues
show
Bug introduced by
The method getURL() does not exist on XoopsObject. It seems like you code against a sub-type of XoopsObject such as SonglistAlbums or XoopsModules\Songlist\Genre or XoopsModules\Songlist\Voice or SonglistGenre or SonglistVoice or XoopsModules\Songlist\Category or XoopsModules\Songlist\Songs or SonglistArtists or SonglistSongs or XoopsModules\Songlist\Albums or SonglistCategory or XoopsModules\Songlist\Artists. ( Ignorable by Annotation )

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

188
					$xoopsMailer->assign("SONGURL", $song->/** @scrutinizer ignore-call */ getURL());
Loading history...
189
					$xoopsMailer->assign("FOUNDTITLE", $sng);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sng does not seem to be defined for all execution paths leading up to this point.
Loading history...
190
					$xoopsMailer->assign("FOUNDALBUM", $alb);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $alb does not seem to be defined for all execution paths leading up to this point.
Loading history...
191
					$xoopsMailer->assign("FOUNDARTIST", $art);
192
					$xoopsMailer->assign("FOUNDGENRE", $gen);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $gen does not seem to be defined for all execution paths leading up to this point.
Loading history...
193
					$xoopsMailer->assign("EMAIL", $obj->getVar('email'));	
194
					$xoopsMailer->assign("NAME", $obj->getVar('name'));
195
					
196
					if(!$xoopsMailer->send() ){
197
						xoops_error($xoopsMailer->getErrors(true), 'Email Send Error');
198
					}
199
    			}		
200
    		}
201
    	}
202
    	return $rid;
203
    }
204
    
205
    function getURL() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
206
    	global $file, $op, $fct, $id, $value, $gid, $cid, $start, $limit;
207
    	if ($GLOBALS['songlistModuleConfig']['htaccess']) {
208
    		return XOOPS_URL.'/'.$GLOBALS['songlistModuleConfig']['baseofurl'].'/'.$file.'/'.$op.'-'.$fct.$GLOBALS['songlistModuleConfig']['endofurl'];
209
    	} else {
210
    		return XOOPS_URL.'/modules/songlist/'.$file.'.php?op='.$op.'&fct='.$fct;
211
    	}
212
    }
213
}
214
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...