SilverStripe canCreate on an Extension - php

For pages that must always be on the website (only 1, not more, like a homepage), I wanted to make an extension. The page should be created when I run dev/build and cannot be created, deleted, unpublished or archived in the CMS. It can be edited however.
Everthing seems to work just fine except the canCreate function. It seems as this function doesn't do much for the CMS when it's on a(n) (Data)Extension. When I'm on .../admin/pages/add I can still create the Page as many times as I want.
If I move the canCreate to the HomePage class, it does work. But I want it in my extension :-)
class OnePage extends DataExtension
{
public function requireDefaultRecords()
{
if($this->canCreate()) {
$page = $this->owner;
$page->Title = $this->owner->className;
$page->write();
$page->publish('Stage', 'Live');
$page->flushCache();
}
}
public function canCreate($member = null)
{
return $this->owner->get()->count() == 0;
}
public function canDeleteFromLive($member = null)
{
return false;
}
public function canArchive($member = null)
{
return false;
}
}
class HomePage extends Page
{
private static $extensions = [
'OnePage'
];
}
Am I doing this the right way or am I missing something?
EDIT
The canCreate method works just fine, so I can solve it like this;
class HomePage extends Page
{
public function canCreate($member = null)
{
return $this->get()->count() == 0;
}
}
This has the desired effect and works like a charm. However, the question is about putting the canCreate method in the Extension. That should work aswell, right?

Well, $this->owner->get() won't work. $this->owner always returns the actual object, not a DataList. It should work like:
public function canCreate() {
$className = $this->owner->ClassName;
return $className::get()->count() == 0;
}
BTW: if you plan to release a module of this please consider another name, I already have published https://github.com/wernerkrauss/silverstripe-onepage/ for making simple onepage-sites utilizing the SiteTree

Related

Lint dynamic return types in PHP

I would like to be able to autocomplete when using my following code. I have looked for a solution, but just can't seem to find anything.
This is about what my models look like. In the following example I would like to be able to tab example and test
class ExampleModel extends Model {
public function example() {
//Do Something
}
public function test() {
//Do Something
}
}
My function to retrieve a model:
/**
* Returns an object of the requested model
* #param string $model Name of the model
* #return Model Object of the model
*/
public function getModel($model) {
$model .= "Model";
$path = "models/$model.php";
if (!isset($this->modelCache[$model])) {
if (file_exists($path)) {
require_once $path;
} else {
throw new Exception("Model: $model does not exist!");
}
$this->modelCache[$model] = new $model($this);
}
return $this->modelCache[$model];
}
This is how I call a model:
$x->getModel("Example")->example();
I am looking forward to your ideas. I am very excited to hear your ideas for this problem! I am also open to suggestions regarding the getModel function itself of course.
Although I would still like to know how to achieve this without changing the process itself, but I have solved this problem by simply using static classes and namespaces. The linting is great like that.

Prestashop override Blocktopmenu function

I'm trying to override the generateCategoriesMenu function from blocktopmenu.php
class BlocktopmenuOverride extends Blocktopmenu
{
public function generateCategoriesMenu($categories, $is_children = 0)
{
die( var_dump ('hello I am an override') );
}
}
I've set up this in the folder override/modules cleared the cache file class_index.php
when I load my index page I would expect a blank page with a string 'hello I am an override'
I'm on 1.6.0.12
edit
I know I can do it the old way 'cloning' the module but there's a new undocumented hidden way in prestashop if you look in module.php this function clearly says that we can override a module.
public static function getInstanceByName($module_name)
{
if (!Validate::isModuleName($module_name))
{
if (_PS_MODE_DEV_)
die(Tools::displayError(Tools::safeOutput($module_name).' is not a valid module name.'));
return false;
}
if (!isset(self::$_INSTANCE[$module_name]))
{
if (Tools::file_exists_no_cache(_PS_MODULE_DIR_.$module_name.'/'.$module_name.'.php'))
{
include_once(_PS_MODULE_DIR_.$module_name.'/'.$module_name.'.php');
if (Tools::file_exists_no_cache(_PS_OVERRIDE_DIR_.'modules/'.$module_name.'/'.$module_name.'.php'))
{
include_once(_PS_OVERRIDE_DIR_.'modules/'.$module_name.'/'.$module_name.'.php');
$override = $module_name.'Override';
if (class_exists($override, false))
return self::$_INSTANCE[$module_name] = new $override;
}
if (class_exists($module_name, false))
return self::$_INSTANCE[$module_name] = new $module_name;
}
return false;
}
return self::$_INSTANCE[$module_name];
}
I already did successfully overrides on other modules with no problem,
this override works if I comment the parent function but that makes pointless the override.
why with blocktopmenu I can't override the desired function?
I hope I made it clear now.
I took a look at it again,
it may be because parent functions is private :
private function generateCategoriesMenu($categories, $is_children = 0)
As you know, private functions are only visible in that class in which it was declared. In this case the whole situation works a little bit like declaring it final.
Your only hope is to submit a patch to GitHub making these methods protected - this should become common practice once the module overrides take off.

__destruct called twice in CodeIgniter

First: I've already reviewed all the questions I can find about __destruct() and CodeIgniter, and none seem to address the same issue I'm seeing.
Right. That aside. I'll show code first, as the problem will make more sense after reading. (N.B.: some code redacted, but nothing important to the question. The Billing::index function however, is comprises only one space character in production as well as here)
application/core/MY_Controller.php
class MY_Controller extends CI_Controller{
public $view = '';
public $data = array();
public $template = '';
function __destruct(){
if(!is_null($this->template) && ($this->template == '')){
$this->template = 'public';
}
if($this->view == ''){
$this->view = $this->uri->segment(1,'index').'/'.$this->uri->segment(2,'index');
}
if(!is_null($this->template)){
echo $this->load->view('templates/'.$this->template.'/top',$this->data,true);
}
echo $this->load->view('views/'.$this->view,$this->data,true);
if(!is_null($this->template)){
echo $this->load->view('templates/'.$this->template.'/bottom',$this->data,true);
}
}
}
class MY_ProtectedController extends MY_Controller{
function __destruct(){
parent::__destruct();
}
}
application/controllers/billing.php
class Billing extends MY_ProtectedController{
public function index(){ }
}
This, loads perfectly. The main "billing" page is just HTML, so the destructor fires the right template and view.
However, I would love to remove the redundant public function index(){ }, as it really does nothing.
So if I remove that from application/controllers/billing.php so that it's just
class Billing extends MY_ProtectedController{ }
and visit /billing, I get the output I want, however, I get it twice.
If I do some test echos, MY_ProtectedController::__destruct() is called once, but MY_Controller::__destruct is called twice.
My question is: why is this the case, and can it be stopped?
I'm not super familiar with CI core, and I figure that by the time I dig far enough to find the controller instantiation, someone here has probably already got an answer. Will of course update this if my own digging turns anything up.
Solution ended up being fairly simple - but credit for spotting this goes to one of our other programmers.
If I put some code in my 404 route to prevent the auto-destructor from running, I only get one set of output.
class Index extends MY_PublicController {
public function not_found(){
$this->cancel_destruct = TRUE;
}
}
class MY_Controller extends CI_Controller{
$this->cancel_destruct = FALSE;
function __destruct(){
if(!$this->cancel_destruct){
[...]
}
}
}

Assigning defaults for Smarty using Object Oriented style

I'm just very slowly starting to sink into object-oriented programming, so please be gentle on me.
I have a custom class for Smarty that was partially borrowed. This is how the only example reflects the basic idea of using it across my current project:
class Template {
function Template() {
global $Smarty;
if (!isset($Smarty)) {
$Smarty = new Smarty;
}
}
public static function display($filename) {
global $Smarty;
if (!isset($Smarty)) {
Template::create();
}
$Smarty->display($filename);
}
Then in the PHP, I use the following to display templates based on the above example:
Template::display('head.tpl');
Template::display('category.tpl');
Template::display('footer.tpl');
I made the following example of code (see below) work across universally, so I wouldn't repeat the above lines (see 3 previous lines) all the time in each PHP file.
I would just like to set, e.g.:
Template::defauls();
that would load:
Template::display('head.tpl');
Template::display('template_name_that_would_correspond_with_php_file_name.tpl');
Template::display('footer.tpl');
As you can see Template::display('category.tpl'); will always be changing based on the PHP file, which name is corresponded with the template name, meaning, if for example, PHP file is named stackoverflow.php then the template for it would be stackoverflow.tpl.
I've tried my solution that have worked fine but I don't like it the way it looks (the way it's structured).
What I did was:
Assigned in config a var and called it $current_page_name (that derives the current PHP page name, like this: basename($_SERVER['PHP_SELF'], ".php"); ), which returned, for e.g.: category.
In PHP file I used Template::defaults($current_page_name);
In my custom Smarty class I added the following:
public static function defaults($template) {
global $Smarty;
global $msg;
global $note;
global $attention;
global $err;
if (!isset($Smarty)) {
Templates::create();
}
Templates::assign('msg', $msg);
Templates::assign('note', $note);
Templates::assign('attention', $attention);
Templates::assign('err', $err);
Templates::display('head.tpl');
Templates::display($template . '.tpl');
Templates::display('footer.tpl');
}
Is there a way to make it more concise and well structured? I know about Code Review but I would like you, guys, to take a good look at it.
This looks like you haven't loaded Smarty, that's why the error happens. You need to start by including Smarty before the class starts. If you follow my other config suggestion you should start by including that one as well.
In you Template class, just add the following function:
function defaults() {
// Don't know if you need the assignes, havn't used Smarty, but if so, insert them here...
Template::display( Config::get('header_template') ); //header_template set in the Config file
Template::display( basename($_SERVER['PHP_SELF'], ".php") . '.tpl' );
Template::display( Config::get('footer_template') ); //footer_template set in the Config file
}
Now you should be able to use it in any file:
$template = new Template();
$template->defaults();
EDIT:
A singleton is in every sense the same as a global, that will keep your same problem.
But your problem is that if you try to use one of the Template's static functions you are in the "static" mode, which means the constructor have not been run. And Smarty has not been assigned. If you want to go this road, you can do one of two thinks:
Make the Template a real singleton, meaning set the constructor to private add a function getInstance, that returns a instance of the class, and then use that object to call the functions in it (which should not be static), or
Make all those static functions check if smarty is set, and if it's not, create a new instance of smarty, otherwise use the one that already is instantiated to run its function.
EDIT 2:
Here's the proper way to make a singleton:
class Singleton {
private static $instance = null;
// private static $smarty = null;
private function __construct() {
//self::$smarty = new Smarty();
}
public static function getInstance() {
if( self::$instance === null ) {
self::$instance = self();
}
return self::$instance;
}
public function doSomething() {
//self::$smarty->doSomething();
}
}
It's used like this:
$singleton = Singletong::getInstance();
$singleton->doSomething();
I commented out the things you probably want do to to make this a singleton wrapper around a singleton Smarty object. Hope this helps.
EDIT 3:
Here's a working copy of your code:
class Template {
private static $smarty_instance;
private static $template_instance;
private function Template() {
self::$smarty_instance = new Smarty();
$this->create();
}
public static function getInstance() {
if( ! isset( self::$template_instance ) ) {
self::$template_instance = new self();
}
return self::$template_instance;
}
private function create() {
self::$smarty_instance->compile_check = true;
self::$smarty_instance->debugging = false;
self::$smarty_instance->compile_dir = "/home/docs/public_html/domain.org/tmp/tpls";
self::$smarty_instance->template_dir = "/home/docs/public_html/domain.org";
return true;
}
public function setType($type) {
self::$smarty_instance->type = $type;
}
public function assign($var, $value) {
self::$smarty_instance->assign($var, $value);
}
public function display($filename) {
self::$smarty_instance->display($filename);
}
public function fetch($filename) {
return self::$smarty_instance->fetch($filename);
}
public function defaults($filename) {
global $user_message;
global $user_notification;
global $user_attention;
global $user_error;
self::$smarty_instance->assign('user_message', $user_message);
self::$smarty_instance->assign('user_notification', $user_notification);
self::$smarty_instance->assign('user_attention', $user_attention);
self::$smarty_instance->assign('user_error', $user_error);
self::$smarty_instance->assign('current_page', $filename);
self::$smarty_instance->display('head.tpl');
self::$smarty_instance->display($filename . '.tpl');
self::$smarty_instance->display('footer.tpl');
}
}
When using this function, you should use it like this:
$template = Template::getInstance();
$template->defaults($filename);
Try it now.
You can get current file name in your defaults() function. Use this piece of code:
$currentFile = $_SERVER['REQUEST_URI'];
$parts = explode('/', $currentFile);
$fileName = array_pop($parts);
$viewName = str_replace('.php', '.tpl', $fileName);
$viewName is the name that you need.
This is a quick wrapper I made for Smarty, hope it gives you some ideas
class Template extends Smarty
{
public $template = null;
public $cache = null;
public $compile = null;
public function var($name, $value, $cache)
{
$this->assign($name, $value, $cache);
}
public function render($file, $extends = false)
{
$this->prep();
$pre = null;
$post = null;
if ($extends)
{
$pre = 'extends:';
$post = '|header.tpl|footer.tpl';
}
if ($this->prep())
{
return $this->display($pre . $file . $post);
}
}
public function prep()
{
if (!is_null($this->template))
{
$this->setTemplateDir($this->template);
return true;
}
if (!is_null($this->cache))
{
$this->setCacheDir($this->cache);
}
if (!is_null($this->compile))
{
$this->setCompileDir($this->compile);
return true;
}
return false;
}
}
Then you can use it like this
$view = new Template();
$view->template = 'path/to/template/';
$view->compile = 'path/to/compile/'
$view->cache = 'path/to/cache';
$view->assign('hello', 'world');
// or
$view->var('hello', 'world');
$view->render('index.tpl');
//or
$view->render('index.tpl', true); // for extends functionality
I did this kinda fast, but just to show you the basic ways you can use smarty. In a more complete version you could probably want to check to see if compile dir is writable, or if file templates exist etc.
After trying for few days to solve this simple problem, I have finally came up with working and fully satisfying solution. Remember, I'm just a newby in object-oriented programming and that's the main reason why it took so long.
My main idea was not to use global $Smarty in my initial code that worked already fine. I like to use my Smarty as just simple as entering, e.g.: Template::assign('array', $array). To display defaults, I came up with the trivial solution (read my initial post), where now it can be just used Template::defaults(p()); to display or assign anything that is repeated on each page of your project.
For doing that, I personally stopped on the following fully working solution:
function p() {
return basename($_SERVER['PHP_SELF'], ".php");
}
require('/smarty/Smarty.class.php');
class Template
{
private static $smarty;
static function Smarty()
{
if (!isset(self::$smarty)) {
self::$smarty = new Smarty();
self::Smarty()->compile_check = true;
self::Smarty()->debugging = false;
self::Smarty()->plugins_dir = array(
'/home/docs/public_html/domain.com/smarty/plugins',
'/home/docs/public_html/domain.com/extensions/smarty');
self::Smarty()->compile_dir = "/home/docs/public_html/domain.com/cache";
self::Smarty()->template_dir = "/home/docs/public_html/domain.org";
}
return self::$smarty;
}
public static function setType($type)
{
self::Smarty()->type = $type;
}
public static function assign($var, $value)
{
self::Smarty()->assign($var, $value);
}
public static function display($filename)
{
self::Smarty()->display($filename);
}
public static function fetch($filename)
{
self::Smarty()->fetch($filename);
}
public static function defaults($filename)
{
Template::assign('current_page_name', $filename);
Template::display('head.tpl');
Template::display($filename . '.tpl');
Template::display('footer.tpl');
}
}
Please use it if you like it in your projects but leave comments under this post if you think I could improve it or you have any suggestions.
Initial idea of doing all of that was learning and exercising in writing a PHP code in object-oriented style.

How to include a php and then remove it?

Well, I don't know if this post have the correct title. Feel free to change it.
Ok, this is my scenario:
pluginA.php
function info(){
return "Plugin A";
}
pluginB.php
function info(){
return "Plugin B";
}
Finally, I have a plugin manager that is in charge of import all plugins info to pool array:
Manager.php
class Manager
{
protected $pool;
public function loadPluginsInfo()
{
$plugin_names = array("pluginA.php", "pluginB.php");
foreach ($plugin_names as $name)
{
include_once $name;
$this->pool[] = info();
}
}
}
The problem here is that when I print pool array it only show me the info on the first plugin loaded. I supposed that the file inclusing override the info because it still calling the info() method from the first include.
Is there a way to include the info of both plugins having the info() function with the same name for all plugins files?
Thank you in advance
PS: a fatal cannot redeclare error is never hurled
you can use the dynamic way to create plugin classes
plugin class
class PluginA
{
public function info()
{
return 'info'; //the plugin info
}
}
manager class
class Manager
{
protected $pool;
public function loadPluginsInfo()
{
$plugin_names = array("pluginA", "pluginB"); //Plugin names
foreach ($plugin_names as $name)
{
$file = $name . '.php';
if(file_exists($file))
{
require_once($file); //please use require_once
$class = new $name(/* parameters ... */); //create new plugin object
//now you can call the info method like: $class->info();
}
}
}
}
Are you sure the interpreter isn't choking w/ a fatal error? It should be since you're trying to define the info function twice here.
There are many ways to achieve what you want, one way as in #David's comment above would be to use classes, eg.
class PluginA
{
function info() { return 'Plugin A'; }
}
class PluginB
{
function info() { return 'Plugin B'; }
}
then the Manager class would be something like this:
class Manager
{
protected $pool;
public function loadPluginsInfo()
{
$plugin_names = array("PluginA", "PluginB");
foreach ($plugin_names as $name)
{
include_once $name . '.php';
$this->pool[] = new $name();
}
}
}
Now you have an instance of each plugin class loaded, so to get the info for a plugin you would have $this->pool[0]->info(); for the first plugin. I would recommend going w/ an associative array though so you can easily reference a given plugin. To do this, the assignment to the pool would become:
$this->pool[$name] = new name();
And then you can say:
$this->pool['PluginA']->info();
for example.
There are many other ways to do it. Now that 5.3 is mainstream you could just as easily namespace your groups of functions, but I would still recommend the associative array for the pool as you can reference a plugin in constant time, rather than linear.

Categories