Template database population - atk4

Need to populate a template:
<div id="atk-layout" class="atk-wrapper">
<?$pastor?>
<?$missionstatement?>
<?$tmissionstatement?>
<div id="atk-footer-guard"></div>
</div>
In the Page I have this:
<?php
class View_Belfry extends View {
function init(){
parent::init();
q=$this->api->db->dsql();
$q->table('gum')->getAll();
foreach($q as $row){
switch ($row['sequence']) {
case 0:
$Pastor = $row['content'];
break;
case 1:
$MissionStatement = $row['content'];
break;
case 2:
$TMissionStatement = $row['content'];
break;
}
$this->template->set('Pastor',$Pastor); <<<<<
in the above line how do you add more than one Element
I either get a blank page or one Field displayed using different syntaxes
What is the syntax for multiple content tags in a template. Or am I missing some design feature? I get the first tag or a blank screen. The table field names don't match the template content names because the table field name "section" holds the name.
Like so:
Database table:
<?php
class Model_Gum extends Model_Table {
public $table="gum";
function init(){
parent::init();
$this->addField('section');
$this->addField('content')->type('text');
$this->addField('publish')->type('boolean');
$this->addField('sequence');
}
}
Thanks

You are trying to display array in View.
To display multiple content you need to use Lister, CompleteLister, Grid or CRUD class.
Also you are using dsql for no reason. It's more simple to use model.
$m = $this->add('Model_Gum');
Now add CompleteLister View (or other).
$l = $this->add('Your_Lister');
$l->setModel($m);
Create Your_Lister class witch extends on of the lister View and make necessary conditions inside
function formatRow(){
parent::formatRow();
}

Related

Prestashop - Product images not showing in category loop with friendly URLs

My product images don't show in categories after turning on friendly URLs. If I turn it off, they appear again.
I have tried :
Re-generating pictures in the back-office
Turning off cache
When I inspect the code with frienly URLs on, image links show in a weird format : https://www.website.fr/3689-home_default/.jpg
Prestashop 1.6
EDIT :
It appears the problem comes from the way I query products in my custom category TPL. I am querying products from subcategories manually (i am not displaying all products of the category at once) like this :
{foreach from=$subcategories item=subcategory}
{if $subcategory.id_category == 64659}
{assign var="subcategory_id" value=$subcategory.id_category}
{assign var="subcategory_object" value=$subcategories_objects.$subcategory_id}
{include file="./product-list.tpl" products=$subcategory_object->getProducts('1','1','100','price','asc')}
{/if}
{/foreach}
When I query products with the following line, images appear as normal :
{include file="./product-list.tpl" products=$products}
In the product-list.tpl, this is the line which gets the image :
src="{$link->getImageLink($product.link_rewrite, $product.id_image, 'home_default')|escape:'html':'UTF-8'}"
Thank you.
By default you will get array of subcategories only in $subcategories variable; so getting products of that sub category will never work $subcategory_object->getProducts('1','1','100','price','asc') until and unless you do have sub category object.
First you need to get sub category object and for that you need to override default function getSubCategories from classes/Category.php file by making override of Category class.
Follow below mentioned steps(1-2) to create override class and modify code in template mentioned in step(3)
1) Make file Category.php on path override\classes and add below code in it.
this will override default function to get category.
<?php
/**
* #override classes/Category.php
*
*/
class Category extends CategoryCore
{
/**
* #override
* Return current category childs
*
* #param int $id_lang Language ID
* #param bool $active return only active categories
* #return array Categories
*/
public function getSubCategories($id_lang, $active = true)
{
$result = parent::getSubCategories($id_lang, $active);
foreach ($result as &$row) {
// Preapre object of sub category here
$row['object'] = new Category($row['id_category'], $id_lang);
}
return $result;
}
}
2) Delete file class_index.php from cache folder.
Your override function is ready now.
3) Add below code to your template to show products
{foreach from=$subcategories item=subcategory}
{if $subcategory.id_category == 64659}
{include file="./product-list.tpl" products=$subcategory.object->getProducts('1','1','100','price','asc')}
{/if}
{/foreach}
Hope this will work for you.

Add a custom head title to a filtered view page in Drupal

I have a site that filters the blogs by specific expeditions.
Currently, when I click on the blog related to that specific expedition it displays the head title (in browser window) as "| mysite". So all the filtered views have the same head title.
I would like to add a custom head title for each filtered view.
So, for example, I would like the blogs that have do with Expedition 1 to have a filtered view with the head title "Expedition 1 blogs | Mysite".
Does anyone have any suggestions?
I suggest you do this :
for Views 3:
If you have a view and you want to be able to programmatically change the title of, you can do it by implementing hook_views_pre_render in your custom module:
<?php
/**
* Implements hook_views_pre_view().
*/
function MODULENAME_views_pre_render($view) {
if ($view->name == 'my_view_name') {
if ($view->current_display == 'my_display_name') {
$view->set_title('my new title');
}
}
}
?>
I hope it helps.
This question may be related to this one where the following solution was given:
In template.php:
function YOUR_THEME_preprocess_page(&$vars){
// You can test if you're in your specific views of course
$path = $_GET['q'];
if (strpos($path,'YOUR_PATH_STRING') !== false) {
drupal_set_title('YOUR_TITLE');
}
}
I also saw the reference to the Page Title module that could suit you.
You can set views page title programmatically by using below hook in modules.
function MODULE_NAME_views_pre_view(&$view, &$display_id, &$args) {
if($view->name == 'VIEW_MACHINE_NAME'){
$view->display[$view->current_display]->display_options["title"] =
$view->display[$view->current_display]->handler->options["title"] =
$view->human_name .' - '.$_GET['field_video_by_event_value'];
}
}

Grid formatter for uploaded file

I'd like to create a link in my grid to download uploaded files.
There is already several topics on it and the only solution that worked for me is: https://groups.google.com/forum/#!msg/agile-toolkit-devel/degomDwwe1s/gGtcap-T27sJ
But I'd like more of an abstract solution which could work whatever the field name is. So I searched for adding formatter : ATK4 How to set up custom formatters?
But it is never used...
I tried many things (v.4.2.5):
//in my model
$this->add('filestore\Field_File', 'file_id')
->display(array('form'=>'upload','grid'=>myField));
$this->hasOne('filestore\File', 'file_id', 'id')
->display(array('form'=>'upload', 'grid'=>'myField'));
//in filestore\FIELD_FILE
$this->display(array('form'=>'upload', 'grid'=>'myField'));
//my grid
class GridFile extends Grid
{
function format_myField($field)
{
$fm = $this->add('filestore/Model_File');
$fm->load($this->current_row[$field]);
$src = $fm->getPath();
$name = $fm->get('original_filename');
$this->current_row_html[$field] = "<a href='$src'>$name</a>";
}
}
Model:
$this->addField("ipv4")
->display(["grid"=>"foo"]);
Page:
$g=$this->add("CRUD", ["grid_class"=>"XGrid"]);
XGrid:
class XGrid extends Grid {
function format_foo($a,$b,$c=null){
$this->current_row_html[$a] = "<b>".$this->current_row[$a]."</b>";
}
}
Works like charm - perhaps your crud/grid not using the extended version. P.s. using latest atk version (master branch from git)

Styling/Theming Drupal 7 Content Type Record

I developed a Content type of "Car Sales" with following fields:
Manufacturer
Model
Make
Fuel Type
Transmission (Manual/Automatic)
Color
Registered? (Yes/No)
Mileage
Engine Power
Condition (New/Reconditioned/Used)
Price
Pictures (Multiple uploads)
I have developed View of this Content Type to display list of cars. Now I want to develop a screen/view for individual Car Sale Record like this:
Apart from arranging fields, please note that I want to embed a Picture Gallery in between. Can this be achieved through Drupal 7 Admin UI or do I need to create custom CSS and template files? If I need to edit certain template files/css, what are those? I'm using Zen Sub Theme.
I would accomplish this by creating a page, and then creating a node template to accompany it. Start by creating a new node, and then record the NID for the name of the template.
Then, in your template, create a new file, and name it in the following manner: node--[node id].tpl.php
Then, in that file, paste in the following helper function (or you can put it in template.php if you're going to use it elsewhere in your site):
/**
* Gets the resulting output of a view as an array of rows,
* each containing the rendered fields of the view
*/
function views_get_rendered_fields($name, $display_id = NULL) {
$args = func_get_args();
array_shift($args); // remove $name
if (count($args)) {
array_shift($args); // remove $display_id
}
$view = views_get_view($name);
if (is_object($view)) {
if (is_array($args)) {
$view->set_arguments($args);
}
if (is_string($display_id)) {
$view->set_display($display_id);
}
else {
$view->init_display();
}
$view->pre_execute();
$view->execute();
$view->render();
//dd($view->style_plugin);
return $view->style_plugin->rendered_fields;
} else {
return array();
}
}
Then add the following code to your template:
<?php
$cars = views_get_rendered_fields('view name', 'default', [...any arguments to be passed to the view]);
foreach ($cars as $car): ?>
<div>Put your mockup in here. It might be helpful to run <?php die('<pre>'.print_r($car, 1).'</pre>'); ?> to see what the $car array looks like.</div>
<?php endforeach;
?>
Just change the placeholders in the code to whatever you want the markup to be, and you should be set!
As I mentioned above, it's always helpful to do <?php die('<pre>'.print_r($car,1).'</pre>'); ?> to have a visual representation of what the array looks like printed.
I use views_get_rendered_fields all the time in my code because it allows me to completely customize the output of the view.
As a Reminder: Always clear your caches every time you create a new template.
Best of luck!

Adding a 1 to many file upload to CRUD

My app has sales listing functionality that will allow the user to add 1 or more photos for the product that they want to sell.
I'm attempting to use the upload/filestore_image of ATK with a Join table to create the relationship - my models:
class Model_Listing extends Model_Table {
public $entity_code='listing';
function init(){
parent::init();
$this->addField('name');
$this->addField('body')->type('text');
$this->addField('status');
$this->addField('showStatus')->calculated(true);
}
function calculate_showStatus(){
return ($this->status == 1) ? "Sold" : "For Sale" ;
}
}
class Model_listingimages extends Model_Table {
public $entity_code='listing_images';
function init(){
parent::init();
$this->addField('listing_id')->refModel('Model_Listing');
$this->addField('filestore_image_id')->refModel('Model_Filestore_Image');
}
}
In my page manager class I have added the file upload to the crud:
class page_manager extends Page {
function init(){
parent::init();
$tabs=$this->add('Tabs');
$s = $tabs->addTab('Sales')->add('CRUD');
$s->setModel('Listing',array('name','body','status'),array('name','status'));
if ($s->form) {
$f = $s->form;
$f->addField('upload','Add Photos')->setModel('Filestore_Image');
$f->add('FileGrid')->setModel('Filestore_Image');
}
}
}
My questions:
I am getting a "Unable to include FileGrid.php" error - I want the user to be able to see the images that they have uploaded and hoped that this would be the best way to do so - by adding the file grid to bottom of the form. - EDIT - ignore this question, I created a FileGrid class based on the code in the example link below - that fixed the issue.
How do I make the association between the CRUD form so that a submit will save the uploaded files and create entries in the join table?
I have installed the latest release of ATK4, added the 4 filestore tables to the db and referenced the following page in the documentation http://codepad.agiletoolkit.org/image
TIA
PG
By creating model based on Filestore_File
You need to specify a proper model. By proper I mean:
It must be extending Model_Filestore_File
It must have MasterField set to link it with your entry
In this case, however you must know the referenced ID when the images are being uploaded, so it won't work if you upload image before creating record. Just to give you idea the code would look
$mymodel=$this->add('Model_listingimages');
$mymodel->setMasterField('listing_id',$listing_id);
$upload_field->setModel($mymodel);
$upload_field->allowMultiple();
This way all the images uploaded through the field will automatically be associated with your listing. You will need to inherit model from Model_Filestore_File. The Model_Filestore_Image is a really great example which you can use. You should add related entity (join) and define fields in that table.
There is other way too:
By doing some extra work in linking images
When form is submitted, you can retrieve list of file IDs by simply getting them.
$form->get('add_photos')
Inside form submission handler you can perform some manual insertion into listingimages.
$form->onSubmit(function($form) uses($listing_id){
$photos = explode(',',$form->get('add_photos'));
$m=$form->add('Model_listingimages');
foreach($photos as $photo_id){
$m->unloadDdata()->set('listing_id',$listing_id)
->set('filestore_image_id',$photo_id)->update();
}
}); // I'm not sure if this will be called by CRUD, which has
// it's own form submit handler, but give it a try.
You must be careful, through, if you use global model inside the upload field without restrictions, then user can access or delete images uploaded by other users. If you use file model with MVCGrid you should see what files they can theoretically get access to. That's normal and that's why I recommend using the first method described above.
NOTE: you should not use spaces in file name, 2nd argument to addField, it breaks javascript.

Resources