Commit 21dab95f by pupi1985

Small refactoring of the qa_set_up_category_field function

parent 04bd3cde
...@@ -1454,82 +1454,86 @@ ...@@ -1454,82 +1454,86 @@
(or null for no maximum) and $excludecategoryid to a category that should not be included. (or null for no maximum) and $excludecategoryid to a category that should not be included.
*/ */
{ {
$pathcategories=qa_category_path($navcategories, $categoryid); $pathcategories = qa_category_path($navcategories, $categoryid);
$startpath=''; $startpath = '';
foreach ($pathcategories as $category) foreach ($pathcategories as $category)
$startpath.='/'.$category['categoryid']; $startpath .= '/' . $category['categoryid'];
if (!isset($maxdepth))
$maxdepth=QA_CATEGORY_DEPTH;
$maxdepth=min(QA_CATEGORY_DEPTH, $maxdepth);
$qa_content['script_rel'][]='qa-content/qa-ask.js?'.QA_VERSION;
$qa_content['script_onloads'][]='qa_category_select('.qa_js($fieldname).', '.qa_js($startpath).');';
$qa_content['script_var']['qa_cat_exclude']=$excludecategoryid; if (isset($maxdepth))
$qa_content['script_var']['qa_cat_allownone']=(int)$allownone; $maxdepth = min(QA_CATEGORY_DEPTH, $maxdepth);
$qa_content['script_var']['qa_cat_allownosub']=(int)$allownosub; else
$qa_content['script_var']['qa_cat_maxdepth']=$maxdepth; $maxdepth = QA_CATEGORY_DEPTH;
$field['type']='select'; $qa_content['script_rel'][] = 'qa-content/qa-ask.js?' . QA_VERSION;
$field['tags']='name="'.$fieldname.'_0" id="'.$fieldname.'_0" onchange="qa_category_select('.qa_js($fieldname).');"'; $qa_content['script_onloads'][] = sprintf('qa_category_select(%s, %s);', qa_js($fieldname), qa_js($startpath));
$field['options']=array();
$qa_content['script_var'] = array(
'qa_cat_exclude' => $excludecategoryid,
'qa_cat_allownone' => (int) $allownone,
'qa_cat_allownosub' => (int) $allownosub,
'qa_cat_maxdepth' => $maxdepth,
);
$field['type'] = 'select';
$field['tags'] = sprintf('name="%s_0" id="%s_0" onchange="qa_category_select(%s);"', $fieldname, $fieldname, qa_js($fieldname));
$field['options'] = array();
// create the menu that will be shown if Javascript is disabled // create the menu that will be shown if Javascript is disabled
if ($allownone) if ($allownone)
$field['options']['']=qa_lang_html('main/no_category'); // this is also copied to first menu created by Javascript $field['options'][''] = qa_lang_html('main/no_category'); // this is also copied to first menu created by Javascript
$keycategoryids=array(); $keycategoryids = array();
if ($allownosub) { if ($allownosub) {
$category=@$navcategories[$categoryid]; $category = @$navcategories[$categoryid];
$upcategory=$category;
while (true) { // first get supercategories
$upcategory=@$navcategories[$upcategory['parentid']];
if (!isset($upcategory)) $upcategory = @$navcategories[$category['parentid']]; // first get supercategories
break; while (isset($upcategory)) {
$keycategoryids[$upcategory['categoryid']] = true;
$keycategoryids[$upcategory['categoryid']]=true; $upcategory = @$navcategories[$upcategory['parentid']];
} }
$keycategoryids=array_reverse($keycategoryids, true); $keycategoryids = array_reverse($keycategoryids, true);
$depth=count($keycategoryids); // number of levels above $depth = count($keycategoryids); // number of levels above
if (isset($category)) { if (isset($category)) {
$depth++; // to count category itself $depth++; // to count category itself
foreach ($navcategories as $navcategory) // now get siblings and self foreach ($navcategories as $navcategory) // now get siblings and self
if (!strcmp($navcategory['parentid'], $category['parentid'])) if (!strcmp($navcategory['parentid'], $category['parentid']))
$keycategoryids[$navcategory['categoryid']]=true; $keycategoryids[$navcategory['categoryid']] = true;
} }
if ($depth<$maxdepth) if ($depth < $maxdepth)
foreach ($navcategories as $navcategory) // now get children, if not too deep foreach ($navcategories as $navcategory) // now get children, if not too deep
if (!strcmp($navcategory['parentid'], $categoryid)) if (!strcmp($navcategory['parentid'], $categoryid))
$keycategoryids[$navcategory['categoryid']]=true; $keycategoryids[$navcategory['categoryid']] = true;
} else { } else {
$haschildren=false; $haschildren = false;
foreach ($navcategories as $navcategory) // check if it has any children foreach ($navcategories as $navcategory) // check if it has any children
if (!strcmp($navcategory['parentid'], $categoryid)) if (!strcmp($navcategory['parentid'], $categoryid)) {
$haschildren=true; $haschildren = true;
break;
}
if (!$haschildren) if (!$haschildren)
$keycategoryids[$categoryid]=true; // show this category if it has no children $keycategoryids[$categoryid] = true; // show this category if it has no children
} }
foreach ($keycategoryids as $keycategoryid => $dummy) foreach ($keycategoryids as $keycategoryid => $dummy)
if (strcmp($keycategoryid, $excludecategoryid)) if (strcmp($keycategoryid, $excludecategoryid))
$field['options'][$keycategoryid]=qa_category_path_html($navcategories, $keycategoryid); $field['options'][$keycategoryid] = qa_category_path_html($navcategories, $keycategoryid);
$field['value']=@$field['options'][$categoryid]; $field['value'] = @$field['options'][$categoryid];
$field['note']='<div id="'.$fieldname.'_note"><noscript style="color:red;">'.qa_lang_html('question/category_js_note').'</noscript></div>'; $field['note'] =
'<div id="' . $fieldname . '_note">' .
'<noscript style="color:red;">' . qa_lang_html('question/category_js_note') . '</noscript>' .
'</div>';
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment