1 <?php
  2 /**
  3  * @package     Joomla.Platform
  4  * @subpackage  Association
  5  *
  6  * @copyright   Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
  7  * @license     GNU General Public License version 2 or later; see LICENSE
  8  */
  9 
 10 use Joomla\Utilities\ArrayHelper;
 11 
 12 defined('JPATH_PLATFORM') or die;
 13 
 14 /**
 15  * Association Extension Helper
 16  *
 17  * @since  3.7.0
 18  */
 19 abstract class JAssociationExtensionHelper  implements JAssociationExtensionInterface
 20 {
 21     /**
 22      * The extension name
 23      *
 24      * @var     array  $extension
 25      *
 26      * @since   3.7.0
 27      */
 28     protected $extension = 'com_??';
 29 
 30     /**
 31      * Array of item types
 32      *
 33      * @var     array  $itemTypes
 34      *
 35      * @since   3.7.0
 36      */
 37     protected $itemTypes = array();
 38 
 39     /**
 40      * Has the extension association support
 41      *
 42      * @var     boolean  $associationsSupport
 43      *
 44      * @since   3.7.0
 45      */
 46     protected $associationsSupport = false;
 47 
 48     /**
 49      * Checks if the extension supports associations
 50      *
 51      * @return  boolean  Supports the extension associations
 52      *
 53      * @since   3.7.0
 54      */
 55     public function hasAssociationsSupport()
 56     {
 57         return $this->associationsSupport;
 58     }
 59 
 60     /**
 61      * Get the item types
 62      *
 63      * @return  array  Array of item types
 64      *
 65      * @since  3.7.0
 66      */
 67     public function getItemTypes()
 68     {
 69         return $this->itemTypes;
 70     }
 71 
 72     /**
 73      * Get the associated items for an item
 74      *
 75      * @param   string  $typeName  The item type
 76      * @param   int     $itemId    The id of item for which we need the associated items
 77      *
 78      * @return   array
 79      *
 80      * @since    3.7.0
 81      */
 82     public function getAssociationList($typeName, $itemId)
 83     {
 84         $items = array();
 85 
 86         $associations = $this->getAssociations($typeName, $itemId);
 87 
 88         foreach ($associations as $key => $association)
 89         {
 90             $items[$key] = ArrayHelper::fromObject($this->getItem($typeName, (int) $association->id), false);
 91         }
 92 
 93         return $items;
 94     }
 95 
 96     /**
 97      * Get information about the type
 98      *
 99      * @param   string  $typeName  The item type
100      *
101      * @return  array  Array of item types
102      *
103      * @since   3.7.0
104      */
105     public function getType($typeName = '')
106     {
107         $fields  = $this->getFieldsTemplate();
108         $tables  = array();
109         $joins   = array();
110         $support = $this->getSupportTemplate();
111         $title   = '';
112 
113         return array(
114             'fields'  => $fields,
115             'support' => $support,
116             'tables'  => $tables,
117             'joins'   => $joins,
118             'title'   => $title
119         );
120     }
121 
122     /**
123      * Get information about the fields the type provides
124      *
125      * @param   string  $typeName  The item type
126      *
127      * @return  array  Array of support information
128      *
129      * @since   3.7.0
130      */
131     public function getTypeFields($typeName)
132     {
133         return $this->getTypeInformation($typeName, 'fields');
134     }
135 
136     /**
137      * Get information about the fields the type provides
138      *
139      * @param   string  $typeName  The item type
140      *
141      * @return  array  Array of support information
142      *
143      * @since   3.7.0
144      */
145     public function getTypeSupport($typeName)
146     {
147         return $this->getTypeInformation($typeName, 'support');
148     }
149 
150     /**
151      * Get information about the tables the type use
152      *
153      * @param   string  $typeName  The item type
154      *
155      * @return  array  Array of support information
156      *
157      * @since   3.7.0
158      */
159     public function getTypeTables($typeName)
160     {
161         return $this->getTypeInformation($typeName, 'tables');
162     }
163 
164     /**
165      * Get information about the table joins for the type
166      *
167      * @param   string  $typeName  The item type
168      *
169      * @return  array  Array of support information
170      *
171      * @since   3.7.0
172      */
173     public function getTypeJoins($typeName)
174     {
175         return $this->getTypeInformation($typeName, 'joins');
176     }
177 
178     /**
179      * Get the type title
180      *
181      * @param   string  $typeName  The item type
182      *
183      * @return  array  Array of support information
184      *
185      * @since   3.7.0
186      */
187     public function getTypeTitle($typeName)
188     {
189         $type = $this->getType($typeName);
190 
191         if (!array_key_exists('title', $type))
192         {
193             return '';
194         }
195 
196         return $type['title'];
197     }
198 
199     /**
200      * Get information about the type
201      *
202      * @param   string  $typeName  The item type
203      * @param   string  $part      part of the information
204      *
205      * @return  array Array of support information
206      *
207      * @since   3.7.0
208      */
209     private function getTypeInformation($typeName, $part = 'support')
210     {
211         $type = $this->getType($typeName);
212 
213         if (!array_key_exists($part, $type))
214         {
215             return array();
216         }
217 
218         return $type[$part];
219     }
220 
221     /**
222      * Get a table field name for a type
223      *
224      * @param   string  $typeName   The item type
225      * @param   string  $fieldName  The item type
226      *
227      * @return  string
228      *
229      * @since   3.7.0
230      */
231     public function getTypeFieldName($typeName, $fieldName)
232     {
233         $fields = $this->getTypeFields($typeName);
234 
235         if (!array_key_exists($fieldName, $fields))
236         {
237             return '';
238         }
239 
240         $tmp = $fields[$fieldName];
241         $pos = strpos($tmp, '.');
242 
243         if ($pos === false)
244         {
245             return $tmp;
246         }
247 
248         return substr($tmp, $pos + 1);
249     }
250 
251     /**
252      * Get default values for support array
253      *
254      * @return  array
255      *
256      * @since   3.7.0
257      */
258     protected function getSupportTemplate()
259     {
260         return array(
261             'state'    => false,
262             'acl'      => false,
263             'checkout' => false
264         );
265     }
266 
267     /**
268      * Get default values for fields array
269      *
270      * @return  array
271      *
272      * @since   3.7.0
273      */
274     protected function getFieldsTemplate()
275     {
276         return array(
277             'id'                  => 'a.id',
278             'title'               => 'a.title',
279             'alias'               => 'a.alias',
280             'ordering'            => 'a.ordering',
281             'menutype'            => '',
282             'level'               => '',
283             'catid'               => 'a.catid',
284             'language'            => 'a.language',
285             'access'              => 'a.access',
286             'state'               => 'a.state',
287             'created_user_id'     => 'a.created_by',
288             'checked_out'         => 'a.checked_out',
289             'checked_out_time'    => 'a.checked_out_time'
290         );
291     }
292 }
293