1<?php
2/**
3 * @package     Joomla.Administrator
4 * @subpackage  com_contact
5 *
6 * @copyright   Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
7 * @license     GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10defined('_JEXEC') or die;
11
12/**
13 * View to edit a contact.
14 *
15 * @since  1.6
16 */
17class ContactViewContact extends JViewLegacy
18{
19	/**
20	 * The JForm object
21	 *
22	 * @var  JForm
23	 */
24	protected $form;
25
26	/**
27	 * The active item
28	 *
29	 * @var  object
30	 */
31	protected $item;
32
33	/**
34	 * The model state
35	 *
36	 * @var  object
37	 */
38	protected $state;
39
40	/**
41	 * Display the view.
42	 *
43	 * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
44	 *
45	 * @return  mixed  A string if successful, otherwise an Error object.
46	 */
47	public function display($tpl = null)
48	{
49		// Initialise variables.
50		$this->form  = $this->get('Form');
51		$this->item  = $this->get('Item');
52		$this->state = $this->get('State');
53
54		// Check for errors.
55		if (count($errors = $this->get('Errors')))
56		{
57			throw new Exception(implode("\n", $errors), 500);
58		}
59
60		// If we are forcing a language in modal (used for associations).
61		if ($this->getLayout() === 'modal' && $forcedLanguage = JFactory::getApplication()->input->get('forcedLanguage', '', 'cmd'))
62		{
63			// Set the language field to the forcedLanguage and disable changing it.
64			$this->form->setValue('language', null, $forcedLanguage);
65			$this->form->setFieldAttribute('language', 'readonly', 'true');
66
67			// Only allow to select categories with All language or with the forced language.
68			$this->form->setFieldAttribute('catid', 'language', '*,' . $forcedLanguage);
69
70			// Only allow to select tags with All language or with the forced language.
71			$this->form->setFieldAttribute('tags', 'language', '*,' . $forcedLanguage);
72		}
73
74		$this->addToolbar();
75
76		return parent::display($tpl);
77	}
78
79	/**
80	 * Add the page title and toolbar.
81	 *
82	 * @return  void
83	 *
84	 * @since   1.6
85	 */
86	protected function addToolbar()
87	{
88		JFactory::getApplication()->input->set('hidemainmenu', true);
89
90		$user       = JFactory::getUser();
91		$userId     = $user->id;
92		$isNew      = ($this->item->id == 0);
93		$checkedOut = !($this->item->checked_out == 0 || $this->item->checked_out == $userId);
94
95		// Since we don't track these assets at the item level, use the category id.
96		$canDo = JHelperContent::getActions('com_contact', 'category', $this->item->catid);
97
98		JToolbarHelper::title($isNew ? JText::_('COM_CONTACT_MANAGER_CONTACT_NEW') : JText::_('COM_CONTACT_MANAGER_CONTACT_EDIT'), 'address contact');
99
100		// Build the actions for new and existing records.
101		if ($isNew)
102		{
103			// For new records, check the create permission.
104			if ($isNew && (count($user->getAuthorisedCategories('com_contact', 'core.create')) > 0))
105			{
106				JToolbarHelper::apply('contact.apply');
107				JToolbarHelper::save('contact.save');
108				JToolbarHelper::save2new('contact.save2new');
109			}
110
111			JToolbarHelper::cancel('contact.cancel');
112		}
113		else
114		{
115			// Since it's an existing record, check the edit permission, or fall back to edit own if the owner.
116			$itemEditable = $canDo->get('core.edit') || ($canDo->get('core.edit.own') && $this->item->created_by == $userId);
117
118			// Can't save the record if it's checked out and editable
119			if (!$checkedOut && $itemEditable)
120			{
121				JToolbarHelper::apply('contact.apply');
122				JToolbarHelper::save('contact.save');
123
124				// We can save this record, but check the create permission to see if we can return to make a new one.
125				if ($canDo->get('core.create'))
126				{
127					JToolbarHelper::save2new('contact.save2new');
128				}
129			}
130
131			// If checked out, we can still save
132			if ($canDo->get('core.create'))
133			{
134				JToolbarHelper::save2copy('contact.save2copy');
135			}
136
137			if (JComponentHelper::isEnabled('com_contenthistory') && $this->state->params->get('save_history', 0) && $itemEditable)
138			{
139				JToolbarHelper::versions('com_contact.contact', $this->item->id);
140			}
141
142			if (JLanguageAssociations::isEnabled() && JComponentHelper::isEnabled('com_associations'))
143			{
144				JToolbarHelper::custom('contact.editAssociations', 'contract', 'contract', 'JTOOLBAR_ASSOCIATIONS', false, false);
145			}
146
147			JToolbarHelper::cancel('contact.cancel', 'JTOOLBAR_CLOSE');
148		}
149
150		JToolbarHelper::divider();
151		JToolbarHelper::help('JHELP_COMPONENTS_CONTACTS_CONTACTS_EDIT');
152	}
153}
154