-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
ContactModel.php
458 lines (393 loc) · 12.9 KB
/
ContactModel.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
<?php
/**
* @package Joomla.Site
* @subpackage com_contact
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Contact\Site\Model;
defined('_JEXEC') or die;
use Joomla\CMS\Application\SiteApplication;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Helper\TagsHelper;
use Joomla\CMS\Language\Multilanguage;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Model\FormModel;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\Database\ParameterType;
use Joomla\Database\QueryInterface;
use Joomla\Registry\Registry;
/**
* Single item model for a contact
*
* @package Joomla.Site
* @subpackage com_contact
* @since 1.5
*/
class ContactModel extends FormModel
{
/**
* The name of the view for a single item
*
* @since 1.6
*/
protected $view_item = 'contact';
/**
* A loaded item
*
* @since 1.6
*/
protected $_item = null;
/**
* Model context string.
*
* @var string
*/
protected $_context = 'com_contact.contact';
/**
* Method to auto-populate the model state.
*
* Note. Calling getState in this method will result in recursion.
*
* @return void
*
* @since 1.6
*/
protected function populateState()
{
/** @var SiteApplication $app */
$app = Factory::getApplication();
$this->setState('contact.id', $app->input->getInt('id'));
$this->setState('params', $app->getParams());
$user = Factory::getUser();
if ((!$user->authorise('core.edit.state', 'com_contact')) && (!$user->authorise('core.edit', 'com_contact')))
{
$this->setState('filter.published', 1);
$this->setState('filter.archived', 2);
}
}
/**
* Method to get the contact form.
* The base form is loaded from XML and then an event is fired
*
* @param array $data An optional array of data for the form to interrogate.
* @param boolean $loadData True if the form is to load its own data (default case), false if not.
*
* @return Form A Form object on success, false on failure
*
* @since 1.6
*/
public function getForm($data = array(), $loadData = true)
{
$form = $this->loadForm('com_contact.contact', 'contact', array('control' => 'jform', 'load_data' => true));
if (empty($form))
{
return false;
}
$temp = clone $this->getState('params');
$contact = $this->_item[$this->getState('contact.id')];
$active = Factory::getApplication()->getMenu()->getActive();
if ($active)
{
// If the current view is the active item and a contact view for this contact, then the menu item params take priority
if (strpos($active->link, 'view=contact') && strpos($active->link, '&id=' . (int) $contact->id))
{
// $contact->params are the contact params, $temp are the menu item params
// Merge so that the menu item params take priority
$contact->params->merge($temp);
}
else
{
// Current view is not a single contact, so the contact params take priority here
// Merge the menu item params with the contact params so that the contact params take priority
$temp->merge($contact->params);
$contact->params = $temp;
}
}
else
{
// Merge so that contact params take priority
$temp->merge($contact->params);
$contact->params = $temp;
}
if (!$contact->params->get('show_email_copy', 0))
{
$form->removeField('contact_email_copy');
}
return $form;
}
/**
* Method to get the data that should be injected in the form.
*
* @return array The default data is an empty array.
*
* @since 1.6.2
*/
protected function loadFormData()
{
$data = (array) Factory::getApplication()->getUserState('com_contact.contact.data', array());
if (empty($data['language']) && Multilanguage::isEnabled())
{
$data['language'] = Factory::getLanguage()->getTag();
}
// Add contact id to contact form data, so fields plugin can work properly
if (empty($data['catid']))
{
$data['catid'] = $this->getItem()->catid;
}
$this->preprocessData('com_contact.contact', $data);
return $data;
}
/**
* Gets a contact
*
* @param integer $pk Id for the contact
*
* @return mixed Object or null
*
* @since 1.6.0
*/
public function getItem($pk = null)
{
$pk = $pk ?: (int) $this->getState('contact.id');
if ($this->_item === null)
{
$this->_item = array();
}
if (!isset($this->_item[$pk]))
{
try
{
$db = $this->getDbo();
$query = $db->getQuery(true);
$query->select($this->getState('item.select', 'a.*'))
->select($this->getSlugColumn($query, 'a.id', 'a.alias') . ' AS slug')
->select($this->getSlugColumn($query, 'c.id', 'c.alias') . ' AS catslug')
->from($db->quoteName('#__contact_details', 'a'))
// Join on category table.
->select('c.title AS category_title, c.alias AS category_alias, c.access AS category_access')
->join('LEFT', $db->quoteName('#__categories', 'c'), 'c.id = a.catid')
// Join over the categories to get parent category titles
->select('parent.title AS parent_title, parent.id AS parent_id, parent.path AS parent_route, parent.alias AS parent_alias')
->join('LEFT', $db->quoteName('#__categories', 'parent'), 'parent.id = c.parent_id')
->where($db->quoteName('a.id') . ' = :id')
->bind(':id', $pk, ParameterType::INTEGER);
// Filter by start and end dates.
$nowDate = Factory::getDate()->toSql();
// Filter by published state.
$published = $this->getState('filter.published');
$archived = $this->getState('filter.archived');
if (is_numeric($published))
{
$queryString = 'a.published = :published';
if ($archived !== null)
{
$queryString = '(' . $queryString . ' OR a.published = :archived)';
$query->bind(':archived', $archived, ParameterType::INTEGER);
}
$query->where($queryString)
->where('(' . $query->isNullDatetime('a.publish_up') . ' OR a.publish_up <= :publish_up)')
->where('(' . $query->isNullDatetime('a.publish_down') . ' OR a.publish_down >= :publish_down)')
->bind(':published', $published, ParameterType::INTEGER)
->bind(':publish_up', $nowDate)
->bind(':publish_down', $nowDate);
}
$db->setQuery($query);
$data = $db->loadObject();
if (empty($data))
{
throw new \Exception(Text::_('COM_CONTACT_ERROR_CONTACT_NOT_FOUND'), 404);
}
// Check for published state if filter set.
if ((is_numeric($published) || is_numeric($archived)) && (($data->published != $published) && ($data->published != $archived)))
{
throw new \Exception(Text::_('COM_CONTACT_ERROR_CONTACT_NOT_FOUND'), 404);
}
/**
* In case some entity params have been set to "use global", those are
* represented as an empty string and must be "overridden" by merging
* the component and / or menu params here.
*/
$registry = new Registry($data->params);
$data->params = clone $this->getState('params');
$data->params->merge($registry);
$registry = new Registry($data->metadata);
$data->metadata = $registry;
// Some contexts may not use tags data at all, so we allow callers to disable loading tag data
if ($this->getState('load_tags', true))
{
$data->tags = new TagsHelper;
$data->tags->getItemTags('com_contact.contact', $data->id);
}
// Compute access permissions.
if (($access = $this->getState('filter.access')))
{
// If the access filter has been set, we already know this user can view.
$data->params->set('access-view', true);
}
else
{
// If no access filter is set, the layout takes some responsibility for display of limited information.
$user = Factory::getUser();
$groups = $user->getAuthorisedViewLevels();
if ($data->catid == 0 || $data->category_access === null)
{
$data->params->set('access-view', in_array($data->access, $groups));
}
else
{
$data->params->set('access-view', in_array($data->access, $groups) && in_array($data->category_access, $groups));
}
}
$this->_item[$pk] = $data;
}
catch (\Exception $e)
{
$this->setError($e);
$this->_item[$pk] = false;
}
}
if ($this->_item[$pk])
{
$this->buildContactExtendedData($this->_item[$pk]);
}
return $this->_item[$pk];
}
/**
* Load extended data (profile, articles) for a contact
*
* @param object $contact The contact object
*
* @return void
*/
protected function buildContactExtendedData($contact)
{
$db = $this->getDbo();
$nullDate = $db->quote($db->getNullDate());
$nowDate = $db->quote(Factory::getDate()->toSql());
$user = Factory::getUser();
$groups = $user->getAuthorisedViewLevels();
$published = $this->getState('filter.published');
$query = $db->getQuery(true);
// If we are showing a contact list, then the contact parameters take priority
// So merge the contact parameters with the merged parameters
if ($this->getState('params')->get('show_contact_list'))
{
$this->getState('params')->merge($contact->params);
}
// Get the com_content articles by the linked user
if ((int) $contact->user_id && $this->getState('params')->get('show_articles'))
{
$query->select('a.id')
->select('a.title')
->select('a.state')
->select('a.access')
->select('a.catid')
->select('a.created')
->select('a.language')
->select('a.publish_up')
->select($this->getSlugColumn($query, 'a.id', 'a.alias') . ' AS slug')
->select($this->getSlugColumn($query, 'c.id', 'c.alias') . ' AS catslug')
->from($db->quoteName('#__content', 'a'))
->leftJoin($db->quoteName('#__categories', 'c') . ' ON a.catid = c.id')
->where($db->quoteName('a.created_by') . ' = :created_by')
->whereIn($db->quoteName('a.access'), $groups)
->bind(':created_by', $contact->user_id, ParameterType::INTEGER)
->order('a.publish_up DESC');
// Filter per language if plugin published
if (Multilanguage::isEnabled())
{
$language = [Factory::getLanguage()->getTag(), $db->quote('*')];
$query->whereIn($db->quoteName('a.language'), $language);
}
if (is_numeric($published))
{
$query->where('a.state IN (1,2)')
->where('(' . $db->quoteName('a.publish_up') . ' = :null' .
' OR ' . $db->quoteName('a.publish_up') . ' <= :now' . ')')
->where('(' . $db->quoteName('a.publish_down') . ' = :null' .
' OR ' . $db->quoteName('a.publish_down') . ' >= :now' . ')')
->bind(':null', $nullDate)
->bind(':now', $nowDate);
}
// Number of articles to display from config/menu params
$articles_display_num = $this->getState('params')->get('articles_display_num', 10);
// Use contact setting?
if ($articles_display_num === 'use_contact')
{
$articles_display_num = $contact->params->get('articles_display_num', 10);
// Use global?
if ((string) $articles_display_num === '')
{
$articles_display_num = ComponentHelper::getParams('com_contact')->get('articles_display_num', 10);
}
}
$db->setQuery($query, 0, (int) $articles_display_num);
$contact->articles = $db->loadObjectList();
}
else
{
$contact->articles = null;
}
// Get the profile information for the linked user
$userModel = $this->bootComponent('com_users')->getMVCFactory()
->createModel('User', 'Administrator', ['ignore_request' => true]);
$data = $userModel->getItem((int) $contact->user_id);
PluginHelper::importPlugin('user');
// Get the form.
Form::addFormPath(JPATH_SITE . '/components/com_users/forms');
$form = Form::getInstance('com_users.profile', 'profile');
// Trigger the form preparation event.
Factory::getApplication()->triggerEvent('onContentPrepareForm', array($form, $data));
// Trigger the data preparation event.
Factory::getApplication()->triggerEvent('onContentPrepareData', array('com_users.profile', $data));
// Load the data into the form after the plugins have operated.
$form->bind($data);
$contact->profile = $form;
}
/**
* Generate column expression for slug or catslug.
*
* @param QueryInterface $query Current query instance.
* @param string $id Column id name.
* @param string $alias Column alias name.
*
* @return string
*
* @since 4.0.0
*/
private function getSlugColumn($query, $id, $alias)
{
return 'CASE WHEN '
. $query->charLength($alias, '!=', '0')
. ' THEN '
. $query->concatenate(array($query->castAsChar($id), $alias), ':')
. ' ELSE '
. $id . ' END';
}
/**
* Increment the hit counter for the contact.
*
* @param integer $pk Optional primary key of the contact to increment.
*
* @return boolean True if successful; false otherwise and internal error set.
*
* @since 3.0
*/
public function hit($pk = 0)
{
$input = Factory::getApplication()->input;
$hitcount = $input->getInt('hitcount', 1);
if ($hitcount)
{
$pk = $pk ?: (int) $this->getState('contact.id');
$table = $this->getTable('Contact');
$table->load($pk);
$table->hit($pk);
}
return true;
}
}