/
position.php
504 lines (463 loc) · 13.4 KB
/
position.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
<?php
/**
* @copyright Copyright (C) 2006-2015 joomleague.at. All rights reserved.
* @license GNU/GPL,see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License,and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/
defined('_JEXEC') or die;
jimport('joomla.application.component.model');
require_once JPATH_COMPONENT.'/models/item.php';
/**
* Joomleague Component position Model
*
* @author JoomLeague Team
* @package JoomLeague
*/
class JoomleagueModelPosition extends JoomleagueModelItem
{
/**
* Method to remove positons of only one project
*
* @access public
* @return boolean True on success
*/
function deleteOne($project_id)
{
if ($project_id > 0)
{
$query='DELETE FROM #__joomleague_project_position WHERE project_id='.(int)$project_id;
$this->_db->setQuery($query);
if (!$this->_db->execute())
{
$this->setError($this->_db->getErrorMsg());
return false;
}
}
return true;
}
/**
* Method to remove a position
*
* @access public
* @return boolean True on success
*/
function delete(&$pks=array())
{
if (count($pks))
{
$cids=implode(',',$pks);
$query="SELECT pos.id FROM #__joomleague_project_position AS ppos
INNER JOIN #__joomleague_position AS pos ON pos.id = ppos.position_id
WHERE pos.id IN ($cids)";
$this->_db->setQuery($query);
if ($this->_db->loadResult())
{
$this->setError(JText::_('COM_JOOMLEAGUE_ADMIN_POSITION_MODEL_ERROR_P_POSITION_EXISTS'));
return false;
}
$query="SELECT id FROM #__joomleague_project_referee AS pref
INNER JOIN #__joomleague_project_position AS ppos ON ppos.id = pref.project_position_id
INNER JOIN #__joomleague_position AS pos ON pos.id = ppos.position_id
WHERE pos.id IN ($cids)";
$this->_db->setQuery($query);
if ($this->_db->loadResult())
{
$this->setError(JText::_('COM_JOOMLEAGUE_ADMIN_POSITION_MODEL_ERROR_P_REFEREE_EXISTS'));
return false;
}
$query="SELECT id FROM #__joomleague_team_player AS tp
INNER JOIN #__joomleague_project_position AS ppos ON ppos.id = tp.project_position_id
INNER JOIN #__joomleague_position AS pos ON pos.id = ppos.position_id
WHERE pos.id IN ($cids)";
$this->_db->setQuery($query);
if ($this->_db->loadResult())
{
$this->setError(JText::_('COM_JOOMLEAGUE_ADMIN_POSITION_MODEL_ERROR_PLAYER_EXISTS'));
return false;
}
$query="SELECT id FROM #__joomleague_team_staff AS ts
INNER JOIN #__joomleague_project_position AS ppos ON ppos.id = ts.project_position_id
INNER JOIN #__joomleague_position AS pos ON pos.id = ppos.position_id
WHERE pos.position_id IN ($cids)";
$this->_db->setQuery($query);
if ($this->_db->loadResult())
{
$this->setError(JText::_('COM_JOOMLEAGUE_ADMIN_POSITION_MODEL_ERROR_STAFF_EXISTS'));
return false;
}
$query="SELECT id FROM #__joomleague_person
WHERE position_id IN ($cids)";
$this->_db->setQuery($query);
if ($this->_db->loadResult())
{
$this->setError(JText::_('COM_JOOMLEAGUE_ADMIN_POSITION_MODEL_ERROR_PERSON_EXISTS'));
return false;
}
$query = ' DELETE
FROM #__joomleague_position_eventtype
WHERE position_id IN ( ' . $cids . ' )';
$this->_db->setQuery( $query );
if ( !$this->_db->execute() )
{
$this->setError( $this->_db->getErrorMsg() );
return false;
}
$query = ' DELETE
FROM #__joomleague_position_statistic
WHERE position_id IN ( ' . $cids . ' )';
$this->_db->setQuery( $query );
if ( !$this->_db->execute() )
{
$this->setError( $this->_db->getErrorMsg() );
return false;
}
return parent::delete($pks);
}
return true;
}
/**
* Method to load content position data
*
* @access private
* @return boolean True on success
*/
function _loadData()
{
// Lets load the content if it doesn't already exist
if (empty($this->_data))
{
$this->_data = parent::getItem($this->_id);
return (boolean) $this->_data;
}
return true;
}
/**
* Method to initialise the position data
*
* @access private
* @return boolean True on success
*/
function _initData()
{
// Lets load the content if it doesn't already exist
if (empty($this->_data))
{
$position = new stdClass();
$position->id = 0;
$position->name = null;
$position->parent_id = 0;
$position->persontype = '';
$position->sports_type_id = '1';
$position->published = 0;
$position->ordering = 0;
$position->checked_out = 0;
$position->checked_out_time = 0;
$position->modifie = null;
$position->modified_by = null;
$position->alias = null;
$this->_data = $position;
return (boolean) $this->_data;
}
return true;
}
/**
* Method to return the query that will obtain all ordering versus positions (with sportstype between brackets)
* It can be used to fill a list box with value/text data.
*
* @access public
* @return string
*/
function getOrderingAndPositionsQuery()
{
return 'SELECT pos.ordering AS value, concat(pos.name, " (", st.name, ")") AS text
FROM #__joomleague_position pos
LEFT JOIN #__joomleague_sports_type st ON st.id = pos.sports_type_id
ORDER BY pos.ordering';
}
/**
* Method to return a events array (id,name)
*
* @access public
* @return array
*/
function getEvents()
{
$query='SELECT evt.id AS value,
evt.name AS eventtype,
st.name AS sportstype
FROM #__joomleague_eventtype AS evt
LEFT JOIN #__joomleague_sports_type AS st ON st.id = evt.sports_type_id
WHERE evt.published=1
ORDER BY evt.name ASC ';
$this->_db->setQuery($query);
if (!$result=$this->_db->loadObjectList())
{
$this->setError($this->_db->getErrorMsg());
return false;
}
foreach ($result as $position){
$position->text=JText::_($position->eventtype)." (".JText::_($position->sportstype).")";
}
return $result;
}
/**
* Method to return the position events array (id,name)
*
* @access public
* @return array
*/
function getEventsPosition()
{
$query=' SELECT p.id AS value,
p.name AS eventtype,
st.name AS sportstype
FROM #__joomleague_eventtype AS p
LEFT JOIN #__joomleague_position_eventtype AS pe
ON pe.eventtype_id=p.id
LEFT JOIN #__joomleague_sports_type AS st ON st.id = p.sports_type_id
WHERE pe.position_id='.(int) $this->_id.'
ORDER BY pe.ordering ASC ';
$this->_db->setQuery($query);
if (!$result=$this->_db->loadObjectList())
{
$this->setError($this->_db->getErrorMsg());
return false;
}
foreach ($result as $event){
$event->text=JText::_($event->eventtype)." (".JText::_($event->sportstype).")";
}
return $result;
}
/**
* Method to return the position stats array (value,text)
*
* @access public
* @return array
*/
function getPositionStatsOptions()
{
$query=' SELECT s.id AS value,
s.name AS statsname,
st.name AS sportstype
FROM #__joomleague_statistic AS s
INNER JOIN #__joomleague_position_statistic AS ps ON ps.statistic_id=s.id
LEFT JOIN #__joomleague_sports_type AS st ON st.id = s.sports_type_id
WHERE ps.position_id='.(int) $this->_id . '
ORDER BY ps.ordering ASC ';
$this->_db->setQuery($query);
if (!$result=$this->_db->loadObjectList())
{
$this->setError($this->_db->getErrorMsg());
return false;
}
foreach ($result as $stat){
$stat->text=JText::_($stat->statsname)." (".JText::_($stat->sportstype).")";
}
return $result;
}
/**
* Method to return the stats not yet assigned to position (value,text)
*
* @access public
* @return array
*/
function getAvailablePositionStatsOptions()
{
$query=' SELECT s.id AS value,
s.name AS statsname,
st.name AS sportstype
FROM #__joomleague_statistic AS s
LEFT JOIN #__joomleague_position_statistic AS ps ON ps.statistic_id = s.id
AND ps.position_id='.(int) $this->_id . '
LEFT JOIN #__joomleague_sports_type AS st ON st.id = s.sports_type_id
WHERE ps.id IS NULL
ORDER BY s.ordering ASC ';
$this->_db->setQuery($query);
if (!$result=$this->_db->loadObjectList())
{
$this->setError($this->_db->getErrorMsg());
return false;
}
foreach ($result as $stat){
$stat->text=JText::_($stat->statsname)." (".JText::_($stat->sportstype).")";
}
return $result;
}
/**
* Method to return the positions array (id,name)
*
* @access public
* @return array
*/
function getParentsPositions()
{
$option = JRequest::getCmd('option');
$mainframe = JFactory::getApplication();
$project_id=$mainframe->getUserState($option.'project');
//get positionss already in project for parents list
//support only 2 sublevel, so parent must not have parents themselves
$query=' SELECT pos.id AS value,
pos.name AS text
FROM #__joomleague_position AS pos
WHERE pos.parent_id=0
ORDER BY pos.ordering ASC ';
$this->_db->setQuery($query);
if (!$result=$this->_db->loadObjectList())
{
$this->setError($this->_db->getErrorMsg());
return false;
}
foreach ($result as $position){$position->text=JText::_($position->text);}
return $result;
}
/**
* Method to update checked persons
*
* @access public
* @return boolean True on success
*/
function storeshort($cid,$post)
{
$result=true;
for ($x=0; $x < count($cid); $x++)
{
$query=" UPDATE #__joomleague_position
SET parent_id='" . $post['parent_id' . $cid[$x]]."',
checked_out= 0,
checked_out_time = 0
WHERE id=".$cid[$x];
$this->_db->setQuery($query);
if(!$this->_db->execute())
{
$this->setError($this->_db->getErrorMsg());
$result=false;
}
}
return $result;
}
/**
* Method to export one or more positions
*
* @access public
* @return boolean True on success
*/
function export($cid=array(),$table,$record_name)
{
$result=false;
if (count($cid))
{
JArrayHelper::toInteger($cid);
$cids=implode(',',$cid);
$query="SELECT * FROM #__joomleague_position WHERE id IN ($cids)";
$this->_db->setQuery($query);
$exportData=$this->_db->loadObjectList();
$SportsTypeArray=array();
$x=0;
foreach ($exportData as $position){$SportsTypeArray[$x]=$position->sports_type_id;}
$st_cids=implode(',',$SportsTypeArray);
$query="SELECT * FROM #__joomleague_sports_type WHERE id IN ($st_cids)";
//echo $query;
$this->_db->setQuery($query);
$exportDataSportsType=$this->_db->loadObjectList();
$output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
// open the positions
$output .= "<positions>\n";
$record_name='SportsType';
//$tabVar=' ';
$tabVar=' ';
foreach ($exportDataSportsType as $name=>$value)
{
$output .= "<record object=\"".JoomleagueHelper::stripInvalidXml($record_name)."\">\n";
foreach ($value as $name2=>$value2)
{
if (($name2!='checked_out') && ($name2!='checked_out_time'))
{
$output .= $tabVar.'<'.$name2.'><![CDATA['.JoomleagueHelper::stripInvalidXml(trim($value2)).']]></'.$name2.">\n";
}
}
$output .= "</record>\n";
}
unset($name,$value);
$record_name_position='Position';
$record_name_parent_position='ParentPosition';
foreach ($exportData as $name => $value)
{
if ($value->parent_id==0)
{
$output .= "<record object=\"".JoomleagueHelper::stripInvalidXml($record_name_parent_position)."\">\n";
}
else
{
$output .= "<record object=\"".JoomleagueHelper::stripInvalidXml($record_name_position)."\">\n";
}
foreach ($value as $name2=>$value2)
{
if (($name2!='checked_out') && ($name2!='checked_out_time'))
{
$output .= $tabVar.'<'.$name2.'><![CDATA['.JoomleagueHelper::stripInvalidXml(trim($value2)).']]></'.$name2.">\n";
//echo "<pre>".$name2."#".$value2."<br /></pre>";
}
}
$output .= "</record>\n";
}
unset($name,$value);
// close positions
$output .= '</positions>';
$mdlJLXExports = JModelLegacy::getInstance("jlxmlexports", 'JoomleagueModel');
$mdlJLXExports->downloadXml($output, $table);
// close the application
$app = JFactory::getApplication();
$app->close();
}
return true;
}
/**
* Returns a Table object, always creating it
*
* @param type The table type to instantiate
* @param string A prefix for the table class name. Optional.
* @param array Configuration array for model. Optional.
* @return JTable A database object
*/
public function getTable($type = 'position', $prefix = 'table', $config = array())
{
return JTable::getInstance($type, $prefix, $config);
}
/**
* Method to get the record form.
*
* @param array $data Data for the form.
* @param boolean $loadData True if the form is to load its own data (default case), false if not.
* @return mixed A JForm object on success, false on failure
*/
public function getForm($data = array(), $loadData = true)
{
// Get the form.
$form = $this->loadForm('com_joomleague.'.$this->name, $this->name,
array('load_data' => $loadData) );
if (empty($form))
{
return false;
}
return $form;
}
/**
* Method to get the data that should be injected in the form.
*
* @return mixed The data for the form.
*/
protected function loadFormData()
{
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState('com_joomleague.edit.'.$this->name.'.data', array());
if (empty($data))
{
$data = $this->getData();
}
return $data;
}
}