Codeigniter creating css_url() and reaching assets in view - codeigniter

I added new function which is called css_url. I set in constructor of controller, It works.
Besides, I want to reach in assets in view. In view directory I put themes, and each themes has own assets. How can I reach assets in view directory?
-view
--theme1
---assets
----css
----js
----images
-----f1.jpg
->
public function __construct() {
parent::__construct();
// Temel veriler veri tabanından çekilecek;
$this -> theme = "theme1";
$this->config->set_item('css_url', $this->theme.'/assets/');
}
css_url("images/f1.jpg");

Try something like this:
$this->config->set_item('css_url', APPLICATION_PATH . '/DIFFERENT_DEPENDING_ON_VERSION_OF_CI/'.$this -> theme.'/assets/');
The differences in codeingiter versions are because of where the application path is. It could be /system/application, it could be just /application/

Related

Laravel Nova. Using Image field, is it possible to make "Download" link appear on "update/edit" views?

"Download" link like that appears only on images on view page. Is it possible to also get it on "update/edit" page?
It seems to be impossible using out of the box Nova field methods.
Is making a custom tool the proper way to achieve this?
You can do it.
public function fields(Request $request)
{
$href = $this->modelsHref; // Or however you want to build the link
return [
Text::make('Download Link')->asHtml(function () use ($href) {
return '<i class="icon"></i> Download';
})->onlyOnForms(),
];
}

Laravel check session on every page

I'm using Laravel 4 for my website, and would like to check on every page load if user has seen a popup, and if not - show the popup to the user.
I don't want to do that in every controller, is there a place where can I put the code, so it's checked before every page is loaded?
You can create a filter to check if the popup is shown.
// app/filters.php
Route::filter('popup.shown', function()
{
// Your check logic here
});
Then, you could use that filter in your routes, controllers or a base controller which you could extend to have this functionality:
class PopupController extends BaseController {
public function __construct()
{
$this->beforeFilter('popup.shown');
}
}
Then, extend this controller:
class MyController extends PopupController {
public funcion getIndex()
{
// this will run the `popup.shown` filter
}
}
You can read more about beforeFilter() here:
http://laravel.com/docs/controllers#controller-filters
Another approach would be to use the App::before() event, so the check would be done on every request. However, I don't recommend this one as it's not flexible and you will have to modify it soon or later.
I would approach this via a view/template style. Although if you use a lot of templates that don't show this popup then maybe the filter method suggested by Manuel Pedrera is best.
In global.php or some bootstrap file you can set a view variable that is injected automatically. http://laravel.com/docs/responses#views
// Perform popup logic
View::share('showPopup', $logicResult);
And then in the various layouts you want to include this popup you can check for the variable within the view. http://laravel.com/docs/templates
#if ($showPopup)
<div class="popup">Popup</div>
#endif
The advantage of this is that you do not have to include the variable to show/hide the popup for every View::make() call.

Joomla 2.5 custom mvc component: different form for task add

I'm developing a custom mvc component for Joomla 2.5.
For my admin section I included a "New" button in the toolbar.
By clicking this button I want to display a separate form with less
fields. So far everything works out but the buttons of the view's
toolbar won't redirect (i.e. 'Cancel')
To redirect the task 'add' I added an override of the method "add()"
to my controller:
class ArchitectProjectControllerArchitectProject extends JControllerForm
{
public function add()
{
JRequest::setVar('layout', 'add');
$result = parent::add();
return $result;
}
}
I don't know if I'm on the right track. Probably someone can give me a hint?
If you wish to redirect for example on 'Cancel' then try the following in the controller:
public function cancel()
{
$this->setRedirect('index.php?option='.$this->option.'&view=yourview');
}

No Delete button when editing an item in Magento Grid

I've successfully created my first admin grid. It works fine when adding a new item. However, when I want to edit an item, strangely, the DELETE button is missing. Even worse, when I click the SAVE button, it creates a new item instead of saving the current item. What have I done wrong?
I'm not sure what file to look at, thus I don't know which code must be posted here. If I put all the code from all files, it will be too much. Please advise.
Update: Not sure why, but now the SAVE button doesn't recreate any more. So I can edit an item peacefully. However, the DELETE button is still missing.
Late in the game, but hopefully this will help a Googler.
Is your _objectId property correctly set to the corresponding parameter in the URL?
$this->_objectId = 'id';
Where ID refers to this part of the URL:
/module/adminhtml_controller/edit/id/1/
See the parent class Mage_Adminhtml_Block_Widget_Form_Container::__construct:
...
$objId = $this->getRequest()->getParam($this->_objectId);
if (! empty($objId)) {
$this->_addButton('delete', array(
'label' => Mage::helper('adminhtml')->__('Delete'),
'class' => 'delete',
'onclick' => 'deleteConfirm(\''. Mage::helper('adminhtml')->__('Are you sure you want to do this?')
.'\', \'' . $this->getDeleteUrl() . '\')',
));
}
...
We can see that if $objId is empty, no delete button will be added.
After struggling around, the culprit was because I put parent::__construct(); at the top (right after the __construct() method. Instead, it should be put AFTER the $this->_controller line. Geezz...
***Remove or rename add new save continue delete button from magento admin
If you don’t want to show the ‘Add New’ button in the Grid. The Add New button is present in top right corner of Grid Page.
Rename ‘Add New’ button
Here are the steps to rename the ‘Add New’ text to anything you required (for example, ‘Add Report’):-
Go to YourNamespace -> YourModule -> Block -> Adminhtml -> YourFile.phenter code herep
Add the following code in the constructor of this file:-
$this->addButtonLabel = Mage::helper('yourmodulename')->_('Add Report');
Remove ‘Add New’ button
Here are the steps to remove the ‘Add New’ button:-
Go to YourNamespace -> YourModule -> Block -> Adminhtml -> YourFile.php
Add the following code in the constructor of this file (it should be just below the call to parent constructor):-
parent::__construct();
$this->_removeButton('add');
In edit.php
parent::__construct();
$this->_removeButton('delete');
$this->_removeButton('save');
$this->_removeButton('back');
in grid.php
parent::__construct();
$this->_removeButton('add');
Try on your Grid.php:
public function getRowUrl($row){
return $this->getUrl('*/*/form', array('id' => $row->getId()));
}

Codeigniter: Is calling a model from a helpler a bad idea?

I have a menu on my page that is loaded on every page. The menu data is fetched from the database through a model. My first thought was to call the model from the controller, and then pass it to the view on every page. But that results in "messy" code, if i ever forget to pass the menu data to the view, it will result in an ugly error (or just no menu items).
So i came up with the solution of fetching the menu items through a helper, and then just call the helper function from the view. It makes more sense, because i only have the code in one place (the menu view).
My views are set up in this way: Controller calls "page" view which then loads the header view, menu view, the appropriate content view, and lastly the footer view. The helper is only called from one place, the menu view.
Normally you can't even load models from helpers, but i did a workaround using $i = get_instance(); and then loading the model through that instance; $i->load->model().
I have a feeling this is not the way to go, but are there any better ways?
Edit: To put it in a better way:
I want:
view -> get data -> display
not:
controller -> get data -> pass to view -> display
I'm just not sure if that's "Ok" to do, since it disregards the MVC model completely.
So I found a quick example to cut-n-paste (in this case I have a model called login autoloaded, but you can of course do that manually)
In the file core\MY_Controller.php
class Admin_Controller extends CI_Controller
{
protected $login_ok;
public function __construct()
{
parent::__construct();
/* --- Check if user is logged in --- */
$this->config->load('adldap', TRUE);
$data->login_ok = $this->login->check_login(TRUE);
$this->load->vars($data);
}
}
If you then extend your controller with this. You will have $login_ok available in your view.
This way you can be sure that the required variables are always prepared and you only have to write the code in one place
I think that the solution is easier than you think.
If now you're doing something like this in your helper:
create_menu()
{
$menu_items = $this->db->query('')->result();
// creating the menu here
}
You could just change the function to accept input like this and still follow the MVC pattern.
Helper
create_menu($input)
{
$menu_items = $input;
// creating the menu here
}
Model:
get_menu_data()
{
$menu_items = $this->db->query('')->result();
}
Does this make sense?
Edit:
This is the way I did it on one of the projects:
I extended my standard controller. Within the constructor of that controller I called the model and grabbed the data:
$this->menu_items = $this->some_model->get_menu_items();
Within a view nav.php:
if(!empty($this->subnav_item))
{
// Generate menu
}
This way the MVC is intact, but I dont have to worry about passing variables.
EDIT 2
How to extend the standard controller:
Create a file MY_Controller.php in application/core
class MY_Controller extends CI_Controller {
public $menu_items = '';
function __construct()
{
parent::__construct();
$this->load->model('some_model_that_you_always_use');
$this->load->library('some_library_that_you_always_use');
$this->menu_items = $this->some_model->get_menu_items();
}
}
When you create a new controller, you extend MY_Controller instead of CI_Controller like this:
class Something extends MY_Controller {
}
Keep in mind that nothing about the MVC pattern prohibits a View from contacting a Model directly. That's just a convention that seems common with CodeIgniter.
I suggest that in this case, your menu view should load the menu data directly from the menu model.

Resources