associations.php
4.07 KB
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
<?php
// namespace administrator\components\com_jmap\framework\helpers;
/**
* @package JMAP::FRAMEWORK::administrator::components::com_jmap
* @subpackage framework
* @subpackage helpers
* @author Joomla! Extensions Store
* @copyright (C) 2015 - Joomla! Extensions Store
* @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html
*/
defined('_JEXEC') or die('Restricted access');
/**
* Generic static helper class
*
* @package JMAP::FRAMEWORK::administrator::components::com_jmap
* @subpackage framework
* @subpackage helpers
* @since 3.5
*/
class JMapHelpersAssociations {
/**
* Get the items associations
*
* @param integer $pk Menu item id
* @usage JMapHelpersAssociations::getMenuAssociations($itemid)
*
* @return array
*/
public static function getMenuAssociations($pk) {
$associations = array();
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select('m2.language, m2.id')
->select($db->quoteName('lg.sef'))
->from('#__menu as m')
->join('INNER', '#__associations as a ON a.id=m.id AND a.context=' . $db->quote('com_menus.item'))
->join('INNER', '#__associations as a2 ON a.key=a2.key')
->join('INNER', '#__menu as m2 ON a2.id=m2.id')
->join('INNER', $db->quoteName('#__languages', 'lg') . ' ON m2.language = lg.' . $db->quoteName('lang_code'))
->where('m.id=' . (int) $pk)
->where('m.type=' . $db->quote('component'));
$db->setQuery($query);
try {
$menuitems = $db->loadObjectList ( 'sef' );
} catch ( RuntimeException $e ) {
return $associations;
}
foreach ( $menuitems as $tag => $item ) {
$associations [$tag] = $item;
}
return $associations;
}
/**
* Get the associations.
*
* @param string $extension The name of the component.
* @param string $tablename The name of the table.
* @param string $context The context
* @param integer $id The primary key value.
* @param string $pk The name of the primary key in the given $table.
* @param string $aliasField If the table has an alias field set it here. Null to not use it
* @param string $catField If the table has a catid field set it here. Null to not use it
* @usage JMapHelpersAssociations::getAssociations('com_content', '#__content', 'com_content.item', $articleid)
*
* @return array The associated items
*/
public static function getContentAssociations($extension, $tablename, $context, $id, $pk = 'id', $aliasField = 'alias', $catField = 'catid') {
$associations = array();
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('c2.language'))
->select($db->quoteName('lg.sef'))
->from($db->quoteName($tablename, 'c'))
->join('INNER', $db->quoteName('#__associations', 'a') . ' ON a.id = c.' . $db->quoteName($pk) . ' AND a.context=' . $db->quote($context))
->join('INNER', $db->quoteName('#__associations', 'a2') . ' ON a.key = a2.key')
->join('INNER', $db->quoteName($tablename, 'c2') . ' ON a2.id = c2.' . $db->quoteName($pk))
->join('INNER', $db->quoteName('#__languages', 'lg') . ' ON c2.language = lg.' . $db->quoteName('lang_code'));
// Use alias field ?
if (! empty ( $aliasField )) {
$query->select ( $query->concatenate ( array (
$db->quoteName ( 'c2.' . $pk ),
$db->quoteName ( 'c2.' . $aliasField )
), ':' ) . ' AS ' . $db->quoteName ( $pk ) );
} else {
$query->select ( $db->quoteName ( 'c2.' . $pk ) );
}
// Use catid field ?
if (!empty($catField)) {
$query->join(
'INNER',
$db->quoteName('#__categories', 'ca') . ' ON ' . $db->quoteName('c2.' . $catField) . ' = ca.id AND ca.extension = ' . $db->quote($extension)
)
->select(
$query->concatenate(
array('ca.id', 'ca.alias'),
':'
) . ' AS ' . $db->quoteName($catField)
);
}
$query->where('c.' . $pk . ' = ' . (int) $id);
$db->setQuery($query);
try {
$items = $db->loadObjectList('sef');
}
catch (RuntimeException $e) {
return $associations;
}
if ($items) {
foreach ($items as $tag => $item) {
$associations[$tag] = $item;
}
}
return $associations;
}
}