Is it possible to create a plugin inside a plugin with CakePHP? - cakephp

I am attemping to create a Plugin inside a Plugin using the CakePHP Framework.
My folder structure is like so
app/Plugin/SbnAdmin/Plugin/SbnChart/.....
I am using the following line to load the SbnAdmin plugin
CakePlugin::loadAll(array('SbnAdmin' => array('bootstrap' => true)));
And in the SbnAdmin bootstrap I have
CakePlugin::loadAll();
I am able to view a controller/model/view from the SbnAdmin plugin, but I am unable to access the SbnChart plugin...
I have tried
www..../sbn_admin/sbn_chart/chart/index
www..../sbn_chart/chart/index
With no success and I am not sure what else I can do, Any ideas?

In your SbnAdmin bootstrap add:
App::build(array('Plugin' => array(CakePlugin::path('SbnAdmin') . 'Plugin' . DS)));
CakePlugin::load('SbnChart');
What we're doing is telling cake to add additional paths to look for to load the plugins, in this case inside your SbnAdmin/Plugin folder. Then we are loading the plugin afterwards.
You should be able to access it now via the normal /plugin_name/controller/action or in your case /sbn_chart/controller/action

Related

Check if event is coming from a model inside a plugin?

I need all models inside a custom CakePHP plugin to use a database prefix. I'm trying to use an event, as suggested by #lorenzo.
EventManager::instance()->on('Model.initialize', function ($event) {
$instance = $event->subject();
$instance->table('prefix_' . $instance->table());
});
I'm getting several callbacks from my plugin model as well as DebugKit models, and potentially it could be other models in the application.
Is there a way to tell if a given $event is coming from within a plugin?
I have checked $event->getSubject() and it contains the corresponding Table class. The only feasible way I could come up with is to check some properties for the plugin name.
$event->getSubject()->getRegistryAlias() is ExamplePlugin.Posts
$event->getSubject()->getEntityClass() is ExamplePlugin\Model\Entity\Post
I could check if either starts with ExamplePlugin. Is there a better way?
The fact that basically any PHP namespace can be a plugin means you could do something like that:
EventManager::instance()->on('Model.initialize', function (\Cake\Event\EventInterface $event) {
/** #var \Cake\ORM\Table $object */
$object = $event->getSubject();
$tableClassName = get_class($object);
$isApp = str_starts_with($tableClassName, 'App');
});
Because your main app's namespace will always begin with App
This of course wouldn't distinguish between your private plugins which are located in plugins and plugins which are installed via composer and therefore live in the vendor directory.
But you could introduce a name prefix to all your private plugins so you can easily distinguish them from any other plugins.

CakePHP 3.x use Plugin in a Plugin

Trying to use the Search Plugin from friendsofcake in my cakedc users plugin.
I used everthing like before (in my normal users/index.ctp it worked) and just added my custom index.ctp to the cakedc users controller, like so:
public function initialize()
{
parent::initialize();
$this->loadComponent('Search.Prg', [
'actions' => ['index']
]);
}
public function index()
{
$this->viewBuilder()->layout('backend');
$query = $this->Users
// Use the plugins 'search' custom finder and pass in the
// processed query params
->find('search', $this->Users->filterParams($this->request->query))
// You can add extra things to the query if you need to
->contain(['Skills'])
->where(['firstname IS NOT' => null]);
$this->set('users', $this->paginate($query));
}
But I am getting the error
Unknown method "filterParams"
Any ideas?
filterParams() was a method of the CakeDC search plugin. It's not available in the FoC plugin nor it still is in the CDC plugin. The FoC Search is not a drop in replacement but a completely different implementation. I've worked on both and I prefer FoC search because the code was written for Cake3 and is IMHO better than the other implementation that was "just" an upgrade from the Cake2 implementation.
I don't know from where you got filterParams() any way. I couldn't find it in the latest CDC docs nor the code. You might want to report it as a bug.

Tinymce image upload not working in cakephp 3.( by JustBoil plugin)

I am trying to add image upload option in tinymce editor using this plugin.
I have downloaded tinymce and keep it in webroot/js/tinymce folder. Then I downloaded justboil plugin, and keep it in tinymce/plugin folder. Then I have tried this code in my add.ctp file
<?php echo $this->Form->input('details',['class'=>'tinymce']); ?>
I have used this js code below
<script>
tinymce.init({selector:'.tinymce',
plugins: "jbimages,code"
});
</script>
Not it's looking like below image and working fine and also saving code data in database table.
After trying a image upload it's giving a code like
In tinymce\plugins\jbimages\config.php file , I have given bellow setting
$config['img_path'] = '/tinymce/images';
$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'] . $config['img_path'];
// same code working fine in php, but not in cakephp. May anyone help me please ?
I have been solve this problem, but still not 100 % accuracy. I am shearing my ans and also need a help to define path.
After add relative_urls: false in tinymce.init
tinymce.init({selector:'.tinymce',
plugins: "jbimages,code",
relative_urls: false
});
and I have also changed upload directory in config.php
$config['img_path'] = '/photobag.in/photobag/img';
All is working fine. Here in config.php file how can I dynamically define base_url? For example here photobag.in is my project folder.It may I will change it.

Cakephp 2.6 how do I return to the main application after entering a plugin

I am using Luis Dias' Report Manager Plugin to generate some quick reports for an application I am developing. From my application dashboard I enter the plugin using the following:
<td style="text-align:center">
<button style="height:75px; width:175px; background-color:BurlyWood; font-size:20px; font-family:Verdana"
onclick="window.location.href='<?php echo Router::url(array('controller'=>'report_manager'
,'action'=>'reports'))?>'">Report Management</button>
I'd really like to open the Report Generator Wizard in a new window but that's a different issue..
Once I am done with the report generator I'd like to return to my dashboard in my application. However, I am now in the Plugin's domain and can't figure out a command to "route" me back to the calling application.
Thanks in advance
Mike
To 'escape' from a plugin when routing you need to pass plugin => false in the route array. For example:-
$this->Html->url([
'controller' => 'pages',
'action' => 'view',
1,
'plugin' => false
]);
If you don't pass the plugin attribute it assumes you want to remain in the context of the current plugin. You need to be careful with this wherever you use links where plugins are in use.

CakePHP Upload Plugin: Programmatic File Retrieval without a Form

I use CakePHP-Upload plugin, now need to use the upload without form, following this example: Programmatic File Retrieval without a Form
All my upload are stored in the associated model, called the Attachment.
So when I save the article, at the same time save the images in Attachmen model.
Plugin documentation suggests something like this:
<?php
$this->Article->set(array('file' => $image_path)); // ?????
$this->Article->save();
?>
I have a larger collection of images on the server, previously uploaded via the Joomla CMS, now I have a migration of data to a custom CMS built on CakePHP framework. I need to take all these pictures and upload again, through this plugin.
How to properly use this plugin for my needs, I try to put the path to a local picture, but uploading is not working ???
EDIT
$image_path = WWW_ROOT . 'img' . DS . 'attachment' . DS . '59'.DS.'hpim4799.jpg';
debug($image_path);
'C:\xampp\htdocs\portal\webroot\img\attachment\59\hpim4799.jpg'
Do not save the record and not create new images.
Note, uploading through HTML form works well, I use windows 7 and xampp server.
EDIT 2 / Solution
Image path must be valid url like 'http://localhost/portal/img/attachment/59/hpim4799.jpg'
$image_path = Router::url('/', true) . 'img/attachment/59/hpim4799.jpg';
Thank you.
Can you tell me what the content of the variable $image_url is? The doucmentation says you can add a file via the form, or you can do it programmatically. If you do that, use the $image_url as path to the image. If you got an associated model like Attachment you shoud use:
<?php
$this->Article->set(array('Attachment.file' => $image_path)); // path + file to file
$this->Article->save();
?>
After hours of searching for solutions and detailed inspection of the Behavior code I finally found it.
Because Behavior uses php FILTER_VALIDATE_URL Filter, path must be a valid URL. Here is an example:
<?php
$image_path = Router::url('/', true) . 'img/attachment/59/hpim4799.jpg';
// return http://localhost/portal/img/attachment/59/hpim4799.jpg
$this->Article->Attachment->set(array('file' => $image_path));
$this->Article->Attachment->save();
?>

Resources