Is using Controller::render() in a Model::afterSave() possible with CakePHP? - php

I've got some sample code that I'd like to refactor as I need it to work after a record is saved. It currently works after the record is first rendered (using the afterFilter). What it does is render the view that I want with the layout and saves it to a file.
function afterFilter() {
parent::afterFilter();
if($this->params['pass'][0] == 'contact') {
$surrenderOuput = $this->surrender($this->params['pass'][0]);
$path = WWW_ROOT . 'cache' . DS . $this->params['pass'][0] . DS . 'index.html';
$file = new File($path, true);
$file->write($surrenderOuput);
$file->close();
}
}
function surrender($action = null, $layout = null, $file = null) {
$this->beforeRender();
$viewClass = $this->view;
if ($this->view != 'View') {
if (strpos($viewClass, '.') !== false) {
list($plugin, $viewClass) = explode('.', $viewClass);
}
$viewClass = $viewClass . 'View';
App::import('View', $this->view);
}
$this->Component->beforeRender($this);
$this->params['models'] = $this->modelNames;
if (Configure::read() > 2) {
$this->set('cakeDebug', $this);
}
$View =& new $viewClass($this);
if (!empty($this->modelNames)) {
$models = array();
foreach ($this->modelNames as $currentModel) {
if (isset($this->$currentModel) && is_a($this->$currentModel, 'Model')) {
$models[] = Inflector::underscore($currentModel);
}
$isValidModel = (
isset($this->$currentModel) && is_a($this->$currentModel, 'Model') &&
!empty($this->$currentModel->validationErrors)
);
if ($isValidModel) {
$View->validationErrors[Inflector::camelize($currentModel)] =&
$this->$currentModel->validationErrors;
}
}
$models = array_diff(ClassRegistry::keys(), $models);
foreach ($models as $currentModel) {
if (ClassRegistry::isKeySet($currentModel)) {
$currentObject =& ClassRegistry::getObject($currentModel);
if (is_a($currentObject, 'Model') && !empty($currentObject->validationErrors)) {
$View->validationErrors[Inflector::camelize($currentModel)] =&
$currentObject->validationErrors;
}
}
}
}
$this->autoRender = false;
$output = $View->render($action, $layout, $file);
return $output;
}
So I'm basically rendering the view with it's layout, and returning it as output, and saving it to a file. Great. Is there any way to do something similar in a model?

You may consider setting a member variable in your afterSave() in the model and checking that value in your afterFilter() in your controller.

I found this thread while searching for how to render a view from a model. In my case I'm calling a custom method in the model, so this might not work for afterSave(), but if you're calling a custom method you can do it like this:
Controller:
$this->Model->myFunc($this);
Model
public function myFunc($object) {
$object->render();
}
Hopefully that helps someone else who comes across this thread.

Related

Redirecting users to a 404 page by using pretty urls

I'm working on a mvc project just for fun.
Pretty urls already work, but i can't find a good way with my code to send visitors to a 404 page, in case the page doesn't exist which people are looking for.
class Route
{
private $_uri = array();
private $_method = array();
/*
* Builds a collection of internal URL's to look for
* #param type $uri
*/
public function add($uri, $method = null)
{
$this->_uri[] = '/' . trim($uri, '/');
if($method != null){
$this->_method[] = $method;
}
}
public function submit()
{
$uriGetParam = isset($_GET['uri']) ? '/' . $_GET['uri'] : '/';
foreach($this->_uri as $key => $value){
if(preg_match("#^$value$#",$uriGetParam)){
if(is_string($this->_method[$key])){
$useMethod = $this->_method[$key];
new $useMethod();
}
else{
call_user_func($this->_method[$key]);
}
}
}
}
}
I didn't analyze your code thoroughly (I couldn't, not sure what is the example route / method you're adding with ->add), but the solution seems simple to me:
public function submit()
{
$uriGetParam = isset($_GET['uri']) ? '/' . $_GET['uri'] : '/';
$routeFound = false;
foreach($this->_uri as $key => $value){
if(preg_match("#^$value$#",$uriGetParam)){
if(is_string($this->_method[$key])){
$routeFound = true;
$useMethod = $this->_method[$key];
new $useMethod();
}
else{
$routeFound = true;
call_user_func($this->_method[$key]);
}
}
}
if(!$routeFound){
http_response_code(404);
echo 'ooooh, not found';
//or:
include('404.php');
die();
}
}
p.s. http_response_code is a built-in function:
https://secure.php.net/manual/en/function.http-response-code.php
edit: you could put the code starting with 'http_response_code(404);' to a separate function and just call it.

Zend Framework 1 ini config for ACL

So I have Zend_ACL set-up(based on this: Zend Framework: need typical example of ACL ).
And I have a config file for setting permissions, but my problem is how do I give access to multiple roles to a specific controller.action
; roles
acl.roles.guest = null
acl.roles.admin = null
acl.roles.company = null
acl.roles.user = null
acl.roles.super_admin = null
; resources
acl.resources.deny.all.all = guest
acl.resources.allow.index.all = guest
acl.resources.allow.index.all = company
So the problem is this one:
acl.resources.allow.index.all = guest
acl.resources.allow.index.all = company
So my question is how do I set this up in order for multiple roles to have access.
I believe you can tweak the code to make it work for you. Change the ini like
acl.resources.allow.index.all = guest,company
Then change the code from Zend Framework: need typical example of ACL
protected function _addResources($resources) {
foreach ($resources as $permissions => $controllers) {
foreach ($controllers as $controller => $actions) {
if ($controller == 'all') {
$controller = null;
} else {
if (!$this->has($controller)) {
$this->add(new Zend_Acl_Resource($controller));
}
}
foreach ($actions as $action => $role) {
if ($action == 'all') {
$action = null;
}
if ($permissions == 'allow') {
if(strpos($role, ',') !== false) {
$multipleRoles = explode(',',$role);
$this->allow($multipleRoles, $controller, $action);
} else {
$this->allow($role, $controller, $action);
}
}
if ($permissions == 'deny') {
if(strpos($role, ',') !== false) {
$multipleRoles = explode(',',$role);
$this->deny($multipleRoles, $controller, $action);
} else {
$this->deny($role, $controller, $action);
}
}
}
}
}
}
I haven't tested this code, but my understanding is that it should work.

How to properly rewrite php code from version 5.6 to 7.x?

I tied it myself but it does not work for me. How to properly change for example this old code to be compatible with php 7.x?
class DbSimple_Generic
{
function& connect($dsn)
{
// Load database driver and create its instance.
$parsed = DbSimple_Generic::parseDSN($dsn);
if (!$parsed) {
$dummy = null;
return $dummy;
}
$class = 'DbSimple_'.ucfirst($parsed['scheme']);
if (!class_exists($class)) {
$file = str_replace('_', '/', $class) . ".php";
if ($f = #fopen($file, "r", true)) {
fclose($f);
require_once($file);
} else {
$base = basename($file);
$dir = dirname(__FILE__);
if (#is_file($path = "$dir/$base")) {
require_once($path);
} else {
trigger_error("Error loading database driver: no file $file in include_path; no file $base in $dir", E_USER_ERROR);
return null;
}
}
}
$object =& new $class($parsed);
if (isset($parsed['ident_prefix'])) {
$object->setIdentPrefix($parsed['ident_prefix']);
}
class DbSimple_Mysql_Blob extends DbSimple_Generic_Blob
{
var $blobdata = null;
var $curSeek = 0;
function DbSimple_Mysql_Blob(&$database, $blobdata=null)
{
$this->blobdata = $blobdata;
$this->curSeek = 0;
}
function read($len)
{
$p = $this->curSeek;
$this->curSeek = min($this->curSeek + $len, strlen($this->blobdata));
return substr($this->blobdata, $this->curSeek, $len);
}
function write($data)
{
$this->blobdata .= $data;
}
function close()
{
return $this->blobdata;
}
function length()
{
return strlen($this->blobdata);
}
}
function& _performNewBlob($blobid=null)
{
$obj =& new DbSimple_Mysql_Blob($this, $blobid);
return $obj;
}
I tried to use every possible way to make this work like this:
$object = new $class($parsed);
$object->method();
Becase it seems for PHP 7.x is this the most problematic part:
$object =& new $class($parsed);
But thisdid not work. I tried to find it on some PHP documentation but no luck so far. So how to properly rewrite this? Thank you
Using this on Ubuntu Server 64bit 16.04+ with Apache and mysql.
Probably it is a better idea to understand that function and rewrite in a cleaner way but please find below my changes, hopefully it helps.
class DbSimple_Generic
{
function connect($dsn)
{
// Load database driver and create its instance.
$parsed = DbSimple_Generic::parseDSN($dsn);
if (!$parsed) {
$dummy = null;
return $dummy;
}
$class = 'DbSimple_'.ucfirst($parsed['scheme']);
if (!class_exists($class)) {
$file = str_replace('_', '/', $class) . ".php";
if ($f = #fopen($file, "r", true)) {
fclose($f);
require_once($file);
} else {
$base = basename($file);
$dir = dirname(__FILE__);
if (#is_file($path = "$dir/$base")) {
require_once($path);
} else {
trigger_error("Error loading database driver: no file $file in include_path; no file $base in $dir", E_USER_ERROR);
return null;
}
}
}
$object = new $class($parsed);
if (isset($parsed['ident_prefix'])) {
$object->setIdentPrefix($parsed['ident_prefix']);
}
}
public static function parseDSN($dsn){ // public or private depends on what you intend to do
// implementation here...
}
public function setIdentPrefix($identPrefix){
// implementation here...
}
}
class DbSimple_Mysql_Blob extends DbSimple_Generic_Blob
{
var $blobdata = null;
var $curSeek = 0;
function __construct($blobdata=null) // use __construct for class constructor
{
$this->blobdata = $blobdata;
$this->curSeek = 0;
}
function read($len)
{
$p = $this->curSeek;
$this->curSeek = min($this->curSeek + $len, strlen($this->blobdata));
return substr($this->blobdata, $this->curSeek, $len);
}
function write($data)
{
$this->blobdata .= $data;
}
function close()
{
return $this->blobdata;
}
function length()
{
return strlen($this->blobdata);
}
}
function _performNewBlob($blobid=null)
{
$obj = new DbSimple_Mysql_Blob($blobid); // no need to use &
return $obj;
}
Just don't ever use =& operator. It's been useless since PHP 5.0 and removed in PHP 7.0:
http://php.net/manual/en/migration70.incompatible.php#migration70.incompatible.other.new-by-ref
You will find more things on that page that no longer work in PHP 7.

Unable to load page with codeigniter

On my codeigniter HMVC project. When I try to run my first isset statement in modules foreach section. If I uncomment the code below then, fire fox loads page error The connection was reset.
But if I comment out the code like below the page loads fine very strange.
//if (isset($part[0]) && $this->setting->get($part[0] . '_status')) {
// $data['modules'][] = Modules::run('catalog/module/'.$part[0].'/index');
//}
For some reason does not like using isset($part[0])
How code works
$part[0] it returns the module name example category
$part[1] it returns the module number example 66 category.66
$this->setting->get($part[0] . '_status') returns either 1 if
enabled or 0 if disabled.
What could be the cause of page not loading when I uncomment the code above. Any suggestions
Controller
<?php
class Column_left extends MX_Controller {
public function index() {
$this->load->model('catalog/extension/model_extension_extension');
$this->load->model('catalog/design/model_design_layout');
$route = $this->uri->segment(1).'/'.$this->uri->segment(2);
// $route outputs like pages/category
$layout_id = 0;
if (!$layout_id) {
$layout_id = $this->model_design_layout->get_layout($route);
}
if (!$layout_id) {
// Setting library autoloaded
$layout_id = $this->setting->get('config_layout_id');
}
$data['modules'] = array();
$modules = $this->model_design_layout->get_layout_modules($layout_id, 'column_left');
foreach ($modules as $module) {
$part = explode('.', $module['code']);
echo $part[0];
// Setting library autoloaded
if (isset($part[0]) && $this->setting->get($part[0] . '_status')) {
$data['modules'][] = Modules::run('catalog/module/'.$part[0].'/index');
}
if (isset($part[1])) {
$setting_info = $this->model_extension_module->get_module($part[1]);
if ($setting_info && $setting_info['status']) {
$data['modules'][] = Modules::run('catalog/module/'.$part[0].'/index', $setting_info);
}
}
}
// Setting library autoloaded
if (file_exists(DIR_TEMPLATE .$this->setting->get('config_template'). '/template/common/column_left_view.php')) {
$this->load->view('theme/'.$this->setting->get('config_template').'/template/common/column_left_view', $data);
} else {
$this->load->view('theme/default/template/common/column_left_view', $data);
}
}
}
View
<?php if ($modules) { ?>
<column id="column-left" class="col-sm-3 hidden-xs">
<?php foreach ($modules as $module) { ?>
<?php echo $module; ?>
<?php } ?>
</column>
<?php } ?>
After working on it all after noon, was able to find the cause of the issue.
In my catalog modules folder I had 2 controllers named the same in different folders, catalog/category & module/category. Even though they were in different folders one was over riding other and causing page load error on fire fox.
How I solved problem. By renaming the controller in subfolder catalog to categories I refreshed page and then works.
I also cleaned up code here.
<?php
class Column_left extends MX_Controller {
public function index() {
$this->load->model('catalog/design/model_design_layout');
$route = $this->uri->segment(1).'/'.$this->uri->segment(2);
$layout_id = 0;
if (!$layout_id) {
$layout_id = $this->model_design_layout->get_layout($route);
}
if (!$layout_id) {
$layout_id = $this->setting->get('config_layout_id');
}
$data['modules'] = array();
$results = $this->model_design_layout->get_layout_modules($layout_id);
foreach ($results as $result) {
$part = explode('.', $result['code']);
if (isset($part[0]) && $this->setting->get($part[0] . '_status')) {
$data['modules'][] = Modules::run('catalog/module/'.$part[0].'/index');
}
if (isset($part[1])) {
$this->load->model('catalog/extension/model_extension_module');
$setting_info = $this->model_extension_module->get_module($part[1]);
if ($setting_info && $setting_info['status']) {
$data['modules'][] = Modules::run('catalog/module/'.$part[0].'/index', $setting_info);
}
}
}
$this->load->view('theme/default/template/common/column_left_view', $data);
}
}

Why is my app running out of memory? Unsetting variables, using chunk

I have the simple app below. I'm turning off query logging in Laravel, I'm unsetting where possible, yet this function will only process about 800 records before I'm out of RAM on my 2GB Linode. I know I'm asking a lot of you guys but I can't seem to see where I'm leaking memory.
There are really only two major steps.
Step 1 - Move records from a temp table to production
class ListingMigrator
{
public function __construct($tempListing, $feed)
{
$this->tempListing = $tempListing;
$this->listing = $this->listingInstance();
$this->feed = $feed;
}
public static function migrateListing($listing, $feed)
{
$instance = new static($listing, $feed);
return $instance->migrate();
}
public function migrate()
{
$this->addExternalData();
$this->populateListing();
$this->processPhotos();
$this->deleteTempListing();
}
private function listingInstance()
{
DB::connection()->disableQueryLog();
$listing = Listing::findByMud($this->tempListing->matrix_unique_id);
return $listing ?: new Listing;
}
private function processPhotos()
{
$retsApi = new RetsFeedApi($this->feed);
/* Initialize Object */
$rets = $retsApi->findMostRecent();
$photos = $rets->getPhotosForListing($this->listing->matrix_unique_id);
foreach ($photos as $photo)
{
$uploader = new PhotoProcessor($this->listing, $photo);
$uploader->process();
}
}
private function populateListing()
{
DB::connection()->disableQueryLog();
$this->listing->fill($this->tempListing->toArray());
$this->listing->imported_at = $this->tempListing->created_at;
$this->listing->board = $this->tempListing->board;
return $this->listing->save();
}
private function addExternalData()
{
// Get Google lattitude and longitude
$googlecoords = getGoogleMapInfo($this->tempListing->FullAddress, $this->tempListing->City);
$this->listing->GoogleLat = $googlecoords['GoogleLat'];
$this->listing->GoogleLong = $googlecoords['GoogleLong'];
// Add or update the Subdivision Table (helper function)
$subdivisiondata = SubdivisionUpdate($this->tempListing->board, $this->tempListing->SubCondoName, $this->tempListing->Development);
$this->listing->SubdivisionID = $subdivisiondata['id'];
}
private function deleteTempListing()
{
return $this->tempListing->delete();
}
}
Step 2 - Download photos and reupload to Amazon S3
class PhotoProcessor
{
public function __construct(Listing $listing, $photoData)
{
$this->bucket = 'real-estate-listings';
$this->s3 = App::make('aws')->get('s3');
$this->tempFileName = 'app/storage/processing/images/retsphotoupload';
$this->photoData = $photoData;
$this->listing = $listing;
$this->photo = new RetsPhoto;
}
public function process()
{
$this->storeTempFile();
$this->storeFileInfo();
$this->buildPhoto();
$success = $this->pushToS3();
// if Result has the full URL or you want to build it, add it to $this->photo
DB::connection()->disableQueryLog();
$this->listing->photos()->save($this->photo);
$this->removeTempFile();
unset ($this->photoData);
return $success;
}
private function storeTempFile()
{
return File::put($this->tempFileName, $this->photoData['Data']) > 0;
}
private function storeFileInfo()
{
$fileInfo = getimagesize($this->tempFileName);
// Could even be its own object
$this->fileInfo = [
'width' => $fileInfo[0],
'height' => $fileInfo[1],
'mimetype' => $fileInfo['mime'],
'extension' => $this->getFileExtension($fileInfo['mime'])
];
}
private function buildPhoto()
{
$this->photo->number = $this->photoData['Object-ID']; // Storing this because it is relevant order wise
$this->photo->width = $this->fileInfo['width'];
$this->photo->height = $this->fileInfo['height'];
$this->photo->path = $this->getFilePath();
}
private function getFilePath()
{
$path = [];
if ($this->listing->City == NULL)
{
$path[] = Str::slug('No City');
}
else
{
$path[] = Str::slug($this->listing->City, $separator = '-');
}
if ($this->listing->Development == NULL)
{
$path[] = Str::slug('No Development');
}
else
{
$path[] = Str::slug($this->listing->Development, $separator = '-');
}
if ($this->listing->Subdivision == NULL)
{
$pathp[] = Str::slug('No Subdivision');
}
else
{
$path[] = Str::slug($this->listing->Subdivision, $separator = '-');
}
if ($this->listing->MLSNumber == NULL)
{
$pathp[] = Str::slug('No MLSNumber');
}
else
{
$path[] = Str::slug($this->listing->MLSNumber, $separator = '-');
}
$path[] = $this->photoData['Object-ID'].'.'.$this->fileInfo['extension'];
return strtolower(join('/', $path));
}
private function pushToS3()
{
return $this->s3->putObject([
'Bucket' => $this->bucket,
'Key' => $this->photo->path,
'ContentType'=> $this->fileInfo['mimetype'],
'SourceFile' => $this->tempFileName
]);
}
private function getFileExtension($mime)
{
// Use better algorithm than this
$ext = str_replace('image/', '', $mime);
return $ext == 'jpeg' ? 'jpg' : $ext;
}
private function removeTempFile()
{
return File::delete($this->tempFileName);
}
}
Edit to show RetsPhoto
class RetsPhoto extends Eloquent {
protected $table = 'rets_property_photos';
public function listing() {
return $this->belongsTo('Listing', 'matrix_unique_id', 'matrix_unique_id');
}
}
Edit #2: Chunk Call
This is in the app/command and the only thing in there is the fire() function below:
public function fire()
{
// Turn off query logging
DB::connection()->disableQueryLog();
$feeds = RetsFeed::where('active','=',1)->get();
foreach ($feeds as $feed)
{
$class = "TempListing{$feed->board}";
$listings = $class::orderBy('MatrixModifiedDT','desc');
$listings->chunk(50, function($listings) use($feed) {
$listings->each(function($listing) use ($feed) {
ListingMigrator::migrateListing($listing,$feed);
echo "Feed: $feed->board\r\n";
echo "SubcondoName: $listing->SubCondoName\r\n";
echo "Development: $listing->Development\r\n";
echo "\r\n";
});
});
}
}
I think I have figured it out.
Your system holds in memory all of the photo data. As witnessed by the unset ($this->photoData);
The problem is that you need to first complete the process function. Your application is not likely processing ANY photos so when you keep grabbing them from the file system you run out of memory BEFORE you even process a single one.
To Confirm this, simply grab 1 file not using the chunk method.
I am not very familar with Laravel, it could be grabbing all of the files all at once as well and eating the ram.
You can do some tracing with memory_get_usage(true) to find out exactly where the ram is getting eaten from. I would suggest analysing the fire method first.

Categories