can i use same controller for user and admin? - php

i am making a online book store in codeigniter.when user logging in, i am using
localhost/folder/index.php/User_controller.
my qustion is when admin is logging in one extra menu should come for approval, rest of the view is same as that of user.so what should i do,when i make seperate controller in different application folder,i have to run different in url, ie localhost/foldername/admin.php/admin_controller
.i am really confused.please help............

You can build a custom library where you may write function for checking if_admin(). After that you can use this function in any view file or controller and load content as required.
Create Custom Library
So doesn't need to create different controller for admin. Just check with is_admin() in menu to load extra menu.
A demo method is look like (in custom library)
public function is_admin() {
$type = (int) $this->CI->session->user_type;
if ($type === 4) {//4 is admin type
return TRUE;
} else {
return FALSE;
}
}
Now check in view file as
if($this->custom_lib->is_admin()){
//load extra menu items
}

Related

Automatic Laravel routing for static pages

I have to maintain a site for a client, they had it made with Laravel, the most frequent requests from them are to create new static pages and the more I create them - the more I feel there are definitely better ways to implement them.
To create a static page I go to the admin panel, a menu "Static Pages", push a button "Create New" and there create an entry that goes into a database table. To make the static page show in the website I have to define it in a controller called "FrontEndController" like this:
public function some_page() {
$page = StaticPages::find(1);
return view('frontend.static_pages.some_page', compact('page'));
}
public function some_other_page() {
$page = StaticPages::find(2);
return view('frontend.static_pages.some_other_page', compact('page'));
}
...
Then define routes like this:
Route::get('some-page', 'FrontEndController#some_page')->name('static_page.some_page');
Route::get('some-other-page', 'FrontEndController#some_other_page')->name('static_page.some_other_page');
Now I always thought that you create an admin panel with a menu "Static Pages" and a button "Create New" so that you don't have to put any code manually, but seemingly the developer had other ideas...
So my question is how do you refactor a code like this so that you don't have to go through all of the repetitive process in any similar scenario and write code manually?
Btw I may need to sometimes send some generic params to different static pages and I want to keep the URLs the same as they are. Please, keep in mind that I'm not a very advanced user of Laravel or even PHP.
There is no need to add extra method and router for per page manually.
I think that is a matter of unexperienced developer ,
So, You need to refactor your codes like this.
Add one router and remove others.
Route::get('/static/{id}','FrontendController#index');
On your Controller.
public function index($id)
{
$page = StaticPages::find($id);
$theme = $page->theme; //assume you have save your html themplate name in your table via theme column
return view('frontend.static_pages.'.$theme, compact('page'));
}
Save url or slug in your database
Route::get('staticpage/{pagename}', 'FrontEndController#MethodName');
$page = StaticPages::where('url',$url)->first();
return view('YourStaticPage', compact('page'));

Avoid repeating session and access right checks in codeigniter 3 controller

I have implemented a simple role check code with the help of helpers using sessions in my codeigniter 3 application.
Access Helper: Defines a function named as access_right which checks if the logged in user has access right to visit a specific module or not and correspondingly returns true or false.
In addition to it, I also have two versions of navigation bars one for admin user and for non admin users.
In controller i have added following code to perform the check for both i.e. which navigation to load and if the logged in user has access or not to module:
if($this->session->userdata('user-type') === 'admin')
{
$this->load->view('templates/sub_header_admin');
}
else
{
$this->load->view('templates/sub_header');
}
if(access_right('client_information'))
{
$this->load->view('pages/clientview/client_page');
}
else
{
$this->load->view('templates/restricted_access');
}
$this->load->view('templates/footer');
Problem is I need to repeat this much of code in each and every method inside a controller.
access_right('client_information'), of-course instead of client_information i check for different value like 'operator_information' depending on which controller is loaded.
How can i avoid this repetition of code ?
Just like you avoided repeating code for checking 'client_information' access rights - create a function to do that.

How to disable module hooks for certain controllers in Prestashop?

I'm writing my own module and the essential option is to control controller from the module options.
I know how to control tpl and js via module options but I can't get the way to control Prestashop controller from the module php file.
Simply I want to know the way how to do it.
I want to have four checkboxes with options to enable or disable module in four controllers like index, cms, category, product.
I have right now:
$values = array('index','product','cms','category');
if(in_array(Tools::getValue('controller'), $values)){
return $this->display(__FILE__, 'mymodule.tpl');
}
And this code display tpl file content in this four controllers in homepage (index), cms, category and the product pages.
But how to put there some trigger to enable/disable values from the array?
Make a configuration field for the controllers:
public function getContent()
{
if (Tools::isSubmit('MY_CONTROLLERS_LIST')) {
Configuration::updateValue('MY_CONTROLLERS_LIST', (string)Tools::getValue('PS_MY_CONTROLLERS'));
}
$value = Configuration::get('MY_CONTROLLERS_LIST');
return '<form action="" method="POST"><input name="PS_MY_CONTROLLERS" value="'.$value.'"><input type="submit" value="Save"></form>';
}
public function hookDisplayTop()
{
$value = Configuration::get('PS_MY_CONTROLLERS');
$controllers = explode(',', $value);
if(in_array(Tools::getValue('controller'), $controllers)){
return $this->display(__FILE__, 'mymodule.tpl');
}
return false;
}
This will appear in Modules > Modules > Configure (of your module). There are form helpers which can render PrestaShop forms, but this is simplified example showing that you will need aform that submits to the same page.
Controllers value for this example should be index,category,cms,product.
Another way
Go to Modules > Positions Find your hook (for example displayTop) and your module, click Edit. Then selected the pages where you don't want to show the block.
Generating Options Form
The correct way (but more complicated) would be to generate the form using HelperForm or HelperOptions classes that come with PrestaShop

Using renderView() in a custom ModuleAdminController (PS1.6)

I am trying to add the view action to my back office module page but i cannot display anything with the renderview() function. I can already display my list with renderList() and it's working well. I also tried renderForm() and it works well too but it seemz i can't get renderView() to display something.
public function renderView(){
if(!($config = $this->loadObject())){
return;
}
$data = Config::getDataForm(Tools::getValue('id_config'));
// var_dump($data);
$this->tpl_view_vars = array(
'id_config' => $data['id_config'],
'prix' => $data['prix'],
'hauteur' => $data['hauteur_passage']
);
return parent::renderView();
}
This is a pretty basic code. My getDataForm($id_config) is getting fields from database in an array so that i can display it. I can see the var_dump displaying for a short time before displaying the blank page with prestashop header and footer. I tried to see if i was doing something wrond by checking other AdminController such as AdminCartsController or AdminCustomersController but it seems that their renderView() function is more or less written the same way.
Thanks in advance for your help !
I manage to resolve this problem simply by adding a view tpl in /modules/mymodule/views/templates/admin/mymodule/helpers/view/.
I wrongly assumed that it didn't need to create a template file for the view action as it didn't need one for the list and form action. After searching through modules and admin files i managed to find that there were indeed a custom view.tpl for the view action.
The renderview() method lets you set up the variables you want to use in your view.tpl.
For more information on how it works, you can check AdminCustomersController to see how it is on the controller side and /adminxxxx/themes/default/template/controllers/customers/helpers/view/view.tpl to see how the template is written.
Feel free to edit or comment if you need more information
If you want to add a configuration page on your module, you'll have to add this function to your module :
public function getContent()
{
// return some html content
}
If you want to use a controller, then you'll have to create a Controller that extends ModuleAdminController and add it in the tabs of the back-office.

Joomla 1.5 com_user and importing user plugins like Joomla 1.6 and above

When accessing com_users component in Joomla 1.6 and 1.7 on front-end the application automatically imports all plugins from 'user' group. Obviously it is very useful if one doesn't want to create a component to simply pass some variables to a plugin.
Ok. let's make it simplier:
User gets an activation link: http://example.com/index.php?option=com_users&task=edit&emailactivation=1&u=63&d077b8106=1 and clicks it.
Of course the component will omit emailactivation and other params simply displying "Edit Profile Form" (or login form for guests).
Then JApplication imports all plugins from 'user' group, which triggers __constructors
Basically, with plugin's __constructor one can set up simple action like this one below:
class plgUserAccountactivation extends JPlugin
{
public function __construct(& $subject, $config)
{
parent::__construct($subject, $config);
if(isset($_GET['emailactivation'])) {
// check token
// activate account, email or whatever
// redirect with message
}
}
}
Wow! It works, it is not necessary to create a whole controller to handle one simple task.
But hold on a minute...
In the link change index.php?option=com_users to index.php?option=com_user
And let's try on Joomla 1.5...
Hey, hey, nothing happens com_user didn't import anything at all and __constructor wan't called.
I am very troubled by this in Joomla 1.5 and I don't feel like writing whole component.
Should anybody have some bright idea, please let me know.
Edit:
I've solved my problem by sending the link in the following form:
http:/example.com/index.php?option=com_user&task=logout&emailactivation=1&u=63&d077b8106=1
This way user plugins are included and __constructors are executed. But this is so frivolous as task=logout doesn't really encourage to click in the link.
The problem with 1.5 is, that events are more limited. You have the following events available: Joomla 1.5 Plugin Events - User. I guess therefore your plugin is not initiated.
How about making this a system plugin and checking for the activation in the URL/request properties? Something like:
class plgSystemUseractiavation extends JPlugin {
function onAfterInitialise(){
$u = &JURI::getInstance();
$option = trim(strtolower($u->getVar('option')));
$emailactivation = trim(strtolower($u->getVar('emailactivation')));
if( strlen($option < 1) ){ // for SEF...
$option = trim(strtolower(JRequest::getString('option')));
}
$app =& JFactory::getApplication();
$appName = trim(strtolower($app->getName()));
if( $appName === 'site' ){
if( ( $option === 'com_users' ) || ( $option === 'com_user' ) ){
if( $emailactivation === '1' ){
// check token
// activate account, email or whatever
// redirect with message
}
}
}
}
}

Categories