// Bootstrap Drupal
define('DRUPAL_ROOT', getcwd());
require_once 'includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
function deleteBillNodes()
{
$results = db_select('node', 'n')
->fields('n', array('nid'))
->condition('type', array('bill'), 'IN')
->execute();
foreach ($results as $result)
{
$nids[] = $result->nid;
}
if (!empty($nids))
{
node_delete_multiple($nids);
}
}
echo "Removing bill nodes from database\n";
deleteBillNodes();
I am getting, "Call to undefined function node_delete_multiple()."
How can I get this to work?
It's because Drupal requires a single constant to be defined (DRUPAL_ROOT) before the bootstrap can be run. Just put this before your call to drupal_bootstrap():
define('DRUPAL_ROOT', getcwd());
This is how Drupal itself does it, the only code lines in index.php are:
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
menu_execute_active_handler();
Try removing fast404 from settings.php and clear cache?
If this is the solution it is time to post it on drupal.org bug page had the same problems on libristranieri.com
Related
I've developed a CakePHP plugin that allows the site administrator to define custom reports as a list of SQL queries that are executed and the results displayed by a .ctp template.
Now I need to allow the administrator to edit the template, stored in the DB together with the report.
Therefore I need to render a template that is inside a string and not in a .ctp file and I could not find anything in the core that helps.
I considered initially the approach to write the templates in .ctp files and load them from there, but I suspect this solution is rigged with flaws re: the location of the files and related permissions.
A better solution seems to override the View class and add a method to do this.
Can anyone suggest a better approach ?
P.S. Security is not a concern here, since the administrator is basically a developer without access to the code.
In CakePHP 2.0:
The View::render() method imports the template file using
include
The include statement includes and evaluates the specified file.
The evaluated template is immediately executed in whatever scope it was included. To duplicate this functionality, you would need to use
eval()
Caution: The eval() language construct is very dangerous because it
allows execution of arbitrary PHP code. Its use thus is discouraged.
If you have carefully verified that there is no other option than to
use this construct, pay special attention not to pass any user
provided data into it without properly validating it beforehand.
(This warning is speaking to you, specifically)
...if you wish to continue... Here is a basic example of how you might achieve this:
$name = 'world';
$template = 'Hello <?php echo $name ?>... <br />';
echo $template;
// Output: Hello ...
eval(' ?>' . $template . '<?php ');
// Output: Hello world...
Which is (almost) exactly the same as:
$name = 'world';
$template = 'Hello <?php echo $name ?>... <br />';
file_put_contents('path/to/template.php', $template);
include 'path/to/template.php';
Except people won't yell at you for using eval()
In your CakePHP application:
app/View/EvaluatorView.php
class EvaluatorView extends View
{
public function renderRaw($template, $data = [])
{
if (empty($data)) {
$data = $this->viewVars;
}
extract($data, EXTR_SKIP);
ob_start();
eval(' ?>' . $template . '<?php ');
$output = ob_get_clean();
return $output;
}
}
app/Controller/ReportsController.php
class ReportsController extends AppController
{
public function report()
{
$this->set('name', 'John Galt');
$this->set('template', 'Who is <?php echo $name; ?>?');
$this->viewClass = 'Evaluator';
}
}
app/View/Reports/report.ctp
// Content ...
$this->renderRaw($template);
Alternatively, you may want to check out existing templating engines like: Mustache, Twig, and Smarty.
Hmmm.. Maybe create a variable that will store the generated code and just 'echo' this variable in ctp file.
I had similar problem (cakephp 3)
Controller method:
public function preview($id = null) {
$this->loadModel('Templates');
$tempate = $this
->Templates
->findById($id)
->first();
if(is_null($template)) {
$this->Flash->error(__('Template not found'));
return $this->redirect($this->referer());
}
$html = $template->html_content;
$this->set(compact('html'));
}
And preview.ctp is just:
<?= $html
I have been at this for a couple days now. Seems like the simplest thing in the world but for the life of me I cannot get this to work.
I am trying to use this class in my Controller:
https://github.com/neitanod/forceutf8
The class is named Encoding.php and it has a namespace ForceUTF8.
I've tried all of the following:
App::uses('Encoding','Vendor'); //(copying Encoding.php directly in the Vendor dir)
App::uses('Encoding','Vendor/ForceUTF8'); //(copying Encoding.php in ForceUTF8 subdir)
App::uses('Encoding’,’Lib'); //(copying Encoding.php directly in the Lib dir)
App::uses('Encoding’,’Lib/ForceUTF8'); //(copying Encoding.php in ForceUTF8 subdir)
require_once(APP . 'Vendor' . DS . 'Encoding.php'); //(use plain old php require_once to load class from Vendor dir)
No matter what I do I get the same error: Class 'Encoding' not found.
I find the CakePHP documentation to be very confusing on this subject. On the one hand it says not to use App::uses for non-CakePHP classes because the classes might not follow CakePHP standards. Fair enough. So instead they say to use App::import. But then there are tons of posts saying that App::import is nothing more than a wrapper for require_once.
So after not being able to get App::uses or App::import to work, I tried require_once. Same error. Then I found a post here on so saying even when using require_once, you still have to 'initialize' the class in order to CakePHP to be able to see/use it. And how is that done? App::uses. So I'm back where I started.
Something tells me the namespace is causing the problem. When I use require_once the class is found (I'm pretty sure) because, for example, if I change
require_once(APP . 'Vendor' . DS . 'Encoding.php');
to
require_once(APP . 'Vendor' . DS . 'blabla.php');
it gives me an error, file not found.
But even though require_once seems to find it, CakePHP still says class not found.
How can I load a namespaced vendor file?
In Cakephp 2.x, follow following steps
at top of class file add
use \Dropbox as dbx;
[Dropbox is vendor directory in my case]
use \Dropbox as dbx;
App::build(array('Vendor/Dropbox' => array('%s' . 'Dropbox' . DS)), App::REGISTER);
// Autoload the classes in the 'vendors'
spl_autoload_register(function ($class) {
foreach (App::path('Vendor') as $base) {
$path = $base . str_replace('\\', DS, $class) . '.php';
if (file_exists($path)) {
include $path;
return;
}
}
});
Now call your class method.
Cake2.x does not support namespaces so you need write your own loader
spl_autoload_register(function ($class) {
foreach (App::path('Vendor') as $base) {
$path = $base . str_replace('\\', DS, $class) . '.php';
if (file_exists($path)) {
include $path;
return;
}
}
});
more info here:
http://www.dereuromark.de/2012/08/06/namespaces-in-vendor-files-and-cake2-x/
Base on this article:
http://www.dereuromark.de/2012/08/06/namespaces-in-vendor-files-and-cake2-x/comment-page-1
I'm using the paypal sdk. And this is my code.
In bootstrap.php:
// Add 'vendors' as sub-package of 'Vendor' to the application
App::build(array('Vendor/vendors' => array('%s' . 'vendors' . DS)), App::REGISTER);
// Autoload the classes in the 'vendors'
spl_autoload_register(function ($class) {
foreach (App::path('Vendor/vendors') as $base) {
$path = $base . str_replace('\\', DS, $class) . '.php';
if (file_exists($path)) {
include $path;
return;
}
}
});
In the PaypalComponent:
use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
Use those classes in function:
$this->apiContext = new ApiContext(
new OAuthTokenCredential()
);
I am trying to perform functions exporting and importing CSV file.
I have done importing file successfully, but while exporting file, the downloaded CSV file contains HTML code with header, body and footer. I think that was from default.ctp, not sure.
Why, and from where, is that code automatically embedded ?
<?php function export()
{
$fp = NULL;
$results = NULL;
$row = NULL;
$results = $this->Order->find('all',array('fields'=> array('Order.sku','Order.inventory')));
$fp = fopen('php://output','w+');
$filename = "results.csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment;');
fputcsv($fp,array(
'sku',
'inventory'
));
$ctr = count($results);
print($ctr);
foreach($results as $row)
{
fputcsv($fp,array(
$row['Order']['sku'],
$row['Order']['inventory']
));
}
fclose($fp);
} ?>
You need to disable layout rendering, stick this in your controller action:
$this->autoRender = false;
Tehnichally, this is not the correct way to do CSV, you should create a CSV layout and view, but if it works, ce le vie.
Thats a simple solution, either create a CSV layout that contains the necessary headers, or just use the built in ajax layout:
$this->layout = 'ajax';
In your controller action. That should fix it for you.
Disable layout in controller
<?php
$this->layout=null;
?>
Is anybody else using David Persson's media plugin for CakePHP? I'm struggling with setting up some features of the latest version. I'd like to set it up to make a UUID-based filename for uploaded images, but I'm not sure how to go about it.
I will fight with it some more, but I'm posting to find out if anybody here can tell me if the 1.3 is generally working or generally NOT working.
Finally got this (partially) working. The UUID filename stuff works when I place the following code in my Attachment model:
function transferTo($via, $from) {
extract($from);
$irregular = array(
'image' => 'img',
'text' => 'txt'
);
$name = Mime_Type::guessName($mimeType ? $mimeType : $file);
if (isset($irregular[$name])) {
$short = $irregular[$name];
} else {
$short = substr($name, 0, 3);
}
$path = $short . DS;
$path .= String::uuid();
$path .= !empty($extension) ? '.' . strtolower($extension) : null;
return $path;
}
I'm still having some trouble with other parts of the Media Helper, but the author posted some updates to his git repository today (Jul 17 2010).
my url is http://localhost/myAppliation/....
I want to get the "myApplication". Which is the key word to get the value?
I think you want:
$this->webroot
or
$this->base
or
$this->here
in your controller. See http://api.cakephp.org/class/controller for definition of this variables.
Edit: Didn't see that you were using CakePHP. If you weren't using any frameworks, you would get the path via this:
First you need to reconstruct the URL.
$pageURL = (#$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
if ($_SERVER["SERVER_PORT"] != "80")
{
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
}
else
{
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
Then parse it for the path.
$parts = parse_url($pageURL);
$path = $parts['path'];
After that if you want to only get the first path, explode it on '/', strip out the null values, and then the first one will be the first path, and so on.
$paths = explode('/', $path);
foreach($paths as $key => $value)
{
if(!empty($value))
{
$new_paths[] = $value;
}
}
$first_path = $new_paths[0];
There is something called the $Name attribute, but I'm not sure if that gives you the application name. The CakePHP manual says:
PHP4 users should start out their
controller definitions using the $name
attribute. The $name attribute should
be set to the name of the controller.
Usually this is just the plural form
of the primary model the controller
uses. This takes care of some PHP4
classname oddities and helps CakePHP
resolve naming.
http://book.cakephp.org/view/52/name
http://book.cakephp.org/view/317/Cake-s-Global-Constants-And-Functions
I think you want to use the WEBROOT_DIR constant.