CakePHP 3 : delete and update array from cookie - cakephp

I'm using CakePHP 3.2 for writing a shopping cart application.
I'm using cookie to add items to the cart.
Now I want to update and delete value from cart. so that if user clicks on the same product add to cart with a different quantity value the existing record will be deleted and new will be added to cart.
This is my addToCart() method.
public function addToCart()
{
$this->loadModel('Products');
if ($this->request->is('post')) {
$p_id = $this->request->data('product_id');
$p_quantity = $this->request->data('qnty');
$product = $this->Products->get($p_id);
$product->quantity = $p_quantity;
if (!$product) {
throw new NotFoundException(__('Invalid Product'));
}
$cart = $this->Cookie->read('Cart') ? $this->Cookie->read('Cart') : [];
$itemInCart = false;
$itemUpdated = false;
if ($cart != null) {
foreach($cart as $cart_item):
if ($cart_item['id'] == $p_id) {
if ($cart_item['quantity'] != $p_quantity) {
$this->Cookie->delete('Cart.'.$cart_item); // line 148
$cart[] = $product;
$this->Cookie->write('Cart', $cart);
$itemsCount = count($this->Cookie->read('Cart'));
$this->Flash->success('Product updated in cart');
return $this->redirect($this->referer);
}
$itemInCart = true;
}
endforeach;
}
if (!$itemInCart) {
$cart[] = $product;
$this->Cookie->write('Cart', $cart);
$itemsCount = count($this->Cookie->read('Cart'));
if ($itemUpdated) {
$this->Flash->success(__('Product updated in cart'));
} else {
$this->Flash->success(__('Product added to cart'));
}
return $this->redirect($this->referer());
} else {
$this->Flash->success(__('Product is already in cart'));
return $this->redirect($this->referer());
}
}
}
But this is giving error as
Notice (8): Array to string conversion [APP/Controller/OrdersController.php, line 148]
How could I update the quantity value in the cart.

Try the following:
public function addToCart()
{
$this->loadModel('Products');
if ($this->request->is('post')) {
$p_id = $this->request->data('product_id');
$p_quantity = $this->request->data('qnty');
$product = $this->Products->get($p_id);
if (!$product) {
throw new NotFoundException(__('Invalid Product'));
}
$product->quantity = $p_quantity;
$cart = $this->Cookie->read('Cart') ? $this->Cookie->read('Cart') : [];
$itemInCart = false;
$new_cart = [];
if ($cart != null) {
foreach($cart as $cart_item):
if ($cart_item['id'] == $p_id) {
if($p_quantity == 0){
//Removed the item from cart and set itemInCart to true
$itemInCart = true;
}else{
//update the quantity of item
$new_cart[] = $product;
$itemInCart = true;
}
}else{
$new_cart[] = $cart_item;
}
endforeach;
}
if ($itemInCart) {
$this->Cookie->write('Cart', $new_cart);
$this->Flash->success(__('Product updated in cart'));
} else {
$cart[] = $product;
$this->Cookie->write('Cart', $cart);
$this->Flash->success(__('Product added to cart'));
}
return $this->redirect($this->referer);
}
}

Related

update client database for IdentityServer4

I have identityServer4 as my oAuth server in my net core appilication. I was able to initialize my client database using the following code:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
SeedData.EnsureSeedData(serviceScope);
}
}
}
The migration and client initialization is done using
public class SeedData
{
public static void EnsureSeedData(IServiceScope serviceScope)
{
Console.WriteLine("Seeding database...");
serviceScope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate();
var context = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
context.Database.Migrate();
EnsureSeedData(context);
Console.WriteLine("Done seeding database.");
Console.WriteLine();
}
private static void EnsureSeedData(ConfigurationDbContext context)
{
if (!context.Clients.Any())
{
Console.WriteLine("Clients being populated");
foreach (var client in Config.GetClients().ToList())
{
context.Clients.Add(client.ToEntity());
}
context.SaveChanges();
}
else
{
Console.WriteLine("Clients already populated, update clients");
foreach (var client in Config.GetClients().ToList())
{
var item = context.Clients.Where(c => c.ClientId == client.ClientId).FirstOrDefault();
if(item == null)
{
context.Clients.Add(client.ToEntity());
} else {
var model = client.ToEntity();
model.Id = item.Id;
context.Entry(item).CurrentValues.SetValues(model);
}
}
context.SaveChanges();
}
if (!context.IdentityResources.Any())
{
Console.WriteLine("IdentityResources being populated");
foreach (var resource in Config.GetIdentityResources().ToList())
{
context.IdentityResources.Add(resource.ToEntity());
}
context.SaveChanges();
}
else
{
Console.WriteLine("IdentityResources already populated");
foreach (var resource in Config.GetIdentityResources().ToList())
{
var item = context.IdentityResources.Where(c => c.Name == resource.Name).FirstOrDefault();
if (item == null)
{
context.IdentityResources.Add(resource.ToEntity());
}
else
{
var model = resource.ToEntity();
model.Id = item.Id;
context.Entry(item).CurrentValues.SetValues(model);
}
}
context.SaveChanges();
}
if (!context.ApiResources.Any())
{
Console.WriteLine("ApiResources being populated");
foreach (var resource in Config.GetApiResources().ToList())
{
context.ApiResources.Add(resource.ToEntity());
}
context.SaveChanges();
}
else
{
Console.WriteLine("ApiResources already populated");
foreach (var resource in Config.GetApiResources().ToList())
{
var item = context.ApiResources.Where(c => c.Name == resource.Name).FirstOrDefault();
if (item == null)
{
context.ApiResources.Add(resource.ToEntity());
}
else
{
var model = resource.ToEntity();
model.Id = item.Id;
context.Entry(item).CurrentValues.SetValues(model);
}
}
context.SaveChanges();
}
}
}
The initialization of the client database works well. However, there are problems to update the client database. Some of the records are not updated. Is there a better way to do this?
After some struggle, I found a solution that works for me. Basically, I created a table, ClientVersion, that track clients definition changes.
Here is my new version of SeedData.cs.
public class SeedData
{
public static void EnsureSeedData(IServiceScope serviceScope)
{
Console.WriteLine("Seeding database...");
serviceScope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate();
var context = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
context.Database.Migrate();
var versionContext = serviceScope.ServiceProvider.GetRequiredService<VersionContext>();
versionContext.Database.Migrate();
EnsureSeedData(context, versionContext);
Console.WriteLine("Done seeding database.");
Console.WriteLine();
}
private static void EnsureSeedData(ConfigurationDbContext context, VersionContext versionContext)
{
ClientVersion version = versionContext.ClientVersion.FirstOrDefault();
bool blUpdate = version == null ? true : (Config.CurrentVersion > version.Version ? true: false);
if(blUpdate)
{
if(version == null)
{
version = new ClientVersion()
{
Version = Config.CurrentVersion,
};
versionContext.ClientVersion.Add(version);
} else
{
version.Version = Config.CurrentVersion;
versionContext.ClientVersion.Update(version);
}
versionContext.SaveChanges();
}
if (!context.Clients.Any())
{
Console.WriteLine("Clients being populated");
foreach (var client in Config.GetClients().ToList())
{
context.Clients.Add(client.ToEntity());
}
context.SaveChanges();
}
else
{
Console.WriteLine("Clients already populated, update clients");
if (blUpdate)
{
foreach (var client in Config.GetClients().ToList())
{
var item = context.Clients
.Include(x => x.RedirectUris)
.Include(x => x.PostLogoutRedirectUris)
.Include(x => x.ClientSecrets)
.Include(x => x.Claims)
.Include(x => x.AllowedScopes)
.Include(x => x.AllowedCorsOrigins)
.Include(x => x.AllowedGrantTypes)
.Where(c => c.ClientId == client.ClientId).FirstOrDefault();
if (item != null)
{
context.Clients.Remove(item);
}
context.Clients.Add(client.ToEntity());
}
context.SaveChanges();
}
}
if (!context.IdentityResources.Any())
{
Console.WriteLine("IdentityResources being populated");
foreach (var resource in Config.GetIdentityResources().ToList())
{
context.IdentityResources.Add(resource.ToEntity());
}
context.SaveChanges();
}
else
{
Console.WriteLine("IdentityResources already populated");
if (blUpdate)
{
foreach (var resource in Config.GetIdentityResources().ToList())
{
var item = context.IdentityResources.Where(c => c.Name == resource.Name).FirstOrDefault();
if (item != null)
{
context.IdentityResources.Remove(item);
}
context.IdentityResources.Add(resource.ToEntity());
}
context.SaveChanges();
}
}
if (!context.ApiResources.Any())
{
Console.WriteLine("ApiResources being populated");
foreach (var resource in Config.GetApiResources().ToList())
{
context.ApiResources.Add(resource.ToEntity());
}
context.SaveChanges();
}
else
{
Console.WriteLine("ApiResources already populated");
if (blUpdate)
{
foreach (var resource in Config.GetApiResources().ToList())
{
var item = context.ApiResources.Where(c => c.Name == resource.Name).FirstOrDefault();
if (item != null)
{
context.ApiResources.Remove(item);
}
context.ApiResources.Add(resource.ToEntity());
}
context.SaveChanges();
}
}
}
}

When changing templates on custom block the content gets deleted from the DB

I have a custom block for Concrete5 I built which uses multiple template files associated with it. If I apply the template to the block when I am initially adding the block to the page everything works fine. However if I then try to change templates after the block has already been set I run into issues. When saving changes with the new template, all my content gets deleted from the DB; so everything in that current row equals null except the block id "bID", the bID will change to the next increment.
I do not know why this happens!! I feel like I ran into a similar a long time ago but don't remember how it was resolved. Any advice would be great!
My template files are just standard html in a php file with the <?php defined('C5_EXECUTE') or die("Access Denied."); ?> at the top of the file.
My controller (which is my suspect for the issue right now) look like this:
<?php
namespace Concrete\Package\ThemeCaboodle\Block\GridBlock;
use Concrete\Core\Block\BlockController;
use Database;
use Page;
use Concrete\Core\Editor\LinkAbstractor;
use Core;
use File;
use View;
use BlockType;
class Controller extends BlockController
{
public $defaultBlockClassList = '';
public $defaultEntriesClassList = 'unit-md-4';
protected $btTable = 'btGrid';
protected $btExportTables = array('btGrid', 'btGridEntries');
protected $btInterfaceWidth = "600";
protected $btWrapperClass = 'ccm-ui';
protected $btInterfaceHeight = "550";
protected $btCacheBlockRecord = true;
protected $btExportFileColumns = array('thumbnailFID');
protected $btCacheBlockOutput = true;
protected $btCacheBlockOutputOnPost = true;
protected $btCacheBlockOutputForRegisteredUsers = false;
protected $btIgnorePageThemeGridFrameworkContainer = true;
public function getBlockTypeDescription()
{
return t("Easily add a grid with prebuilt templates to your site using the grid block");
}
public function getBlockTypeName()
{
return t("Grid");
}
public function getFileObject($fID) {
return File::getByID($fID);
}
public function getSearchableContent()
{
$db = Database::get();
$rows = $db->Execute('SELECT * FROM btGridEntries WHERE bID = ?', array($this->bID));
$content = '';
foreach ($rows as $row) {
$content .= $row['title'].' ';
$content .= $row['description'].' ';
}
return $content;
}
public function view()
{
$this->set('entries', $this->getEntries());
$this->set('block', $this->getBlockData());
$this->addHeaderItem('<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"/>');
}
public function add()
{
$this->requireAsset('core/file-manager');
$this->requireAsset('core/sitemap');
$this->requireAsset('redactor');
}
public function edit()
{
$this->requireAsset('core/file-manager');
$this->requireAsset('core/sitemap');
$this->requireAsset('redactor');
$this->set('entries', $this->getEntries());
$this->set('block', $this->getBlockData());
}
public function composer()
{
$this->edit();
}
public function registerViewAssets($outputContent = '')
{
$al = \Concrete\Core\Asset\AssetList::getInstance();
$this->requireAsset('javascript', 'jquery');
}
public function duplicate($newBID)
{
parent::duplicate($newBID);
$db = Database::get();
$rows = $db->Execute('SELECT * FROM btGridEntries WHERE bID = ?', array($this->bID));
while ($row = $rows->FetchRow()) {
$db->execute('INSERT INTO btGridEntries (bID, thumbnailFID, fallbackFID, title, description, classList, buttonText, sortOrder, externalLinkURL, internalLinkCID, fileFID) values(?,?,?,?,?,?,?,?,?,?,?)',
array(
$newBID,
$row['ENTRY_thumbnailFID'],
$row['ENTRY_fallbackFID'],
$row['ENTRY_title'],
$row['ENTRY_description'],
$row['ENTRY_classList'],
$row['ENTRY_buttonText'],
$row['ENTRY_sortOrder'],
$row['ENTRY_externalLinkURL'],
$row['ENTRY_internalLinkCID'],
$row['ENTRY_fileFID']
)
);
}
}
public function delete()
{
$db = Database::get();
$db->delete('btGridEntries', array('bID' => $this->bID));
parent::delete();
}
public function save($args)
{
$db = Database::get();
$db->execute('DELETE from btGridEntries WHERE bID = ?', array($this->bID));
parent::save($args);
if (isset($args['ENTRY_sortOrder'])) {
$count = count($args['ENTRY_sortOrder']);
$i = 0;
while ($i < $count) {
$externalLinkURL = $args['ENTRY_externalLinkURL'][$i];
$internalLinkCID = $args['ENTRY_internalLinkCID'][$i];
$fileFID = $args['ENTRY_fileFID'][$i];
switch (intval($args['ENTRY_linkType'][$i])) {
case 1:
$externalLinkURL = '';
$fileFID = 0;
break;
case 2:
$internalLinkCID = 0;
$fileFID = 0;
break;
case 3:
$externalLinkURL = '';
$internalLinkCID = 0;
break;
default:
$externalLinkURL = '';
$internalLinkCID = 0;
$fileFID = 0;
break;
}
if (isset($args['ENTRY_description'][$i])) {
$args['ENTRY_description'][$i] = LinkAbstractor::translateTo($args['ENTRY_description'][$i]);
}
$db->execute('INSERT INTO btGridEntries (bID, thumbnailFID, fallbackFID, title, description, classList, buttonText, sortOrder, externalLinkURL, internalLinkCID, fileFID) values(?,?,?,?,?,?,?,?,?,?,?)',
array(
$this->bID,
intval($args['ENTRY_thumbnailFID'][$i]),
intval($args['ENTRY_fallbackFID'][$i]),
$args['ENTRY_title'][$i],
$args['ENTRY_description'][$i],
$args['ENTRY_classList'][$i],
$args['ENTRY_buttonText'][$i],
$args['ENTRY_sortOrder'][$i],
$externalLinkURL,
$internalLinkCID,
$fileFID
)
);
++$i;
}
}
}
public function getBlockAssetPath() {
$bt = BlockType::getByHandle('buckets_block');
return Core::make('helper/concrete/urls')->getBlockTypeAssetsURL($bt);
}
public function getBlockData()
{
$db = Database::get();
$row = $db->GetRow('SELECT * FROM btGrid WHERE bID = ?', array($this->bID));
if ($row['bgFID']) {
$row['BG'] = \File::getByID($row['bgFID'])->getVersion()->getRelativePath();
}
return $row;
}
public function getEntries()
{
$v = View::getInstance();
$db = Database::get();
$rows = $db->GetAll('SELECT * FROM btGridEntries WHERE bID = ? ORDER BY sortOrder', array($this->bID));
// in view mode, linkURL takes us to where we need to go whether it's on our site or elsewhere
$entries = array();
foreach ($rows as $row) {
// Generate the URL based on what the linkType is
if ($row['externalLinkURL'] =='' && !$row['fileFID'] && $row['internalLinkCID']) {
$c = Page::getByID($row['internalLinkCID'], 'ACTIVE');
$row['linkURL'] = $c->getCollectionLink();
} elseif ($row['externalLinkURL'] =='' && !$row['internalLinkCID'] && $row['fileFID']) {
$f = File::getByID($row['fileFID']);
$row['linkURL'] = $f ? $f->getVersion()->getRelativePath() : '';
} elseif ($row['externalLinkURL']!='') {
$row['linkURL'] = $row['externalLinkURL'];
} else {
$row['linkURL'] = '';
}
// Thumbnail
$thumbnail = $row['thumbnailFID'] ? File::getByID($row['thumbnailFID'])->getVersion()->getRelativePath() : $v->getThemePath().'/no-image.jpg';
$row['thumbnail'] = $thumbnail;
$fallback = $row['fallbackFID'] ? File::getByID($row['fallbackFID'])->getVersion()->getRelativePath() : $v->getThemePath().'/no-image.jpg';
$row['fallback'] = $fallback;
$row['description'] = LinkAbstractor::translateFrom($row['description']);
$entries[] = $row;
}
return $entries;
}
public function getClassList($string) {
$array = explode(',',$string);
if (count($array) > 0) {
return implode(' ',$array);
}
}
}
I apologize for the pretty long file in advance ;) i just want to make sure you can see all possible issues
I think your problem is that in the duplicate function, once you got your results back from your select you put an ENTRY_ prefix everywhere like in $row['ENTRY_thumbnailFID'] when it should really be $row['thumbnailFID'] according to your screenshot

Return single true from function in AngularJS

I'm having a bit of trouble getting my function to return a single true or false
Basically I have an array like below;
orderItem contains menu_modifier_groups which contains menu_modifier_items
Now each menu_modifier_groups has an attribute max_selection_points.
The menu_modifier_items are displayed using ng-repeat with checkboxes and have an ng-model = item.selected
What I want to do is to be able to loop through all menu_modifier_groups and determine if the required menu_modifier_items have been selected by comparing them to max_selection_points of each menu_modifier_groups
This is what I have so far
$scope.isValid = function (orderItem) {
var count = 0;
angular.forEach(orderItem.menu_modifier_groups, function(group) {
angular.forEach(group.menu_modifier_items, function (item) {
count += item.selected ? 1 : 0;
});
if (count != group.max_selection_points) {
return false;
} else if (count == group.max_selection_points) {
return true;
}
});
}
Any help/advice appreciated
Your code should be
$scope.isValid = function(orderItem) {
//By default make it false
var IsAllSelected = false;
angular.forEach(orderItem.menu_modifier_groups, function(group) {
var count = 0;
angular.forEach(group.menu_modifier_items, function(item) {
count += item.selected ? 1 : 0;
});
if (count == group.max_selection_points) {
IsAllSelected = true;
} else {
//if one item failed All select do return false
IsAllSelected = false;
}
});
return IsAllSelected;
}

Kendo Grid: Column Header Checkbox 'Check All' that checks boxes across grid all pages

Is there an example out there in javascript and/or jquery where someone has a 'Check all' checkbox that checks all checkboxes across all pages of a grid?
I have been trying for 3 days now and I am not finding a clean answer.
What i have now is ugly and does not really work...
function onRequestEnd(e) {
var masterCbChecked = $("#masterCheckBox").is(':checked');
var grid = $("#grid").data("kendoGrid");
for (var i = 0; i < grid.dataSource.total(); i ++) {
var dataRow = $("#grid").data("kendoGrid").dataSource.data()[i];
var elementRow = grid.table.find(".cbAdvisor")[i];
if (elementRow != null) {
var checked = elementRow.checked,
row = $(elementRow).closest("tr"),
dataItem = grid.dataItem(grid.tbody.find("tr").eq(i));
checkedIds[dataItem.DimAgentId] = masterCbChecked;
if (masterCbChecked) {
//-select the row
elementRow.checked = true;
row.addClass("k-state-selected");
dataRow.IsSelected = true;
} else {
//-remove selection
elementRow.checked = false;
row.removeClass("k-state-selected");
dataRow.IsSelected = false;
}
}
}
}
function checkAll(ele) {
var state = $(ele).is(':checked');
var grid = $("#grid").data("kendoGrid");
//grid.dataSource.pageSize(grid.dataSource.total());
//grid.dataSource.read();
//grid.refresh();
var currentPage = $("#grid").data("kendoGrid").dataSource.page();
checkedIds = {};
//for (var a = 0; a < modelJson.length; a ++) {
// var m = modelJson[a];
// m.IsSelected = true;
//}
for (var a = 1; a < 2; a ++) {
var pager = grid.pager;
pager.bind('change', a);
grid.one("dataBound", function () {
this.dataSource.page(a);
});
grid.dataSource.fetch();
for (var i = 0; i < grid.dataSource.total(); i ++) {
var dataRow = $("#grid").data("kendoGrid").dataSource.data()[i];
var elementRow = grid.table.find(".cbAdvisor")[i];
if (elementRow != null) {
var checked = elementRow.checked,
row = $(elementRow).closest("tr"),
dataItem = grid.dataItem(grid.tbody.find("tr").eq(i));
checkedIds[dataItem.DimAgentId] = state;
if (state) {
//-select the row
elementRow.checked = true;
row.addClass("k-state-selected");
dataRow.IsSelected = true;
} else {
//-remove selection
elementRow.checked = false;
row.removeClass("k-state-selected");
dataRow.IsSelected = false;
}
}
}
pager.bind('change', currentPage);
grid.one("dataBound", function () {
this.dataSource.page(currentPage);
});
grid.dataSource.fetch();
//mark for paging
if (dataRow != null) {
if (state) {
dataRow.IsSelected = true;
} else {
dataRow.IsSelected = false;
}
}
};
if (!state) {
checkedIds = {};
}
//grid.dataSource.pageSize(50);
//grid.refresh();
}
I got this with this code with some modification:
columns.Template(#<text><input name="chkStatus" type="checkbox" class="chkFormols" /></text>)
.HeaderTemplate("<input type='checkbox' id='chkSelectAll' onclick='checkAll(this)' />").Width(50);
function checkAll(ele) {
debugger;
var state = $(ele).is(':checked');
var grid = $('#grid').data('kendoGrid');
if (state == true) {
$('.chkFormols').prop('checked', true);
}
else {
$('.chkFormols').prop('checked', false);
}
};
I solved this.
<div id="panelGridContainer" class="containerGrid">
#(Html.Kendo().Grid<ReportGridModel>()
.Name("grid")
.Columns(columns =>
{
var m = Model.Section.DrilldownColumns[0];
columns.BoundAll(p => p.DimAgentId, m);
columns.Template(#<text></text>).ClientTemplate("<input type='checkbox' #= IsSelected ? checked='checked':'' # class='cbAdvisor' />")
.HeaderTemplate("<input type='checkbox' id='masterCheckBox' onclick='checkAll(this)'/>").Width(30);
columns.BoundAll(p => p.AdvisorName, m).ClientTemplate("" + "#: AdvisorName #" + "<div class='reportDetailLink ui-button ui-widget ui-button-text-only' onclick='getAdvisorDetailView(this)'>Detail</div>").Width(300);
columns.BoundAll(p => p.GoalAmount, m).ClientTemplate("" + "$#: GoalAmount.formatMoney(0, '.', ',') #" + "<div id='editIndividualGoalLink' class='reportEditLink ui-button ui-widget ui-button-text-only' onclick='openFytaIndividualGoalOverlay(this)'><i class='icon icon-pencil'></i> Edit</div><div class='editIndGoalPanel' style='display:none;'></div>").Width(200);
columns.BoundAll(p => p.RevenueAmount, m).Width(100);
columns.BoundAll(p => p.GoalProgressToDate, m).Width(75);
columns.BoundAll(p => p.RevenueChangeYoY, m).Width(75);
columns.BoundAll(p => p.BranchComposer, m).Width(200);
})
.Sortable()
.Pageable(pager => pager.PageSizes(new[] { 50, 100, 500, 1000 }))
//.Scrollable()
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(50)
.Read(read => read.Action("GetReportGridData", "FytByAdvisor"))
.Events(events => events.RequestStart("onDataSourceRequestStart"))
)
.Events(events => events.DataBound("onGridDataFound").DataBound("onDataBound")))
</div>
var checkedIds = {};
//on click of the checkbox:
$('#grid').on('click', '.cbAdvisor', function() {
var checked = this.checked,
row = $(this).closest("tr"),
grid = $("#grid").data("kendoGrid"),
dataItem = grid.dataItem(row);
if (!checked) {
$('#masterCheckBox').attr('checked', false); // Unchecks it
}
checkedIds[dataItem.DimAgentId] = checked;
if (checked) {
//-select the row
row.addClass("k-state-selected");
} else {
//-remove selection
row.removeClass("k-state-selected");
}
});
//on dataBound event restore previous selected rows:
function onDataBound(e) {
var view = this.dataSource.view();
for(var i = 0; i < view.length;i++){
if(checkedIds[view[i].DimAgentId]){
this.tbody.find("tr[data-uid='" + view[i].uid + "']")
.addClass("k-state-selected")
.find(".cbAdvisor")
.attr("checked","checked");
}
}
var masterCbChecked = $("#masterCheckBox").is(':checked');
var grid = $("#grid").data("kendoGrid");
//if(!masterCbChecked)
//{
// checkedIds = {};
//}
if(masterCbChecked)
{
for (var i = 0; i < grid.dataSource.total(); i ++) {
var dataRow = $("#grid").data("kendoGrid").dataSource.data()[i];
var elementRow = grid.table.find(".cbAdvisor")[i];
if (elementRow != null) {
var checked = elementRow.checked,
row = $(elementRow).closest("tr"),
dataItem = grid.dataItem(grid.tbody.find("tr").eq(i));
if (masterCbChecked) {
checkedIds[dataItem.DimAgentId] = masterCbChecked;
//-select the row
elementRow.checked = true;
row.addClass("k-state-selected");
dataRow.IsSelected = true;
} else {
//-remove selection
elementRow.checked = false;
row.removeClass("k-state-selected");
dataRow.IsSelected = false;
}
}
}
}
}
function checkAll(ele) {
var state = $(ele).is(':checked');
var grid = $("#grid").data("kendoGrid");
grid.dataSource.pageSize(grid.dataSource.total());
grid.dataSource.read();
grid.refresh();
checkedIds = {};
for (var i = 0; i < grid.dataSource.total(); i ++) {
var dataRow = $("#grid").data("kendoGrid").dataSource.data()[i];
var elementRow = grid.table.find(".cbAdvisor")[i];
if (elementRow != null) {
var checked = elementRow.checked,
row = $(elementRow).closest("tr"),
dataItem = grid.dataItem(grid.tbody.find("tr").eq(i));
if (state) {
checkedIds[dataItem.DimAgentId] = state;
//-select the row
elementRow.checked = true;
row.addClass("k-state-selected");
dataRow.IsSelected = true;
} else {
//-remove selection
elementRow.checked = false;
row.removeClass("k-state-selected");
dataRow.IsSelected = false;
}
}
//mark for paging
if (dataRow != null) {
if (state) {
dataRow.IsSelected = true;
} else {
dataRow.IsSelected = false;
}
}
};
// set back to 50 page size
grid.dataSource.pageSize(50);
}
Try this code:
set column in grid(mvc)==>{
columns.Template(#<text></text>).ClientTemplate("<input type='checkbox' name='chkbSlc_Formols' class='chkFormols' />").HeaderTemplate("<input type='checkbox' id='chkSelectAll' onclick='checkAll(this)' name='chk[]'/>").Width(50);
}
function checkAll(ele) {
var state = $(ele).is(':checked');
$('.chkFormols').prop('checked', state);
}

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting T_STRING or T_VARIABLE

I am having a problem while setting up my database file please help me.
I am testing a filehosting script here at http://toorhamza.host56.com/ and I get this error when opening my website
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting T_STRING or T_VARIABLE or '{' or '$' in /home/a6788850/public_html/includes/class.database.php on line 23
This is my class.database.php file and line
<?php
class Database
{
// Singleton object. Leave $me alone.
private static $me;
public $db;
public $host;
public $name;
public $username;
public $password;
public $dieOnError;
public $queries;
public $result;
public $redirect = false;
// Singleton constructor
private function __construct($connect = false)
{
$Config = Config::getConfig();
$this->host = $Config->"localhost";
$this->name = $Config->'test';
$this->username = $Config->'test';
$this->password = $Config->'password';
$this->dieOnError = $Config->dbDieOnError;
$this->db = false;
$this->queries = array();
if ($connect === true)
$this->connect();
}
// Waiting (not so) patiently for 5.3.0...
public static function __callStatic($name, $args)
{
return self::$me->__call($name, $args);
}
// Get Singleton object
public static function getDatabase($connect = true)
{
if (is_null(self::$me))
self::$me = new Database($connect);
return self::$me;
}
// Do we have a valid database connection?
public function isConnected()
{
return is_resource($this->db) && get_resource_type($this->db) == 'mysql link';
}
// Do we have a valid database connection and have we selected a database?
public function databaseSelected()
{
if (!$this->isConnected())
return false;
$result = mysql_list_tables($this->name, $this->db);
return is_resource($result);
}
public function connect()
{
$this->db = mysql_connect($this->host, $this->username, $this->password) or $this->notify('Failed connecting to the database with the supplied connection details. Please check the details are correct and your MySQL user has permissions to access this database.<br/><br/>(host: ' . $this->host . ', user: ' . $this->username . ', pass: ********)');
if ($this->db === false)
return false;
mysql_select_db($this->name, $this->db) or $this->notify();
if($this->isConnected())
{
mysql_set_charset('UTF-8', $this->db);
$this->query("SET NAMES utf8");
}
return $this->isConnected();
}
public function close()
{
if ($this->db)
{
mysql_close($this->db);
}
$this->db = false;
$this->queries = array();
}
public function query($sql, $args_to_prepare = null, $exception_on_missing_args = true)
{
if (!$this->isConnected())
$this->connect();
// Allow for prepared arguments. Example:
// query("SELECT * FROM table WHERE id = :id", array('id' => $some_val));
if (is_array($args_to_prepare))
{
foreach ($args_to_prepare as $name => $val)
{
$val = $this->quote($val);
$sql = str_replace(":$name", $val, $sql, $count);
if ($exception_on_missing_args && (0 == $count))
throw new Exception(":$name was not found in prepared SQL query.");
}
}
$this->queries[] = $sql;
$this->result = mysql_query($sql, $this->db) or $this->notify();
return $this->result;
}
// Returns the number of rows.
// You can pass in nothing, a string, or a db result
public function numRows($arg = null)
{
$result = $this->resulter($arg);
return ($result !== false) ? mysql_num_rows($result) : false;
}
// Returns true / false if the result has one or more rows
public function hasRows($arg = null)
{
$result = $this->resulter($arg);
return is_resource($result) && (mysql_num_rows($result) > 0);
}
// Returns the number of rows affected by the previous operation
public function affectedRows()
{
if (!$this->isConnected())
return false;
return mysql_affected_rows($this->db);
}
// Returns the auto increment ID generated by the previous insert statement
public function insertId()
{
if (!$this->isConnected())
return false;
$id = mysql_insert_id($this->db);
if ($id === 0 || $id === false)
return false;
else
return $id;
}
// Returns a single value.
// You can pass in nothing, a string, or a db result
public function getValue($arg = null)
{
$result = $this->resulter($arg);
return $this->hasRows($result) ? mysql_result($result, 0, 0) : false;
}
// Returns an array of the first value in each row.
// You can pass in nothing, a string, or a db result
public function getValues($arg = null)
{
$result = $this->resulter($arg);
if (!$this->hasRows($result))
return array();
$values = array();
mysql_data_seek($result, 0);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
$values[] = array_pop($row);
return $values;
}
// Returns the first row.
// You can pass in nothing, a string, or a db result
public function getRow($arg = null)
{
$result = $this->resulter($arg);
return $this->hasRows() ? mysql_fetch_array($result, MYSQL_ASSOC) : false;
}
// Returns an array of all the rows.
// You can pass in nothing, a string, or a db result
public function getRows($arg = null)
{
$result = $this->resulter($arg);
if (!$this->hasRows($result))
return array();
$rows = array();
mysql_data_seek($result, 0);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
$rows[] = $row;
return $rows;
}
// Escapes a value and wraps it in single quotes.
public function quote($var)
{
if (!$this->isConnected())
$this->connect();
return "'" . $this->escape($var) . "'";
}
// Escapes a value.
public function escape($var)
{
if (!$this->isConnected())
$this->connect();
return mysql_real_escape_string($var, $this->db);
}
public function numQueries()
{
return count($this->queries);
}
public function lastQuery()
{
if ($this->numQueries() > 0)
return $this->queries[$this->numQueries() - 1];
else
return false;
}
private function notify($err_msg = null)
{
if ($err_msg === null)
{
$err_msg = mysql_error($this->db);
}
error_log($err_msg);
if ($this->dieOnError === true)
{
echo "<p style='border:5px solid red;background-color:#fff;padding:12px;font-family: verdana, sans-serif;'><strong>Database Error:</strong><br/>$err_msg</p>";
if (strlen($this->lastQuery()))
{
echo "<p style='border:5px solid red;background-color:#fff;padding:12px;font-family: verdana, sans-serif;'><strong>Last Query:</strong><br/>" . $this->lastQuery() . "</p>";
}
//echo "<pre>";
//debug_print_backtrace();
//echo "</pre>";
exit;
}
if (is_string($this->redirect))
{
header("Location: {$this->redirect}");
exit;
}
}
// Takes nothing, a MySQL result, or a query string and returns
// the correspsonding MySQL result resource or false if none available.
private function resulter($arg = null)
{
if (is_null($arg) && is_resource($this->result))
return $this->result;
elseif (is_resource($arg))
return $arg;
elseif (is_string($arg))
{
$this->query($arg);
if (is_resource($this->result))
return $this->result;
else
return false;
}
else
return false;
}
}
Please help me thanks.
On this line 23 $this->host = $Config->"localhost";
localhost should be using single quotation marks
like this: $this->host = $Config->'localhost';

Resources