In my module config, I have a button. When that button is clicked I pop up a window that gathers some more input. That file is just an html file, but where should it live in the directory structure of the module?
To give a bit more info - just to see something working, I defined my field as follows:
<button_url_test_window_open><![CDATA[/px.html]]></button_url_test_window_open>
<frontend_model>mymodule/adminhtml_system_config_testWindowOpenDialog</frontend_model>
and I put the px.html file in my htdocs/magento folder. When I click the button it results in /px.html being opened, but that doesn't seem right. I'm not sure how to word the question, but I feel I should be doing something more like 'open the file called px.html for mymodule' and magento would then look in the right place. Sorry about the terminology, I'm still getting to grips with Magento/PHP/Apache.
Just to complete the picture of what I currently have, the frontend_model block is:
protected function _prepareLayout()
{
parent::_prepareLayout();
if (!$this->getTemplate()) {
$this->setTemplate('mypackage/system/config/test_window_open_dialog.phtml');
}
return $this;
}
public function render(Varien_Data_Form_Element_Abstract $element)
{
$element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
return parent::render($element);
}
protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
{
$originalData = $element->getOriginalData();
$this->addData(array(
'button_label_test_window_open' => Mage::helper('mymodule')->__($originalData['button_label_test_window_open']),
'button_url_test_window_open' => $originalData['button_url_test_window_open'],
'html_id' => $element->getHtmlId(),
));
return $this->_toHtml();
}
and the test_window_open_dialog.phtml file contains:
<table>
<tr>
<td>
<button style="" onclick="javascript:window.open('<?php echo $this->getButtonUrlTestWindowOpen()?>', 'testing','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, ,left=100, top=100, width=600, height=470'); return false;" class="scalable" type="button" id="<?php echo $this->getHtmlId() ?>">
<span><?php echo $this->escapeHtml($this->getButtonLabelTestWindowOpen()); ?></span>
</button>
</td>
</tr>
</table>
phtml files don't go anywhere within your module directory. Module directories house your Blocks, Helpers, Models, Controllers, Sql installation/upgrade files, and your configuration xml files. Template files go in either app/design/adminhtml (if it is an admin template), or in app/design/frontend if it will be used on the frontend of the site.
Related
I have a user controller with an agentexport function, which is supposed to download an excell spreadsheet. Below is the function:
function agentexport($agentName) {
if($this->isAdmin() == TRUE) {
$this->loadThis();
}
else {
$this->excel->setActiveSheetIndex(0);
// Gets all the data using agent name
$data = $this->excel_model->getdatabyname($agentname);
//print_r($data);
//die;
$this->excel->stream('crosstown.xls', $data);
}
}
In my views I am trying to execute the above function with the following button:
<a class="btn btn-sm btn-info" href="<?php echo base_url().'agentexport/'.$record->agentName; ?>" title="Download Sheet><i class="fa fa-pencil"></i>Download Sheet</a>
The above button is meant to download the spreadsheet right away.
The url is defined in my routes as :
$route['agentexport'] = "user/agentexport";
Did I define my route the right way ? When I click on the route I get the following url
http://www.XXXXX.com/John%20Grisham.
As you can see, the name is appended at the end of the url but the page shows a 404. What am I doing wrong ?
Personal opinion here, but I don't think there's any strong reason to use a route. If nothing else, the following will be a good experiment to see if the $route definition is the problem.
Delete the $route you have been using for 'agentexport'.
Change the link to
<a class="btn btn-sm btn-info" href="<?= base_url('user/agentexport/'.$record->agentName); ?>" title="Download Sheet"><i class="fa fa-pencil"></i>Download Sheet</a>
To test that the link works and is passing the value, use the following version of agentexport
public function agentexport($agentName)
{
echo $agentName;
//or alternately
//var_dump($agentName);
}
It is assumed that you verified $agentName is a usable value before you used it in the link. If the above shows you a value, then you know the $route was the problem.
You can experiment to find a $route, but $route['agentexport/(:any)'] = 'user/agentexport/$1'; should work. If you're going to switch back to using a route don't forget to revert the link code. I'd write it like this, where the URI is passed as an argument to base_url.
<a class="btn btn-sm btn-info" href="<?= base_url('agentexport/'.$record->agentName); ?>" title="Download Sheet"><i class="fa fa-pencil"></i>Download Sheet</a>
If you find a route that works - and using a route is what you really, really want - then restore the code in agentexport to what you actually need. But again, I don't see any strong reason to obfuscate the link's URL.
If, from the view, you're pointing to /controller_name/method/agent_name (which is basically what you're doing after the route "translation", all you need to do is pick the agent name from the URI using the URL helper (remember to load it beforehand)
$user_id = $this->uri->segment(3);
The above will take whatever is in the third segment of the URI (agent_name in my example) and assign it to the $user_id variable.
Remember that whatever is possible to be manipulated by the user cannot be trusted, so you need to sanitize $user_id and make sure that the user requesting the file is allowed to access it
I'm building a small app in CI. I'm using a pre-made template with a separate menu file. That menu file gets included in the pageview:
<?php include('include/sidebar.php'); ?>
Now I want to make the items in the menu dynamic based on the user permissions. In my sidebar.php I define the menu items like this:
<?php
$classname = "logs";
if (check_class($classname) == true){
?>
<li id="<? echo $classname;?>" class="<?php if($this->uri->segment(1)==$classname){echo "active";}?>">
<a href="javascript:void(0);" class="menu-toggle">
<i class="material-icons">youtube_searched_for</i>
<span><?php echo $this->lang->line('menu_logs') ?></span>
</a>
<ul class="ml-menu">
<?php
$methodname = "viewlogs";
if (check_method($classname,$methodname) == true){
?>
<li id="<? echo $classname;?>" class="<?php if($this->uri->segment(1)==$classname AND $this->uri->segment(2)==$methodname){echo "active";}?>">
<?php echo $this->lang->line('menu_logs') ?>
</li>
<?php }?>
</ul>
</li>
<?php }?>
check_class and check_method are currently included in the sidebar.php file as well:
<?php
// This should not be here...
global $thisglobal;
$thisglobal = $this;
global $auth_roleglobal;
$auth_roleglobal = $auth_role;
function check_class($class) {
global $thisglobal;
//Override if admin
if ($thisglobal->auth_role == "admin") {
return true;
}
// Get current roles permissions
$role_arr_flipped = array_flip(array($thisglobal->auth_role)); // Avoid Error # Only variables should be passed by reference
$role_arr_intersected = array_intersect_key($thisglobal->config->item('user_role_permissions'), $role_arr_flipped);
$role_perms = array_shift($role_arr_intersected);
if (array_key_exists($class, $role_perms)) {
return true;
} else {
return false;
}
}
function check_method($class,$method) {
global $thisglobal;
//Override if admin
if ($thisglobal->auth_role == "admin") {
return true;
}
// Get current roles permissions
$role_arr_flipped = array_flip(array($thisglobal->auth_role)); // Avoid Error # Only variables should be passed by reference
$role_arr_intersected = array_intersect_key($thisglobal->config->item('user_role_permissions'), $role_arr_flipped);
$role_perms = array_shift($role_arr_intersected);
// Flip arrary
$role_perms["$class"] = array_flip($role_perms["$class"]);
if (array_key_exists($method, $role_perms["$class"])) {
return true;
} else {
return false;
}
}
?>
This works, but obviously including those functions into the view file is against the MVC approach, and I might want to reuse check_class and check_method in other views as well. I have moved those functions to my_controller, but again I should not call those functions from my view.
I am kinda lost in how to continue...
The sidebar does not have its own controller. Should I create a separate one? But then how do I load it because I can't (should not) call the menu controller from the page view.
Or should I call the check_class and check_method before loading the view, but I don't know yet which menu items I should check at that point.
Thanks!
I would create a library called Menu.php in there I would create functions that would check the user permissions and stuff, and also have a render method that will just output the menu.
This way your controller would load that library. Send some data into it and get the menu as a string. Then you just send that string to the view and echo it.
Other option would be looking into a presenter pattern and try to implement that in codeigniter.
Presenter pattern
Presenter library for ci
I just make a simple html page and write content to it. I created a link when I click on that link the content of the page will converted into PDF file, I have implemented this using laravel dompdf -> https://github.com/barryvdh/laravel-dompdf . Now, My problem is that all things doing good, but the PDF that is generated after click will shows that link in the PDF file. How to remove that button from PDF file.
My code :
My controller : ItemController.php
class ItemController extends Controller
{
public function pdfview(Request $request)
{
if($request->has('download')){
$pdf = \PDF::loadView('pdfview');
return $pdf->download('pdfview.pdf');
//return $pdf->stream();
}
return view('pdfview');
}
}
My route: web.php
Route::get('/pdfview',array('as'=>'pdfview','uses'=>'ItemController#pdfview'));
My view : pdfview.blade.php
<body>
<div class="container">
<div class="rows"><br/>
Click to PDF
<h2 align="Center"> What is PDF ? </h2><br/><br/>
<h4> Portable Document Format (PDF) is a file format used to present and exchange documents reliably, independent of software, hardware, or operating system.</h4>
</div>
</div>
</body>
Help me, If you understand my problem. Thanks
Pass a variable to the view, i.e:
$pdf = \PDF::loadView('pdfview', array('pdf' => true));
...
return view('pdfview', array('pdf' => false));
and check the variable in the template:
#if(! $pdf )
Click to PDF
#endif
I'm excited that the October CMS recently added back-end functionality for sorting records in the list view. But I'm having some trouble getting it to work. The documentation is here. I've followed the direction like so:
In my controller, I implemented the ReorderController:
<?PHP namespace BTruchan\Team\Controllers;
use Backend;
use BackendMenu;
use BackendAuth;
use Backend\Classes\Controller;
use System\Classes\SettingsManager;
class Members extends \Backend\Classes\Controller
{
public $implement = [
'Backend.Behaviors.FormController',
'Backend.Behaviors.ListController',
'Backend.Behaviors.ReorderController'
];
public $formConfig = 'config_form.yaml';
public $listConfig = 'config_list.yaml';
public $reorderConfig = 'config_reorder.yaml';
public $requiredPermissions = ['btruchan.team.manage'];
public function __construct()
{
parent::__construct();
BackendMenu::setContext('BTruchan.Team', 'team');
}
public function index()
{
$this->makeLists();
$this->makeView('reorder');
}
}
?>
I've created the reorder view file (reorder.htm) which contains:
<?= $this->reorderRender() ?>
My config_reorder.yaml file contains:
# ===================================
# Reorder Behavior Config
# ===================================
# Reorder Title
title: Reorder Members
# Attribute name
nameFrom: name
# Model Class name
modelClass: BTruchan\Team\Models\Members
# Toolbar widget configuration
#toolbar:
# Partial for toolbar buttons
# buttons: reorder_toolbar
You'll notice that the reorder_toolbar partial is commented out. That's because I really don't know what's supposed to go in that toolbar. I haven't been able to find any documentation that shows the contents for the _reorder_toolbar.htm file.
Unsurprisingly, with the code commented out, it throws an error:
Undefined variable: reorderToolbarWidget
Some additional information:
It was suggested that I read up on list toolbars here.
So I added the following toolbar partial (named _reorder_toolbar.htm):
<div data-control="toolbar">
<a
href="<?= Backend::url('btruchan/team/members/create') ?>"
class="btn btn-primary oc-icon-plus">
New Team Member
</a>
<button
class="btn btn-default oc-icon-trash-o"
disabled="disabled"
onclick="$(this).data('request-data', {
checked: $('.control-list').listWidget('getChecked')
})"
data-request="onDelete"
data-request-confirm="Delete Team Member: Are you sure?"
data-trigger-action="enable"
data-trigger=".control-list input[type=checkbox]"
data-trigger-condition="checked"
data-request-success="$(this).prop('disabled', false)"
data-stripe-load-indicator>
Delete
</button>
</div>
But I'm still getting an error:
Undefined variable: reorderToolbarWidget
/var/www/terrasearch/public/modules/backend/Behaviors/reordercontroller/partials/_container.htm
line 1
The code, in October CMS, which that error message is referring is:
<?php if ($reorderToolbarWidget): ?>
<!-- Reorder Toolbar -->
<div id="<?= $this->getId('reorderToolbar') ?>" class="reorder-toolbar">
<?= $reorderToolbarWidget->render() ?>
</div>
<?php endif ?>
<!-- Reorder List -->
<?= Form::open() ?>
<div
id="reorderTreeList"
class="control-treelist"
data-control="treelist"
I've tried to trace this error down. It seems like, in \public\modules\backend\behaviors\ReorderController.php, the reorder() function is not being called, which means that the prepareVars() function is also not being called. This prevents the following code from being executed:
$this->vars['reorderToolbarWidget'] = $this->toolbarWidget;
ReorderController.php:: makeToolbarWidget() is being called and seems to be OK. I've checked $this->toolbarWidget, and it seems to contain perfectly good data. (It isn't NULL).
The ReorderController is a behavior, so it's meant to be called as a controller destination (e.g. example.com/backend/btruchan/team/members/reorder). It's not coded to be called as a view the way you have it in your index function.
In the ReorderController source, the reorder function is the only method that calls the prepareVars protected function which is the only place that the reorderToolbarWidget is defined for the page. That prepareVars function isn't available from the host controller.
So, rather than try to create a view with $this->makeView('reorder');, create a toolbar button in the _list_toolbar.htm partial that points to the reorder destination url. For example:
<div data-control="toolbar">
New Member
Reorder Members
</div>
When you click on the "Reorder Members" button, you'll be directed to a new page with the records that can be reordered.
You can use the _reorder_toolbar.htm partial to add anything you want at the top of the reorder page. Or, not use it at all.
I created a confirmation page for deleting an item. This works perfectly.
Now I want to appear the confirmation page in fancybox, still passing all the variables and delete the data or close the dialog.
It's important that the process still works as it does now (delete->confirmationpage->redirect/deletion) so that users with javascript disabled can perform these actions without a problem.
Now have I been reading about zend framework,ajax, json and more, but the more I read, the less I understand.
So in a nutshell, my question:
I want to pass a variable to the fancybox and if 'yes' perform the delete action or on 'no' close the dialog. This within the zend framework and jquery.
Any advice or tips are greatly appreciated!
You need to use content switching in your ajax which will then render your action appropriately, for example:
function confirmDeleteAction() {
if($this->_request->isXmlHttpRequest()) {
//This is ajax so we want to disable the layout
$this->_helper->layout->disableLayout();
//Think about whether you want to use a different view here using: viewRenderer('yourview')
}
//The normal code can go here
}
function deleteAction() {
//You probably don't want to show anything here, just do the delete logic and redirect therefore you don't need to worry where it's coming from
}
And in your fancybox, have a view that has a form and two buttons, the form should point to your delete action with the id of whatever your deleting as a get param. Set some javascript up that says something like (and this is mootools code, but you can convert it easily):
$('confirmButton').addEvent('click', function(e) {
e.preventDefault();
this.getParent('form').submit();
}
$('cancelButton').addEvent('click', function(e) {
e.preventDefault();
$('fancyBox').destroy(); //I'm not sure it has an id of fancyBox, never used it
}
Came across the question today and figured I could give it a fresh look. For Zend Framework the solution is really simple:
This is how the action looks:
public function deleteAction()
{
$this->_helper->layout()->disableLayout();
$this->view->news = $this->newsService->GetNews($this->_getParam('id'));
if($this->getRequest()->isPost())
{
if($this->getRequest()->getPost('delete') == 'Yes')
{
$this->newsService->DeleteNews($this->_getParam('id'), $this->view->user->username, $this->view->translate('deleted: ').'<strong>'.$this->view->pages[0]['title'].'</strong>');
$this->_helper->flashMessenger(array('message' => $this->view->translate('The page is deleted'), 'status' => 'success'));
$this->_helper->redirectToIndex();
}
elseif($this->getRequest()->getPost('delete') == 'No')
{
$this->_helper->flashMessenger(array('message' => $this->view->translate('The page is <u>not</u> deleted'), 'status' => 'notice'));
$this->_helper->redirectToIndex();
}
}
}
The delete.phtml
<div>
<h2><?php echo $this->translate('Delete news'); ?></h2>
<?php
foreach($this->news as $news)
{
?>
<p>
<?php echo $this->translate('You are now deleting <strong>\'').$news['title'].$this->translate('\'</strong>. Are you sure about this?'); ?>
</p>
<p>
<?php echo $this->translate('<strong>Note! </strong>This action is inreversable, even for us!'); ?>
</p>
<form action="<?php echo $this->baseUrl(false).'/news/index/delete/'.$news['id']; ?>" method="post">
<?php
}
?>
<input class="submit deleteYes" type="submit" name="delete" value="<?php echo $this->translate('Yes'); ?>" />
<input class="submit deleteNo" type="submit" name="delete" value="<?php echo $this->translate('No'); ?>" />
</form>
And this is how the link to delete a file looks (within a foreach loop with my database results)
<a class="deleteConfirmation" href="<?php echo $this->baseUrl(false).'/news/index/delete/'.$news->id; ?>">delete</a>
This works like you would expect; when you click on delete the user goes to the delete confirmation page and redirects the user back to the index after the form is submitted. But I wanted the confirmation in a dialog (in my case I use fancybox). To achieve this, add the following jquery to your index:
$('.deleteConfirmation').fancybox({
// Normal fancybox parameters
ajax : {
type : "POST"
}
});