Preconfigured Magento Widgets - magento

Is there a UI or programatic system for taking advantage of the "Preconfigured Widget" functionality that's part of Magento's CMS page rendering?
When adding a widget to a CMS page, the code that renders that widget is located in the template directive processing class. This code
File: app/code/core/Mage/Widget/Model/Template/Filter.php
class Mage_Adminhtml_Cms_PageController extends Mage_Adminhtml_Controller_Action
{
...
}
When loading a Widget's paramaters, there the following bit of code
// validate required parameter type or id
if (!empty($params['type'])) {
$type = $params['type'];
} elseif (!empty($params['id'])) {
$preconfigured = Mage::getResourceSingleton('widget/widget')
->loadPreconfiguredWidget($params['id']);
$type = $preconfigured['type'];
$params = $preconfigured['parameters'];
} else {
return '';
}
This code appears to parse a widget directive tag for an id value
{{widget name="foobazbar" id=""}}
and then load the configuration from a widget model
public function loadPreconfiguredWidget($widgetId)
{
$read = $this->_getReadAdapter();
$select = $read->select();
$select->from($this->getMainTable())
->where($this->getIdFieldName() . ' = ?', $widgetId);
var_dump((string)$select);
$widget = $read->fetchRow($select);
if (is_array($widget)) {
if ($widget['parameters']) {
$widget['parameters'] = unserialize($widget['parameters']);
}
return $widget;
}
return false;
}
When I first encountered this code, I assumed it was loading up a Widget Instance model. However, it's not. Instead it's loading data from a widget/widget class, which corresponds to the widget table.
mysql> describe widget;
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| widget_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| code | varchar(255) | NO | MUL | NULL | |
| type | varchar(255) | NO | | NULL | |
| parameters | text | YES | | NULL | |
+------------+------------------+------+-----+---------+----------------+
Is there a UI or system for adding data to this table? Does anyone (who works fro Magento Inc. or not) know if this is a supported feature, or if it's the start of something that's been abandoned, but left in for backward compatibility reasons?

This answer is somewhat off-topic, but I'm not sure if it might satisfy your need anyway. I've discovered that you can create widget instances in the admin CMS>Widgets section, and then render them via the following code:
$oWidget = Mage::getModel('widget/widget_instance')->load('HomepageTwitter','title');
$oWidgetBlock = Mage::app()->getLayout()->createBlock($oWidget->getType(), $oWidget->getTitle(), $oWidget->getWidgetParameters());
echo $oWidgetBlock->toHtml();
Note that the block is loaded by title (rather than arbitrary ID), and that the widget parameters are passed for the Block to render.

Per several comments, and private emails, it appears this is a privatish feature for the Magento core team, and has nothing to do with Instance Widgets.

Related

Laravel: Merge two query builders

I have a table of courses which will be free to access or an admin will need to click something to let users see the course.
The course table looks like this:
| id | title | invite_only |
|----|----------------|-------------|
| 1 | free course | 0 |
| 2 | private course | 1 |
Separate from this I have a course_user table, where initially users request access, then admins can approve or deny access:
| id | user_id | course_id | approved | declined |
|----|---------|-----------|----------|----------|
| 1 | 3 | 2 | 1 | 0 |
| 2 | 4 | 1 | 0 | 1 |
| 3 | 4 | 2 | 0 | 0 |
I'd like to index all the courses a user has access to:
class User extends model{
public function myCourses(){
$public = $this->publicCourses;
$invited = $this->invitedCourses;
return $public->merge($invited);
}
public function publicCourses(){
return $this
->hasMany('App\Course')
->where('invite_only', false);
}
public function invitedCourses(){
return $this
->belongsToMany("\App\Course")
->using('App\CourseUser')
->wherePivot('approved', 1);
}
}
How can I make the myCourses function return the results of both publicCourses and invitedCourses by doing only one database query? I'd like to merge the two query builder instances.
According to the doc, you can use union to merge query builders. But as far as I know, it does not work with relations. So maybe you should do it from within controller instead of model. This is an example based on what I understand from your example:
$q1 = App\Course::join('course_user', 'course_user.course_id', 'courses.id')
->join('users', 'users.id', 'course_user.user_id')
->where('courses.invite_only', 0)
->select('courses.*');
$q2 = App\Course::join('course_user', 'course_user.course_id', 'courses.id')
->join('users', 'users.id', 'course_user.user_id')
->where('courses.invite_only', 1)
->where('course_user.approvoed', 1)
->select('courses.*');
$myCourses = $q1->unionAll($q2)->get();
You can also refactor the code further by creating a join scope in App\Course.
I was able to make a much simpler query, and use Laravel's orWherePivot to extract the correct courses:
public function enrolledCourses()
{
return $this
->courses()
->where('invitation_only', false)
->orWherePivot('approved', true);
}

Doctrine boolean type can't be set to false

I can't set false value to an entity column which type is boolean.
/**
* #ORM\Column(type="boolean")
*/
private $isActive;
Sending JSON:
{myEntity: {isActive: false}}
...will cause:
Integrity constraint violation: 1048 Column 'is_active' cannot be null
While sending:
{myEntity: {isActive: 0}}
...will work fine
There are some similar answers on stackoverflow however none of the solution worked.
RAW COLUMN
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| is_active | tinyint(1) | NO | | NULL | |
+-------------+--------------+------+-----+---------+-----------------+
EDIT
Read my answer below. It's not a Doctrine bug but MariaDB 10.2 serie.
This is unfortunately not a solution to the main problem, however it will give some insight.
The problem translating false to null occurs with MariaDB 10.2 serie (did not test 10.0 serie).
The described problem does not occur on MySQL or MariaDB 5.5 serie and it transforms false to false in a correct way.
Going to make another issue specifically about MariaDB 10.2 bug.
Alternatively, you should evaluate doing:
/**
* #ORM\Column(type="boolean", nullable=true)
*/
private $isActive;
You will not always have the chance of and/or will want to initialize a value in the class.
UPDATE:
I realize that my answer wasn't the right one. Maybe you could initialize the property in the constructor, or call the setter automatically. In any case, you have to "deceive" Doctrine's Unit of Work that your entity has changed.

How can I filter a query using laravel relationships

I have a table of items that contain info of the items. Plus, I have 3 other tables for some characteristics that could have more than one.
Follows an example:
table items
-----------------------
| id | price | stock |
-----------------------
| 1 | 19 | 99 |
-----------------------
table tallas
-----------------------------
| id | item_id| description |
-----------------------------
| 1 | 1 | large |
-----------------------------
table colors
-----------------------------
| id | item_id| description |
-----------------------------
| 1 | 1 | blue |
-----------------------------
table materials
-----------------------------
| id | item_id| description |
-----------------------------
| 1 | 1 | cotton |
-----------------------------
I want to know if there is a way where I can filter an item passing the item_id to the tallas relationship without using join (for example).
My model looks like this:
<?php
class Item extends Eloquent{
use Conner\Tagging\TaggableTrait;
public function tallas()
{
return $this->hasMany('Talla','item_id');
}
public function colores()
{
return $this->hasMany('Color','item_id');
}
public function materiales()
{
return $this->hasMany('Material','item_id');
}
public function imagenes()
{
return $this->hasMany('ItemImage','item_id');
}
}
And I have tried this:
$items = Item::with('imagenes')
->with(array('tallas' => function($query) use($dataFilter) {
$query->where('id','=',$dataFilter['filter-size']);
}));
But this return all the items and filter the tallas , using the example tables if i look for an item small it will return something like this.
[item]{
id:1,
price:19,
stock:99,
tallas:
[]...
colores:
[]...
materiales:
[]...
}
It should not return any item, any help?
EDITED
i forget to mention i'm using laravel 4.2
If I understand you question correctly I think you want whereHas e.g.
$items = Item::with('imagenes')
->whereHas('tallas', function ($query) use ($dataFilter) {
$query->where('id', '=', $dataFilter['filter-size']);
});
https://laravel.com/docs/4.2/eloquent#querying-relations
Hope this helps!

Cakephp 3: belongsToMany contain or matching condition?

Piece of my database looks like database part
Categories use tree behavior.
How can i get a manufacturer's (Producers) Products for current Category?
I tried contain and matching, but i received duplicated data or Producers names without related Products.
EDIT:
$query = $this->Producers->find()->matching('Products.Categories',
function ($q) {
return $q->where(['Categories.id' => 18]);
}
);
Results:
Producent: Canon
-------------------------------------------
| ID | Name | Barcode |
-------------------------------------------
| 1 | EOS 1000D | |
-------------------------------------------
| 18 | Camera | |
-------------------------------------------
| 23 | 18 | |
-------------------------------------------
First row (id = 1) it's what i need.
Now i have to remove from results:
second row (id = 18) this is Category id from table Categories,
thrid row (id = 23) - from Products_Categories table.
Done. There is working query:
$query = $this->Producers->find()
->select(['Producers.id','Producers.name', 'Products.id', 'Products.name'])
->matching(
'Products.Categories', function ($q) use ($categoryId){
return $q->where(['Categories.id' => $categoryId]);
}
);

ManyToMany relation - how update attribute in pivot table

I am now learning to work with pivot tables: https://laravel.com/docs/4.2/eloquent#working-with-pivot-tables
I have WeeklyRoutine model. Each routine has several Activities. The assigned activities are attached in a pivot table activity_routine.
Relation defined in the WeeklyRoutine model:
return $this->belongsToMany('App\Models\Activity', 'activity_routine', 'routine_id', 'activity_id')->withPivot('done_at')->withTimestamps();
}
it looks like this:
// activity_routine pivot table (relevant columns only)
| id | activity_id | routine_id | done_at |
| 34 | 1 | 4 | 2016-04-23 09:27:27 | // *1
| 35 | 2 | 4 | null | // *2
*1 this activity is marked as done with the code below
*2 this activity is not yet done
what I have:
I can update the done_at field in the pivot table, thus making it marked as DONE for the given week (a weeklyroutine_id = 4 in the above code
public function make_an_activity_complete($routineid, $activityid) {
$date = new \DateTime;
$object = Routine::find($routineid)->activities()->updateExistingPivot($activityid, array('done_at' => $date));
return 'done!';
}
what I need
I want to UN-DO an activity. When it is already done, that is when the done_at is not null buc contains a date, make it null.
In other words I need to do the below switch of value, but the proper way:
$pivot = DB::table('activity_routine')->where('routine_id, $routineid)->where('activity_id, $activityid)->first();
if($pivot->done_at != null) {
$new_val = new \DateTime;
} else {
$new_val = null;
}
$object = Routine::find($routineid)->activities()->updateExistingPivot($activityid, array('done_at' => $new_val));
How to do it? I have no clue!
Thx.
Your approach seems fine to me. I would probably do it like this.
$routine = Routine::find($routineid);
$activity = $routine->activities()->find($activityid);
$done_at = is_null($activity->pivot->done_at) ? new \DateTime : null;
$routine->activities()->updateExistingPivot($activityid, compact('done_at'));

Resources