index.php ➔ get_cat_content()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 3
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 33 and the first side effect is on line 104.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 *
5
 * Module: SmartPartner
6
 * Author: The SmartFactory <www.smartfactory.ca>
7
 * Licence: GNU
8
 */
9
10
/**
11
 *Ceci nous produira un tableau de forme:
12
 *
13
 *PartnersArray[] =
14
 *     PartnersArray[TopCat1][info] = (nom, description)
15
 *     PartnersArray[TopCat1][partners] = array de partner (via fct get_partners_array())
16
 *      PartnersArray[TopCat1][subcats][0][info] = (nom, description)
17
 *      PartnersArray[TopCat1][subcats][0][partners] = array de partner
18
 *      PartnersArray[TopCat1][subcats][0][subcats]....
19
 *    Ainsi de suite
20
 *
21
 *ex: PartnersArray[TopCat1][partners][0][nom] contiendra le nom du 1er partenaire de TopCat1
22
 *
23
 */
24
25
/**
26
 *Loop inside the array of all partners to match with current category
27
 *
28
 *param $categoryid - id of the current category
29
 *return array of partners for the current category
30
 * @param $categoryid
31
 * @return array
32
 */
33
function get_partners_array($categoryid)
34
{
35
    global $every_partners_array, $count, $xoopsModuleConfig, $view_category_id;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
36
    $partners = array();
37
    foreach ($every_partners_array as $partnerObj) {
38
        if (in_array($categoryid, explode('|', $partnerObj->categoryid())) && ($view_category_id || (!$view_category_id && count($partners) < $xoopsModuleConfig['percat_user']))) {
39
            $partner    = $partnerObj->toArray('index');
40
            $partners[] = $partner;
41
        }
42
    }
43
44
    return $partners;
45
}
46
47
/**
48
 *Loop inside the array of all categories to find subcats for current category
49
 *recusrive function: for each subcat found, call to function getcontent to
50
 *get partners and subcats within it
51
 *
52
 *param $categoryid - id of the current category
53
 *return array of subcats for the current category
54
 * @param $every_categories_array
55
 * @param $categoryid
56
 * @param $level
57
 * @return array
58
 */
59
function get_subcats($every_categories_array, $categoryid, $level)
60
{
61
62
    //global $every_categories_array;
63
    $subcatArray = array();
64
    ++$level;
65
66
    foreach ($every_categories_array as $subcatObj) {
67
        if ($subcatObj->parentid() == $categoryid) {
68
            $subcatArray[] = get_cat_content($every_categories_array, $subcatObj, $level);
69
        }
70
    }
71
72
    return $subcatArray;
73
}
74
75
/**
76
 *Retrieve content for the current category
77
 *
78
 *param $categoryid - id of the current category
79
 *return array of content for the current category
80
 * @param $every_categories_array
81
 * @param $categoryObj
82
 * @param $level
83
 * @return array
84
 */
85
function get_cat_content($every_categories_array, $categoryObj, $level)
86
{
87
    $category = array();
88
    $decalage = '';
89
    /*for ($i=0;$i<$level;++$i) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
90
         $decalage .= '--';
91
     }*/
92
    $decalage .= ' ';
93
    $category['title']       = $decalage . '' . $categoryObj->name();
94
    $category['categoryid']  = $categoryObj->categoryid();
95
    $category['description'] = $categoryObj->description();
96
    $category['link_view']   = $categoryObj->getCategoryUrl();
97
    $category['partners']    = get_partners_array($categoryObj->categoryid());
98
    $category['image_url']   = $categoryObj->getImageUrl(true);
99
    $category['subcats']     = get_subcats($every_categories_array, $categoryObj->categoryid(), $level);
100
101
    return $category;
102
}
103
104
include __DIR__ . '/header.php';
105
$xoopsOption['template_main'] = 'smartpartner_index.tpl';
106
include XOOPS_ROOT_PATH . '/header.php';
107
include __DIR__ . '/footer.php';
108
109
// At which record shall we start
110
$start = isset($_GET['start']) ? (int)$_GET['start'] : 0;
111
112
$view_category_id = isset($_GET['view_category_id']) ? (int)$_GET['view_category_id'] : 0;
113
114
$partners_total = $smartPartnerPartnerHandler->getPartnerCount();
115
116
if ($xoopsModuleConfig['index_sortby'] === 'title' || $xoopsModuleConfig['index_sortby'] === 'weight') {
117
    $order = 'ASC';
118
} else {
119
    $order = 'DESC';
120
}
121
//Retreive all records from database
122
$every_categories_array = $smartPartnerCategoryHandler->getCategories(0, 0, -1, 'weight', 'ASC', true);
123
$every_partners_array   = $smartPartnerPartnerHandler->getPartnersForIndex(-1, _SPARTNER_STATUS_ACTIVE, $xoopsModuleConfig['index_sortby'], $order);
124
125
$partnersArray = array();
126
127
//display All categories and partners
128
if (!$view_category_id) {
129
    //get orphan first if preference says so
130
    if ($xoopsModuleConfig['orphan_first']) {
131
        $partnersArray['orphan']['partners'] = get_partners_array(0);
132
    }
133
134
    //get all categories and content
135
    foreach ($every_categories_array as $categoryObj) {
136
        if ($categoryObj->parentid() == 0) {
137
            $partnersArray[] = get_cat_content($every_categories_array, $categoryObj, 0);
138
        }
139
    }
140
141
    //get orphan last if preference says so
142
    if (!$xoopsModuleConfig['orphan_first']) {
143
        $partnersArray['orphan']['partners'] = get_partners_array(0);
144
    }
145
146
    $categoryPath = '';
147
} //viewing a specific category
148
else {
149
    $currentCategoryObj = $every_categories_array[$view_category_id];
150
    $partnersArray[]    = get_cat_content($every_categories_array, $currentCategoryObj, 0);
151
152
    if (!$partnersArray[0]['partners'] && !$partnersArray[0]['subcats']) {
153
        redirect_header(SMARTPARTNER_URL, 3, _MD_SPARTNER_CATEGORY_EMPTY);
154
    }
155
    // Retreiving the category path
156
    $categoryPath = $currentCategoryObj->getCategoryPath();
157
}
158
159
//$partners_total_onpage = $count;.partners
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
160
$xoopsTpl->assign('partners', $partnersArray);
161
162
//end new code to implement categories
163
164
// Partners Navigation Bar
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
165
//$pagenav = new XoopsPageNav($partners_total_onpage, $xoopsModuleConfig['perpage_user'], $start, 'start', '');
166
//$xoopsTpl->assign('pagenav', '<div style="text-align:right;">' . $pagenav->renderNav() . '</div>');
167
$xoopsTpl->assign('view_deteils_cat', _MD_SPARTNER_DETAIL_CAT);
168
$xoopsTpl->assign('on_index_page', $view_category_id == 0);
169
$xoopsTpl->assign('sitename', $xoopsConfig['sitename']);
170
$xoopsTpl->assign('displayjoin', $xoopsModuleConfig['allowsubmit'] && (is_object($xoopsUser) || $xoopsModuleConfig['anonpost']));
171
$xoopsTpl->assign('img_max_width', $xoopsModuleConfig['img_max_width']);
172
$xoopsTpl->assign('module_home', '<a href="' . SMARTPARTNER_URL . '">' . $smartPartnerModuleName . '</a>');
173
$xoopsTpl->assign('categoryPath', $categoryPath);
174
$xoopsTpl->assign('lang_intro_text', $myts->displayTarea($xoopsModuleConfig['welcomemsg']));
175
$xoopsTpl->assign('lang_partner', _MD_SPARTNER_PARTNER);
176
$xoopsTpl->assign('lang_desc', _MD_SPARTNER_DESCRIPTION);
177
$xoopsTpl->assign('lang_edit', _MD_SPARTNER_EDIT);
178
$xoopsTpl->assign('lang_delete', _MD_SPARTNER_DELETE);
179
$xoopsTpl->assign('lang_hits', _MD_SPARTNER_HITS);
180
$xoopsTpl->assign('lang_join', _MD_SPARTNER_JOIN);
181
$xoopsTpl->assign('lang_no_partners', _MD_SPARTNER_NOPART);
182
$xoopsTpl->assign('lang_main_partner', _MD_SPARTNER_PARTNERS);
183
$xoopsTpl->assign('lang_readmore', _MD_SPARTNER_READMORE);
184
$xoopsTpl->assign('partview_msg', $xoopsModuleConfig['partview_msg']);
185
if (!$xoopsModuleConfig['hide_module_name']) {
186
    $xoopsTpl->assign('lang_partnerstitle', $myts->displayTarea($xoopsModule->getVar('name')));
187
}
188
include_once XOOPS_ROOT_PATH . '/footer.php';
189