joomla routing in cmgroupbuing component - joomla

How can I change cmgroupbuying joomla URL in component router file?
For example I have a Deal width this address
index.php/component/cmgroupbuying/deal/5/aaaaa?Itemid=437
replace
with
index.php/deal/5
Right now I'm trying change route.php file in component but this not right

function CMGroupBuyingBuildRoute(&$query)
{
$segments = array();
if(isset($query['view']))
{
if($query['view'] == 'dealprevue' || $query['view'] == 'freecouponprevue'
|| $query['view'] == 'todaydeal' || $query['view'] == 'todayfreecoupon'
|| $query['view'] == 'cart' || $query['view'] == 'checkout' || $query['view'] == 'orders'
|| $query['view'] == 'alldeals' || $query['view'] == 'freecoupons'
|| $query['view'] == 'activedeals' || $query['view'] == 'activefreecoupons'
|| $query['view'] == 'upcomingdeals' || $query['view'] == 'upcomingfreecoupons'
|| $query['view'] == 'expireddeals' || $query['view'] == 'expiredfreecoupons'
|| $query['view'] == 'partner' || $query['view'] == 'dealmanagement'
|| $query['view'] == 'freecouponmanagement'|| $query['view'] == 'products')
{
unset($query['view']);
}
elseif($query['view'] == 'deal' || $query['view'] == 'freecoupon'
|| $query['view'] == 'order' || $query['view'] == 'rssfeeds'
|| $query['view'] == 'search' || $query['view'] == 'dealsubmission'
|| $query['view'] == 'partnermanagement'
|| $query['view'] == 'staffmanagement'
|| $query['view'] == 'freecouponsubmission'
|| $query['view'] == 'category'
|| $query['view'] == 'coupon'
|| $query['view'] == 'product')
{
$segments[] = $query['view'];
unset($query['view']);
}
else
{
$segments[] = $query['view'];
}
if(isset($query['navigation']))
{
$segments[] = $query['navigation'];
unset($query['navigation']);
}
if(isset($query['id']))
{
$segments[] = $query['id'];
unset($query['id']);
}
if(isset($query['alias']))
{
$segments[] = $query['alias'];
unset($query['alias']);
}
if(isset($query['download']))
{
$segments[] = $query['download'];
unset($query['download']);
}
}
if(isset($query['type']))
{
$segments[] = $query['type'];
unset($query['type']);
}
if(isset($query['location']))
{
$segments[] = $query['location'];
unset($query['location']);
}
return $segments;
}

function CMGroupBuyingParseRoute($segments)
{
$vars = array();
switch($segments[0])
{
case 'deal':
$vars['view'] = 'deal';
if(isset($segments[1]))
$vars['id'] = $segments[1];
if(isset($segments[2]))
$vars['alias'] = $segments[2];
break;
case 'freecoupon':
$vars['view'] = 'freecoupon';
if(isset($segments[1]))
$vars['id'] = $segments[1];
if(isset($segments[2]))
$vars['alias'] = $segments[2];
break;
case 'order':
$vars['view'] = 'order';
if(isset($segments[1]))
$vars['id'] = $segments[1];
break;
case 'todaydeal':
$vars['view'] = 'todaydeal';
break;
case 'todayfreecoupon':
$vars['view'] = 'todayfreecoupon';
break;
case 'freecoupons':
$vars['view'] = 'freecoupons';
break;
case 'alldeals':
$vars['view'] = 'alldeals';
break;
case 'activedeals':
$vars['view'] = 'activedeals';
break;
case 'upcomingdeals':
$vars['view'] = 'upcomingdeals';
break;
case 'expireddeals':
$vars['view'] = 'expireddeals';
break;
case 'activefreecoupons':
$vars['view'] = 'activefreecoupons';
break;
case 'upcomingfreecoupons':
$vars['view'] = 'upcomingfreecoupons';
break;
case 'expiredfreecoupons':
$vars['view'] = 'expiredfreecoupons';
break;
case 'cart':
$vars['view'] = 'cart';
break;
case 'checkout':
$vars['view'] = 'checkout';
break;
case 'search':
$vars['view'] = 'search';
break;
case 'orders':
$vars['view'] = 'orders';
break;
case 'partner':
$vars['view'] = 'partner';
break;
case 'dealmanagement':
$vars['view'] = 'dealmanagement';
break;
case 'dealsubmission':
$vars['view'] = 'dealsubmission';
if(isset($segments[1]))
$vars['id'] = $segments[1];
break;
case 'freecouponmanagement':
$vars['view'] = 'freecouponmanagement';
break;
case 'freecouponsubmission':
$vars['view'] = 'freecouponsubmission';
if(isset($segments[1]))
$vars['id'] = $segments[1];
break;
case 'rssfeeds':
$vars['view'] = 'rssfeeds';
if(isset($segments[1]))
$vars['type'] = $segments[1];
if(isset($segments[2]))
$vars['location'] = $segments[2];
break;
case 'partnermanagement':
$vars['view'] = 'partnermanagement';
if(isset($segments[1]))
$vars['navigation'] = $segments[1];
if(isset($segments[2]))
$vars['id'] = $segments[2];
break;
case 'staffmanagement':
$vars['view'] = 'staffmanagement';
if(isset($segments[1]))
$vars['navigation'] = $segments[1];
if(isset($segments[2]))
$vars['id'] = $segments[2];
break;
case 'category':
$vars['view'] = 'category';
if(isset($segments[1]))
$vars['id'] = $segments[1];
break;
case 'coupon':
$vars['view'] = 'coupon';
if(isset($segments[1]))
{
$vars['download'] = $segments[1];
$vars['tmpl'] = 'component';
}
break;
case 'products':
$vars['view'] = 'products';
break;
case 'product':
$vars['view'] = 'product';
if(isset($segments[1]))
$vars['id'] = $segments[1];
if(isset($segments[2]))
{
$vars['alias'] = str_replace(':', '-', $segments[2]);
}
break;
default:
$vars['view'] = '';
break;
}
return $vars;
}

You have 2 options.
1:
Create a custom system plugin which is listening to the onAfterRoute and onAfterInitialise event, to add buildroules and to preprocess the request url, so that the »cmgroupbuying« component will be triggered with the right variables like:
$vars = [
'option' => 'com_cmgroupbuying',
'view' => …
];
All in all this can become a quite complicated thing, since you have to find a generic way to do transform the url in both ways (url to variables and vice versa), what should be done by registering additional router rules. If you decide to do so, have a look at the »language filter« plugin or here, because therein a similar thing is done.
In a nutshell, your plugin must act if either: JRoute::_( … ) is called, to make the transformation you desire (variables to url). And onAfterRoute to do the opposite (url to variables).
That is basically what a component router does and if com_cmgroupbuying is not your own, you cannot/should not create/modify this and take the »route over a plugin« instead.
2:
Create a menu link for each deal/item. This is the easiest way, but not generic, depending on how many links you need to create, or to maintain, this the fastest thing to do.
UPDATE
For me routing in Joomla! is the most complicated and misleading thing in the whole system and good documentation is rare. One needs a good understanding of the overall application flow, how the used component is figured out and instantiated.
Additionally you need to understand the routing concept in Joomla!, what is different from most other Frameworks like symfony or drupal, where a url like /way/to/go/{id} is mapped to a controller method. Joomla! is working like a command-line application and »SEF« (search engine friendly) URLS are lying in a layer above that. With a url like /way/to/go/{id} two things need to be done:
Transform the array of Segments (numerical index based array) ['way', 'to', 'go', <id>] into a set of arguments (most often called vars, associative array) [ 'option' => 'com_…', 'view' => 'viewtoshow', 'task' => 'thingToDo', … ]. That is what the parse method needs to return to the Joomla! system, which will use those »vars«/arguments to execute the component.
Transform the set of »vars«/arguments (associative array) to the url segments (numerical indexed array), what basically is the opposite of 1. But this transformation is executed each time JRoute::_() is executed. If it is called like JRoute::_('index.php?option=com_somecome&view=someview'), Joomla! will use the Router of com_somecome and call its build method and hand over the arguments from the string given to JRoute::_(<string>)
But keep in mind that going from 1. to 2. and from 2. to 1. needs to keep up an invariance, what means the set of vars and the connected url need to be distinct, and going from an A to B must always be reversible and lead to the same result all the time.
How this transformation is done, is totally dependent from your component and if you would like to »short-circuit« joomla, so that /deal/{id} goes over to ['option' => 'com_cmgroupbuying', 'view' => 'product', …] you will need a system plugin and set the missing vars onAfterInitialise, otherwise your component will never be called.
More detailed documentation can be found here
I want to underline the recommendation to look into the source as mentioned above.

Related

How to write a condition based query?

I have a query to run based on the user type selected. I have around 9 users. Based on 9 users, only first line will be different remaining conditions are going to be same.
Below is the code I am writing. I dont want to repeat the same line again for 9 users.
How can I this query without repeating the condition?
if($user_id == 1) {
$slmids = Production::groupBy('gid', 'rid', 'aid', 'sid')
//same
->where("yrsno", $currentYear)
->orderBy("order")
->orderBy("rid")
->orderBy("aid")
->get(['gid', 'rid', 'aid', 'sid'])
->toArray();
}
else if($user_id == 2) {
$slmids = Production::where('gid', $user['sgrp'])
//same
->where("yrsno", $currentYear)
->orderBy("order")
->orderBy("rid")
->orderBy("aid")
->get(['gid', 'rid', 'aid', 'sid'])
->toArray();
}
else if($user_id == 3){
.....
}
$production = Production::where("yrsno", $currentYear)
->orderBy("order")
->orderBy("rid")
->orderBy("aid")
->get(['gid', 'rid', 'aid', 'sid']);
if ($user_id == 1) {
$production = $production->groupBy('gid', 'rid', 'aid', 'sid');
} else if($user_id == 2) {
$production = $production->where('gid', $user['sgrp']);
} else if($user_id == 3) {
....
}
$result = $production->toArray();
You can try like this way.
$slmids = Production::where("yrsno", $currentYear);
if ($user_id == 1)
$slmids = $slimds->groupBy('gid', 'rid', 'aid', 'sid');
elseif ($user_id == 2)
$slmids = $slmids->where('gid', $user['sgrp']);
...
// you can use switch-case if you want
$slmids = $slmids->orderBy('order')
->orderBy('rid')
->orderBy('aid')
->get(['gid', 'rid', 'aid', 'sid'])
->toArray();
I believe groupBy must be placed first before orderBy
Use When to make easy Try following code:
$production = Production::where("yrsno", $currentYear)
->orderBy("order")
->orderBy("rid")
->orderBy("aid")
->query();
$production->when($user_id == 1, function ($query) {
$query->groupBy('gid', 'rid', 'aid', 'sid');
});
$production->when($user_id == 2, function ($query) use ($user) {
$query->where('gid', $user['sgrp']);
});
$results = $production->get()->toArray();
check more example https://stackoverflow.com/a/52333718/8970463

multiple if/else statement is laravel not working for me

please help me out, i want to perform this conditional statement is one or two of this conditional statement will be through but none of the expression i provide will be save to database... Thanks
This are the function that i called
public function updateuserstage($userid){
$user = User::where("user_id",$userid)->first();
//get the stage bonus for this stage
$stageBonus = StageBonus::where("user_id",$userid)-
where("stage",$user->stage)->first();
//get the completion bonus
$bonus = $this->completionbonus($user->stage);
$directdownline = $this->loaduserdirectdownlines($userid);
// $downlineright = $this-
loaduserdirectdownlines($directdownline[0]->user_id);
// $downlineleft = $this-
loaduserdirectdownlines($directdownline[1]->user_id);
$downlineright = $this-
loaduserdirectdownlinesrightleft($directdownline[0]->user_id);
$downlineleft = $this-
loaduserdirectdownlinesrightleft($directdownline[1]->user_id);
// matching bonus reward
if(($user->boosted == 1) && ($directdownline[0]->boosted == 1) &&
($directdownline[1]->boosted == 1) && ($downlineright == 2) &&
($downlineleft == 2)){
if (isset($bonus['point']))
$stageBonus->point = $stageBonus->point+$bonus['point'];
if (isset($bonus['ultimapv']))
$stageBonus->point = $stageBonus-
point+$bonus['ultimapv'];
if (isset($bonus['cash']))
$stageBonus->cash = $stageBonus->cash+$bonus['cash'];
// direct downline reward
This function here return true when i log it outside loop but never save to database in here
} else if(($user->boosted == 1) && ($directdownline[0]->boosted ==
1) || ($directdownline[1]->boosted == 1)){
if (isset($bonus['point']))
$stageBonus->point = $stageBonus->point+$bonus['point'];
if (isset($bonus['totalultimarefbonus']))
$stageBonus->cash = $stageBonus-
cash+$bonus['totalultimarefbonus'];
} else if(($user->boosted == 1) && ($directdownline[0]->boosted ==
1) || ($directdownline[1]->boosted == 0)){
if (isset($bonus['point']))
$stageBonus->point = $stageBonus->point+$bonus['point'];
if (isset($bonus['ultimarefbonus']))
$stageBonus->cash = $stageBonus-
cash+$bonus['ultimarefbonus'];
} else if(($user->boosted == 1) && ($directdownline[0]->boosted ==
0) || ($directdownline[1]->boosted == 1)){
if (isset($bonus['point']))
$stageBonus->point = $stageBonus->point+$bonus['point'];
if (isset($bonus['ultimarefbonus']))
$stageBonus->cash = $stageBonus-
>cash+$bonus['ultimarefbonus'];
} else if(($user->boosted == 1) && ($downlineright[0]->boosted ==
1) || ($downlineright[1]->boosted == 1) || ($downlineleft[0]-
>boosted == 1) || ($downlineleft[1]->boosted == 1)){
if (isset($bonus['ultimarefbonuschild']))
$stageBonus->cash = $stageBonus-
>cash+$bonus['ultimarefbonuschild'];
// referral reward for parent user if boosted
} else if($user->boosted == 1){
if (isset($bonus['refbonus']))
$stageBonus->cash = $stageBonus->cash+$bonus['refbonus'];
// referral reward for user that is not boosted
} else if($user->boosted == 0){
if (isset($bonus['point']))
$stageBonus->point = $stageBonus->point+$bonus['point'];
if (isset($bonus['refbonus']))
$stageBonus->cash = $stageBonus->cash+$bonus['refbonus'];
}
$stageBonus->completed = 1;
$stageBonus->completed_on = Carbon::now();
$stageBonus->save();
//increase user stage
$user->stage+= 1;
$user->save();
}
}
This is the function that will determine what to save in those expression
public function completionbonus($stage){
switch($stage){
case 1:
return [ 'voucher'=>600, 'cash'=>5000, 'point'=>100, 'refbonus'=>2000, 'totalultimarefbonus'=>4000, 'ultimarefbonus'=>2000, 'ultimarefbonuschild'=>1500, 'ultimapv'=>700];
break;
}
}
This is the model
Schema::create('stage_bonuses', function (Blueprint $table) {
$table->increments('id');
$table->string('user_id');
$table->bigInteger('point')->default(0);
$table->bigInteger('cash')->default(0);
$table->date('date_entered')->nullable();
$table->timestamps();
$table->foreign('user_id')->references('user_id')->on('users');
});
Thank you very much i really appreciate your effort
Have actually got the answer the condition is returning two or true and if is returning two or more true it will not get save to database... Thanks

Paging property missing in Status

In latest version of LINQ to Twitter v2.1.08 there is no longer support for paging. How do I get a certain page without page property?
Cheers
int curPageIndex=5;
string pageindex="5";
string cmd = "next";
using (var twitterCtx = new TwitterContext(myauth))
{
try
{
// set up the "main query"
IQueryable<Status> test = from tweet in twitterCtx.Status select tweet;
switch (cmd)
{
case "prev":
test = pageindex.Length > 0
? test.Where(p => p.Type == StatusType.Home && p.Page == curPageIndex)
: test.Where(p => p.Type == StatusType.Home);
break;
case "next":
test = pageindex.Length > 0
? test.Where(p => p.Type == StatusType.Home && p.Page == curPageIndex)
: test.Where(p => p.Type == StatusType.Home);
break;
default:
//
test = test.Where(p => p.Type == StatusType.Home);
break;
}
Solution: Changed Page parameter to SinceID and MaxID
//Get the statusids in the query, add or subtract so you skip current id's
ulMaxId = test.Min(status => ulong.Parse(status.StatusID)) - 1;
ulSinceID = test.Max(status => ulong.Parse(status.StatusID)) + 1;
//Return ID above and use them in future calls (below)
//Now you can navigate timelines, depending if you are stepping forward or backwards
? test.Where(p => p.Type == StatusType.Home && p.SinceID == ulong.Parse(sinceid)
...
? test.Where(p => p.Type == StatusType.Home && p.MaxID == ulong.Parse(maxid))

Magento hardcodes VARCHAR attributes to 255 length, any reason to not change this?

I have a requirement to save an Order Item attribute as a string up to 400 char. While it could be implemented using a text attribute, I would rather use a varchar(400). However, the _addFlatAttribute() method in Mage_Sales_Model_Resource_Setup hardcodes the length of varchar to 255. It could be changed after the fact by a setup script with DDL, but I'm wondering if there are likely to be downstream dependencies on VARCHAR being 255 char.
Any thoughts?
I believe that you will be fine & well-advised to do this given the ever-expanding size of order entity data in an instance. I'm curious what version of Magento though, as CE1.6+ is the first release to have the Mage_Sales_Model_Resource_Setup [link] class. Prior to CE1.6 the class was Mage_Sales_Model_Mysql4_Setup [link]. This is an important distinction because the column definition method in these classes differs to accomodate the DB-agnostic approach in 1.6+
Mage_Sales_Model_Mysql4_Setup::_getAttributeColumnDefinition [link]:
protected function _getAttributeColumnDefinition($code, $data)
{
$columnDefinition = '';
$type = isset($data['type']) ? $data['type'] : 'varchar';
$req = isset($data['required']) ? $data['required'] : false;
switch ($type) {
case 'int':
$columnDefinition = 'int(10) unsigned';
break;
case 'decimal':
$columnDefinition = 'decimal(12,4)';
break;
case 'text':
$columnDefinition = 'text';
break;
case 'date':
$columnDefinition = 'datetime';
break;
default:
$columnDefinition = 'varchar(255)';
break;
}
if ($req) {
$columnDefinition.= ' NOT NULL';
}
return $columnDefinition;
}
And Mage_Sales_Model_Resource_Setup::_getAttributeColumnDefinition [link]:
protected function _getAttributeColumnDefinition($code, $data)
{
// Convert attribute type to column info
$data['type'] = isset($data['type']) ? $data['type'] : 'varchar';
$type = null;
$length = null;
switch ($data['type']) {
case 'timestamp':
$type = Varien_Db_Ddl_Table::TYPE_TIMESTAMP;
break;
case 'datetime':
$type = Varien_Db_Ddl_Table::TYPE_DATETIME;
break;
case 'decimal':
$type = Varien_Db_Ddl_Table::TYPE_DECIMAL;
$length = '12,4';
break;
case 'int':
$type = Varien_Db_Ddl_Table::TYPE_INTEGER;
break;
case 'text':
$type = Varien_Db_Ddl_Table::TYPE_TEXT;
$length = 65536;
break;
case 'char':
case 'varchar':
$type = Varien_Db_Ddl_Table::TYPE_TEXT;
$length = 255;
break;
}
if ($type !== null) {
$data['type'] = $type;
$data['length'] = $length;
}
$data['nullable'] = isset($data['required']) ? !$data['required'] : true;
$data['comment'] = isset($data['comment']) ? $data['comment'] : ucwords(str_replace('_', ' ', $code));
return $data;
}
I can't imagine so, it might be different if you were to decrease the size of varchar, though.
See also:
Is there a good reason I see VARCHAR(255) used so often (as opposed to another length)?
Use type TEXT instead of VARCHAR. Mage_Sales_Model_Resource_Setup assumes a length of 64KB in this case.

How wait to AJAX response

i wanna know how i can make a pause in the system to wait the answer from the server in an AJAX request?
var method = childs[cont].getAttribute('method');
var address = childs[cont].getAttribute('address');
/*
* Making the AJAX connection
* and returning the results.
*/
phone = new ConstructorXMLHttpRequest();
onreadystatechange = function(){
switch(phone.readyState){
case 0: if(phone.readyState == 0){
break;
}
case 1: if(phone.readyState == 1){
break;
}
case 2: if(phone.readyState == 2){
break;
}
case 3: if(phone.readyState == 3){
break;
}
case 4: if(phone.readyState == 4){
if(phone.status == 200){
var val = phone.responseText;
alert([val,1]);
dataInsert(val);
break;
}
else{
alert("Problemas status:"+phone.status+" state:"+phone.readyState);
break;
}
}
}
};
phone.onreadystatechange = onreadystatechange;
if (method == 'POST'){
phone.open(method, address, true);
phone.setRequestHeader("Content-type", "multipart/form-data");
phone.send(xml2string(prepCall(childs[cont])));
}else if(method == 'GET'){
phone.open(method, address, true);
phone.setRequestHeader("Content-type", "multipart/form-data");
}
Your code looks OK. Remember AJAX is asynchronous, so instead of actually pausing, just hook a callback to your ajax request which will be executed once the request is completed.
In your case, if the request is completed successfully it will alert and execute the dateInsert function.

Resources