Drupal - Dispaly block if term page has no tagged nodes and no child terms? - drupal-6

How can I show a block on term pages if no nodes are tagged with that term and also there are no child terms?
Im using Drupal 6.
Thanks

Tricky one but you could use custom PHP code for your block display, something like this (assuming Drupal 6 here):
if (strstr($_GET['q'], 'taxonomy/term/')) {
$parts = explode('/', $_GET['q']);
$term = taxonomy_get_term($parts(2));
if ($term && $term->tid) {
$node_count = db_result(db_query('SELECT COUNT(nid) FROM {term_node} WHERE tid = %d', $term->tid));
if ($node_count == 0) {
return FALSE;
}
if (count(taxonomy_get_children($term->tid)) == 0) {
return FALSE;
}
return TRUE;
}
}
return TRUE;
And for Drupal 7:
if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric($arg(2))) {
$term = taxonomy_term_load(arg(2));
if ($term && $term->tid) {
if (db_query('SELECT COUNT(nid) FROM {taxonomy_index} WHERE tid = :tid', array(':tid' => $term->tid))->fetchField() == 0) {
return FALSE;
}
if (count(taxonomy_get_children($term->tid)) == 0) {
return FALSE;
}
return TRUE;
}
}
return TRUE;

Related

Using RegisterHotKey with number pad values not working?

I have some constants:
constexpr int MonitorDisplay1 = 100;
constexpr int MonitorDisplay2 = 200;
constexpr int MonitorDisplay3 = 400;
constexpr int MonitorDisplay4 = 500;
constexpr int MonitorDisplay1KeyPad = 101;
constexpr int MonitorDisplay2KeyPad = 201;
constexpr int MonitorDisplay3KeyPad = 401;
constexpr int MonitorDisplay4KeyPad = 501;
I have an OnCreate where I setup the hot keys:
int CCenterCursorOnScreenDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (__super::OnCreate(lpCreateStruct) == -1)
return -1;
auto RegisterAppHotkey = [hAppWnd = GetSafeHwnd()](const int nHotKeyID, UINT vk)->bool
{
if (!::RegisterHotKey(hAppWnd, nHotKeyID, MOD_CONTROL | MOD_SHIFT, vk))
{
auto const ec = ::GetLastError();
auto const err_msg = std::format(L"RegisterHotKey failed (error: {})\n",
ec);
AfxMessageBox(err_msg.c_str());
return false;
}
return true;
};
if (m_monitors.rcMonitors.size() > 0)
{
if (!RegisterAppHotkey(MonitorDisplay1, '1'))
{
return -1;
}
if (!RegisterAppHotkey(MonitorDisplay1KeyPad, VK_NUMPAD1))
{
return -1;
}
}
if (m_monitors.rcMonitors.size() > 1)
{
if (!RegisterAppHotkey(MonitorDisplay2, '2'))
{
return -1;
}
if (!RegisterAppHotkey(MonitorDisplay2KeyPad, VK_NUMPAD2))
{
return -1;
}
}
if (m_monitors.rcMonitors.size() > 2)
{
if (!RegisterAppHotkey(MonitorDisplay3, '3'))
{
return -1;
}
if (!RegisterAppHotkey(MonitorDisplay3KeyPad, VK_NUMPAD3))
{
return -1;
}
}
if (m_monitors.rcMonitors.size() > 3)
{
if (!RegisterAppHotkey(MonitorDisplay4, '4'))
{
return -1;
}
if (!RegisterAppHotkey(MonitorDisplay4KeyPad, VK_NUMPAD4))
{
return -1;
}
}
return 0;
}
Previously I just had the 4 hotkeys for 1 / 2 / 3 and 4. They still work. I tried to add new hotkeys for for the number pad on the keyboard, but they are not working.
My OnHotKey handler:
void CCenterCursorOnScreenDlg::OnHotKey(UINT nHotKeyId, UINT nKey1, UINT nKey2)
{
if (nHotKeyId == MonitorDisplay1 || nHotKeyId == MonitorDisplay1KeyPad)
{
CenterCursorOnMonitor(0);
}
else if (nHotKeyId == MonitorDisplay2 || nHotKeyId == MonitorDisplay2KeyPad)
{
CenterCursorOnMonitor(1);
}
else if (nHotKeyId == MonitorDisplay3 || nHotKeyId == MonitorDisplay3KeyPad)
{
CenterCursorOnMonitor(2);
}
else if (nHotKeyId == MonitorDisplay4 || nHotKeyId == MonitorDisplay4KeyPad)
{
CenterCursorOnMonitor(3);
}
__super::OnHotKey(nHotKeyId, nKey1, nKey2);
}
But the number pad versions are not working. Why?
I am unregistering all 8 hotkeys, and num-lock is on. I get no warnings when registering.
This article explains List of Keys (Keyboard, Mouse and Joystick):
Because I was using CTRL + SHIFT, then the meaning of the 4 numeric keys was changing:
End
Down
Page Down
Left
It made sense to change my hotkeys to CTRL + ALT instead to avoid this issue.

Show only one partial view among many in ajax success method

Here is my controler action method......from that action i want to return a partial view which is conditionaly true....and show this partial view in ajax success method....
public ActionResul InsertPestType (PestTypeViewModel model)
{
if (ModelState.IsValid == true)
{
pest_type emp = new pest_type();
ViewBag.emp = db.pest_type.SingleOrDefault(x => x.pest_type_name
== model.pest_type_name && x.isdeleted == false);
if (ViewBag.emp != null)
{
emp = ViewBag.emp;
}
if (emp.pest_type_name == model.pest_type_name)
{
ViewBag.Message = "This Pest Type is already exist in the
record";
return PartialView("Insert_PestType", model);
}
else
{
emp.pest_type_name = model.pest_type_name;
emp.isdeleted = false;
db.pest_type.Add(emp);
db.SaveChanges();
List<pest_type> model1 = db.pest_type.Where(x => x.isdeleted
== false).ToList();
ViewBag.pesttypelist = model1;
return PartialView("pest_type_partial", model);
}
}
else
return PartialView("inex_partial", model);
}
Below is my ajax success method.......here i want to show only one partial view which is true ocording to the condition...........Now my question is that how can i differentiate between two partial views in ajax success method??
var success=function(response)
{
if ()
{
$("#RegisterPestTypeModel").modal('hide')
$("#dvCategoryResults").html(response.AjaxReturn)
}
else
{
$("#PestTypeModalBody").html(response)
}

The Intern: Polling Until Element is Visible

Is there a way in Intern that I can poll until an element is visible? A lot of elements in my website are in the dom but are hidden, so every time I do a "find for" element X after it is supposed to appear, it fails because the element clearly breaks one of the visible attributes that selenium checks.
I've tried the helper function "pollUntil" but I can't seem to get that to work. Dojo seems to not like document.getElement*()
Helper Function that is passed into pollUntil
//this is a helper function for pollUntil
//first we want to find an element by class name and text
var elementVisibleAndText = function(elems, innerText){
elems = document.getElementsByClassName(elems);
//if we can't even find it, we return null
//but if we do find it, we want to return a
//not null element
if (!elems || elems.length == 0){
return null;
}
//now let's look at all of the elements found by
//in elems, and see if the innerHTML matches. If it
//does then we want to return that it was found
var each;
for(each in elems){
if(elems[each].innerHTML == innerText)
return (elems[each].offsetWidth > 0 && elems[each].offsetHeight > 0) ? elems[each] : null;
}
//else return null if nothing is found in the elements
return null;
};
Check out https://theintern.github.io/leadfoot/pollUntil.html. Intern uses leadfoot - so you should have access to this functionality.
var Command = require('leadfoot/Command');
var pollUntil = require('leadfoot/helpers/pollUntil');
new Command(session)
.get('http://example.com')
.then(pollUntil('return document.getElementById("a");', 1000))
.then(function (elementA) {
// element was found
}, function (error) {
// element was not found
});
To use the function within one of your tests - you would import it using the following path:
'intern/dojo/node!leadfoot/helpers/pollUntil'
I run into this issue all the time and we used the pollUntil functionality of intern to write a few helper utilities. In our tests we use something like .then(pollUntil(util.element_visible_by_class(), ['toast_notification'], 22000))
In a separate util.js file we have
/**
* Our shared utility for unit testing
*/
define([
'intern!object',
'intern/chai!assert',
'require',
'intern/dojo/node!leadfoot/helpers/pollUntil'
], function (registerSuite, assert, require, util, pollUntil) {
return {
element_visible_by_class: function(elem) {
return function(elem) {
elem = document.getElementsByClassName(elem);
if (!elem || elem.length == 0) { return null; }
elem = elem[0];
return (elem.offsetWidth > 0 && elem.offsetHeight > 0) ? elem : null;
}
},
element_visible_by_id: function(elem) {
return function(elem) {
elem = document.getElementById(elem);
if (!elem) { return null; }
return (elem.offsetWidth > 0 && elem.offsetHeight > 0) ? elem : null;
}
},
element_hidden_by_id: function(elem) {
return function(elem) {
elem = document.getElementById(elem);
if (!elem) { return null; }
return (elem.offsetWidth > 0 && elem.offsetHeight > 0) ? null : elem;
}
},
element_hidden_by_class: function(elem) {
return function(elem) {
elem = document.getElementsByClassName(elem);
if (!elem || elem.length == 0) { return null; }
elem = elem[0];
return (elem.offsetWidth > 0 && elem.offsetHeight > 0) ? null : elem;
}
},
}
})

Optimising A* pathfinding, runs very slow. Possible bugs(?) visual c++

Hi I'm having a few problems with my A* pathfinding algorithm. The algorithm does successfully execute, however in a debug environment it executes in about 10 seconds, in release it will still take 2-3 seconds. This speed is way too slow. I suspect this is either due to a bug in the code, or the fact it isn't well optimised.
The map that pathfinding is being used on is a 30*30 grid, with each square being 10 unites away from one another.
I have noticed when running the algorithm, that when the open and closed list are searched to see if a node already exists, the node already stored in one of the lists always has a lower cost, so there is no updating of nodes. Not sure if this is normal or not. Also, I am not sure if quicksort is a good sort to be using in this situation.
Here is the code:
The coords struture used as a node:
struct coords
{
int x;
int z;
coords* parent;
int cost;
int score;
};
The sort compare function:
bool decompare(coords* o1, coords* o2)
{
return (o1->score < o2->score);
}
The main pathfind loop:
while (!goalFound) //While goal has not been found
{
current = openList.front(); //Retrieve current state from the open list
openList.pop_front();
for (int count = 1; count < 5; count++)
{
if (!goalFound)
{
coords* possibleState = new (coords); //Allocate new possible state
found = false;
if (count == 1)
{
possibleState->x = current->x;
possibleState->z = current->z + 10; //North
}
else if (count == 2)
{
possibleState->x = current->x + 10; //East
possibleState->z = current->z;
}
else if (count == 3)
{
possibleState->x = current->x; //South
possibleState->z = current->z - 10;
}
else if (count == 4)
{
possibleState->x = current->x - 10; //West
possibleState->z = current->z;
}
if (possibleState->x >-1 && possibleState->x <291 && possibleState->z >-1 && possibleState->z < 291) //If possible state is in game boundary
{
possibleState->cost = current->cost + 10; //Add 10 to current state to get cost of new possible state
int a = (possibleState->x / 10) + (30 * (possibleState->z / 10)); //get index of map
if (map[a] != wallTest) //Test possible state is not inside a wall
{
p = openList.begin();
while (p != openList.end() && !found) //Search open list to see if possible state already exists
{
if (possibleState->x == (*p)->x && possibleState->z == (*p)->z) //Already exists
{
found = true;
if (!possibleState->cost >= (*p)->cost) //Test possible state has lower cost
{
(*p)->parent = current; //Update existing with attributes of possible state
a = abs((*p)->x - goalState->x);
b = abs((*p)->z - goalState->z);
(*p)->cost = possibleState->cost;
(*p)->score = (possibleState->cost) + ((a)+(b));
}
}
else
{
found = false; //Set not found
}
p++;
}
q = closedList.begin();
while (q != closedList.end())
{
if (possibleState->x == (*q)->x && possibleState->z == (*q)->z)
{
found = true;
int a = (*q)->cost;
if (possibleState->cost < a) //Test if on closed list
{
(*q)->parent = current;
a = abs((*q)->x - goalState->x);
b = abs((*q)->z - goalState->z);
(*q)->cost = possibleState->cost;
(*q)->score = (possibleState->cost) + ((a)+(b)); //If cost lower push onto open list
coords* newcoord;
newcoord->x = (*q)->x;
newcoord->z = (*q)->z;
newcoord->score = (*q)->score;
newcoord->cost = (*q)->cost;
openList.push_back(newcoord);
closedList.erase(q);
}
}
q++;
}
if (!found) //If not found on either list
{
possibleState->parent = current; //Push onto open list
a = abs((possibleState)->x / 10 - goalState->x / 10);
b = abs((possibleState)->z / 10 - goalState->z / 10);
(possibleState)->score = (possibleState->cost) + ((a)+(b));
openList.push_back(possibleState);
}
sort(openList.begin(), openList.end(), decompare); // Sort the open list by score
}
if (possibleState->x == goalState->x && possibleState->z == goalState->z) //if goal found
{
openList.push_back(possibleState);
node = possibleState;
goalFound = true;
while (node != 0)
{
wayPoints.push_back(*node);
node = node->parent;
wayCount = wayPoints.size() - 1;
}
}
}
}
}
closedList.push_back(current);
}
player->setWayPoints(wayPoints);
wayPoints.clear();
player->setMoved(2);
player->setPath(1);
openList.clear();
closedList.clear();
goalFound = false;
player->setNewPath(1);
return true;
}
else {
return false;
}
}
Are there any bugs that need to be sorted in this code that anyone can see? Or is it just important optimizations that need making? Thanks

Opencart get current layout_id

I have created a custom layout and i planned to hide products only for those custom layout assigned pages.
Is there any way to get current layout id ?
For example, Let's say my custom layout_id is 10, I planned to use some looping like below to hide/show products
if (current_layout_id != 10) {
// Display products
}else {
// Hide Products
}
find layout id of current displayed module page
if (isset($this->request->get['route'])) {
$route = $this->request->get['route'];
} else {
$route = 'common/home';
}
$layout_id = 0;
if (substr($route, 0, 16) == 'product/category' && isset($this->request->get['path'])) {
$path = explode('_', (string)$this->request->get['path']);
$layout_id = $this->model_catalog_category->getCategoryLayoutId(end($path));
}
if (substr($route, 0, 16) == 'product/product' && isset($this->request->get['product_id'])) {
$layout_id = $this->model_catalog_product->getProductLayoutId($this->request->get['product_id']);
}
if (substr($route, 0, 16) == 'product/information' && isset($this->request->get['information_id'])) {
$layout_id = $this->model_catalog_information->getInformationLayoutId($this->request->get['information_id']);
}
if (!$layout_id) { $layout_id = $this->model_design_layout->getLayout($route); }
if (!$layout_id) { $layout_id = $this->config->get('config_layout_id'); }
end of layout id finding

Resources