How to display image from filestore/Field_Image in agiletoolkit? - php

I have a model Profile
<?php
class Model_Profile extends Model_Table {
public $table = 'profile';
function init(){
parent::init();
...
$m = $this->add('filestore/Field_Image');
}
}
Now in my index.php, i am trying to set the full image path in the template.
$m = $this->add('Model_Profile')->loadBy('user_id',$this->api->auth->get('id'));
//Here i want to var_dump the full image path example (/admin/upload/0/20131123014011__photo-on-23-11-2013-at-01.36.jpg)
This displays me the image id from the table filestore_file.
die('Filestore image:' . $m->get('filestore_field_image'));
Discussion related to this topic:
https://groups.google.com/forum/#!topic/agile-toolkit-devel/i-i9OK4Ya1A
I tried this
$fm = $this->add('filestore/Field_Image')->load($m->get('filestore_field_image'));
var_dump($fm->getThumbURLExpr());
but it threw an exception
Other links :
FileStore step by step example with 4.2.1
How to use file upload in agile toolkit 4.2
How to use Filestore in API in Agiletoolkit?

I figured out, its like this:
$m = $this->add('Model_Profile')->loadBy('user_id',$this->api->auth->get('id'));
$mf = $this->add('filestore/Model_File');
$mf->load($m->get('filestore_field_image'));
var_dump($mf->get('url'));

Related

PHP - How to pass class to another class method

Im trying to pass the verot image editing class to a custom class that I created, but it doesnt seem to work, it doesnt do anything when I try to run it. How do I pass the verot image class to my class?
//Edit.php
//Now I run my class
$process = new ProcessEventLogo();
$process->editEventLogo($event_id,$file_ext,$savepath,new upload(''));
Here is my custom class. I thought by running upload('') to this method, Im passing a copy of the verot upload class that I can access in my custom class method. But when I run it, it doesnt even get past the $mainimg->uploaded path. In fact $mainimg = $fileupload->upload($savefile); returns NULL when I var_dump it. What am I doing wrong?
class ProcessEventLogo {
public function editEventLogo($eventid,$fext,$url,$savepath,$fileupload)
{
//We generate the file name to save this image to
$savefile = $savepath .'event_' .$eventid .'.' .$fext;
//We check to see if the event image is there
$mainimg = $fileupload->upload($savefile);
//We now resize the image
if($mainimg->uploaded)
{
$mainimg->file_overwrite = TRUE;
$mainimg->image_ratio_crop = TRUE;
$mainimg->image_resize = TRUE;
$mainimg->image_x = 50;
$mainimg->image_y = 50;
$mainimg->process($savefile);
if($mainimg->processed)
{
echo $mainimg->error;
}
}
}
It appears this worked, can someone verify this is the proper way of doing this? So instead of this line:
//We check to see if the event image is there
$mainimg = $fileupload->upload($savefile);
This worked.
//We check to see if the event image is there
$mainimg = new $fileupload($savefile);
#mr.void Right, so how else would I pass this class to my custom class
method? Just seems like this is the wrong way
I think writing a little Factory is the right way for this:
class uploadFac {
public function getUploadInstance($file)
{
return new upload($file)
}
}
And use it like this:
$uplFac = new uploadFac();
$process = new ProcessEventLogo();
$process->editEventLogo($event_id,$file_ext,$savepath,$uplFac);
and in the method:
public function editEventLogo($eventid,$fext,$url,$savepath,$uplFac)
{
//We generate the file name to save this image to
$savefile = $savepath .'event_' .$eventid .'.' .$fext;
$mainimg = $uplFac->getUploadInstance($savefile);

Image is saving as tmp file in database laravel

Before explaining the problem. Let me show the controller function:
public function storePost(IdeaRequest $request)
{
$idea = new Idea();
$idea->idea_title = $request->input('idea_title');
$idea->user_id = $request->input('user_id');
$idea->idea_image = $request->file('idea_image')->move('publicPages\images')->getClientOriginalName();
$idea->idea_info = $request->input('idea_info');
$idea->selection = $request->input('selection');
$idea->idea_location = $request->input('idea_location');
$idea->idea_goal = $request->input('idea_goal');
$idea->idea_description = $request->input('idea_description');
$idea->save();
session()->flash('flash_message', 'Your idea has been submitted for Review');
return back();
}
It stores the image as .tmp file. Things I have tried out
guessExtension(), It just returns the extension name and does not
even store the image.
getClientOriginalName(), it throws an error;
getClientOriginalName method is not defined. I have searched the
method and it is in there. used its namespace
Symfony\Component\HttpFoundation\File\UploadedFile. it did not work
either.
Tried different things out from stackoverflow, Nothing has worked
for me till now. In other words, i have invested a lot of time to
solve this problem but nothing worked. Any help would highly be
appreciated.
Here you go:
public function storePost(IdeaRequest $request)
{
$request->file('idea_image')->move('publicPages\images');
$filename = $request->file('idea_image')->getClientOriginalName();
$idea = new Idea();
$idea->idea_title = $request->input('idea_title');
$idea->user_id = $request->input('user_id');
$idea->idea_image = $filename;
$idea->idea_info = $request->input('idea_info');
$idea->selection = $request->input('selection');
$idea->idea_location = $request->input('idea_location');
$idea->idea_goal = $request->input('idea_goal');
$idea->idea_description = $request->input('idea_description');
$idea->save();
session()->flash('flash_message', 'Your idea has been submitted for Review');
return back();
}
Observe closely. You will have to do this in 2 separate lines:
$request->file('idea_image')->move('publicPages\images');
$filename = $request->file('idea_image')->getClientOriginalName();
Why?
move() returns a File object representing the new file.
The File class doesn't have a getClientOriginalName() method. That method belongs to UploadedFile.
When you chain them, you are trying to access getClientOriginalName() from File, which doesn't exist. Have a look at the docs here.
However...
File extends the PHP native SplFileInfo class which has a getFilename() method. So I guess you could also do:
$request->file('idea_image')->move('publicPages\images')->getFilename();

error not found model after upload into hosting

In localhost, it works fine. After uploading into my hosting i got this error. I'm using zend framework 1.12.
Fatal error: Class 'Application_Models_DBTable_Kereta' not found in /home/mysite/public_html/application/controllers/CarController.php
others post said the problem is because the case sensitivity of file names. But i tried to change and nothing happens. See my attachment for the structured of my project. The attachment shown application and model names.
edited : This problem occurs to all my models class.. can't find models..
Controller code :
class CarController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
//klu login sbgai admin, papar layout admin, klu login sbgai user, papar layout user laaaa,
Zend_Session::start();//start session
$session = new Zend_Session_Namespace("MyNamespace");
$id_pengguna = $session->id_pengguna;
$kategori = $session->kategori;
if($kategori==3)
{
$this->_helper->layout->setLayout('layoutadmin');
}
else
{
}
}
public function indexAction()
{
// $albums = new Application_Model_DbTable_Albums();
//$this->view->albums = $albums->fetchAll();
}
public function reservationAction()
{
if($this->getRequest()->isPost())
{
$jadual_tarikhmula = $this->getRequest()->getPost("jadual_tarikhmula");
$jadual_tarikhtamat = $this->getRequest()->getPost("jadual_tarikhtamat");
$jadual_masamula1 = $this->getRequest()->getPost("jadual_masamula1");
$jadual_masamula2 = $this->getRequest()->getPost("jadual_masamula2");
$jadual_masatamat1 = $this->getRequest()->getPost("jadual_masatamat1");
$jadual_masatamat2 = $this->getRequest()->getPost("jadual_masatamat2");
$simpan = array($jadual_tarikhmula,$jadual_tarikhmula,$jadual_masamula1,$jadual_masatamat1);
$papar = $this->view->dataReserve= $simpan;
$db = new Application_Model_DbTable_Kereta();
$paparkereta = $this->view->reserve = $db->getReservationCar($jadual_tarikhmula,$jadual_tarikhmula,$jadual_masamula1,$jadual_masatamat1);
$this->view->dataWujud = count($paparkereta);
$gambar = $this->view->gambarKereta = $db->getGambarKereta($paparkereta[0]['id_kereta'],false);
}
}
}
Your folder name is DbTable and your model class name is ..._DBTable_... ?
Note that Linux is case-sensitive directory or filename.
And did you add this line to .ini file?
Appnamespace = "Application"
Are you sure you don't have another class named Kereta in /home/mysite/public_html/application/controllers/CarController.php ?
Also, consider cleaning the symfony cache with symfony cc. It's the cache for autoload that is giving you this error I guess.

Why I can't load models in modules?! [Using HMVC in codeigniter 2.1.2]

I hope someone can help me with this one before I jump off the window. I spent few hours on this one and don't know what am I doing wrong.
Basically, I've installed HMVC in CodeIgniter 2.1.2 and everything works fine, BUT for some reason I can't load models the same way I'm doing it in standard controllers. In the old codeigniter 1.7.1 I could use it simply by invoking $this->load->model('my_model') but now I can't?!
Every single time I'm trying to load model I get this error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Special_cart::$db
Filename: core/Model.php
Line Number: 51
I have had installed it step-by-step according to the instructions. I got third_party next to modules folder. In modules I have few modules stored like this:
modules
--boxes
----controller
----models
----views
I invoke module in my code like this:
<?=modules::run('boxes/special_cart/index');?>
My module controller code looks like this:
class Special_cart extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
if ($this->session->userdata('cart'))
{
# get product id's and each quantity
$cart_product_list = array_count_values($this->session->userdata('cart'));
# get list of product_id
$product_list = array_keys($cart_product_list);
# get product details
$this->load->model('productmodel');
$this->load->model('stockmodel');
$cart_products = $this->productmodel->cart_get_products_details($product_list);
$final_cart_array = array();
foreach($cart_products as $cart_product){
$product_stock = $this->stockmodel->view_product_stock($cart_product["id"]);
if(empty($product_stock) || $product_stock["UNITS"]<=0)
$cart_product["UNITS"] = 0;
else{
if($cart_product_list[$cart_product["id_web"]]>$product_stock["UNITS"])
$cart_product["UNITS"] = $product_stock["UNITS"];
else{
$cart_product["UNITS"] = $cart_product_list[$cart_product["id_web"]];
}
}
$final_cart_array[] = $cart_product;
}
$refresh_cart_array = array();
foreach($final_cart_array as $cart_product){
for($i=1;$i<=$cart_product["UNITS"];$i++){
$refresh_cart_array[] = $cart_product["id_web"];
}
}
$this->load->view("special_cart",array(
'refresh_cart_array' => $refresh_cart_array,
'final_cart_array' => $final_cart_array
));
} else {
$this->load->view("special_cart",array(
'refresh_cart_array' => NULL,
'final_cart_array' => NULL
));
}
}
}
I've tried every possible solution found on internet - none of them work....
I hope you understand my problem but in case you need some further explanation please ask me. Can anyone help?
Looks like the model you're trying to load wants to connect to the db, but the database driver is not available. If you use database queries in your application, why don't you load the database driver automatically?
Just insert "database" in the "libraries" array in application/config/autoload.php file. Don't forget to insert your database credentials into application/config/database.php.
$autoload['libraries'] = array('database');
If you need database connection just in one single model, load it before trying to access database library.
$this->load->database();
Try loading the model stating the module name as follows
$this->load->model('module_name/productmodel');
Class Models extends MX_Loader{
function getUser($username){
$sql="SELECT * FROM user WHERE username = ? ";
return $this->db->query($sql,array($username))->row();
}
}
you must using extends MX_Loader because i don't know if using CI_Model the database core cant be load in Codeigniter,,,
Try to use extend MX_Controller class (not CI_Contoller like you are doing atm)
Based on what you have wrote in comment above, I figured that you tried to create new instance of DB in module (based on chrises comment).
Do it on constuctor of Special_cart
So update current construct to be like
public function __construct()
{
parent::__construct();
$this->load->database('default');
}
(I'm writing this from top of my head, so check the methods)
Now for sure db driver should be available in your models.
Regarding issue with HMVC I dont think there are any. I'm using HMVC for a while now, and I found no problems in it (working with databases)
I had the same problem and mistake. I missed to extend controllers to MX_Controller. So the solution would be to change CI_Controller to MX_Controller like this:
class Special_cart extends MX_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('productmodel');
$this->load->model('stockmodel');
}
public function index()
{
if ($this->session->userdata('cart'))
{
# get product id's and each quantity
$cart_product_list = array_count_values($this->session->userdata('cart'));
# get list of product_id
$product_list = array_keys($cart_product_list);
# get product details
$cart_products = $this->productmodel->cart_get_products_details($product_list);
$final_cart_array = array();
foreach($cart_products as $cart_product){
$product_stock = $this->stockmodel->view_product_stock($cart_product["id"]);
if(empty($product_stock) || $product_stock["UNITS"]<=0)
$cart_product["UNITS"] = 0;
else{
if($cart_product_list[$cart_product["id_web"]]>$product_stock["UNITS"])
$cart_product["UNITS"] = $product_stock["UNITS"];
else{
$cart_product["UNITS"] = $cart_product_list[$cart_product["id_web"]];
}
}
$final_cart_array[] = $cart_product;
}
$refresh_cart_array = array();
foreach($final_cart_array as $cart_product){
for($i=1;$i<=$cart_product["UNITS"];$i++){
$refresh_cart_array[] = $cart_product["id_web"];
}
}
$this->load->view("special_cart",array(
'refresh_cart_array' => $refresh_cart_array,
'final_cart_array' => $final_cart_array
));
} else {
$this->load->view("special_cart",array(
'refresh_cart_array' => NULL,
'final_cart_array' => NULL
));
}
}
}
this is also explained in the documentation
https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/
, here the quote:
Notes:
To use HMVC functionality, such as Modules::run(), controllers must
extend the MX_Controller class. To use Modular Separation only,
without HMVC, controllers will extend the CodeIgniter Controller
class. You must use PHP5 style constructors in your controllers. ie:
<?php
class Xyz extends MX_Controller
{
function __construct()
{
parent::__construct();
}
}

Apply styles using php file in Zend?

NOTE: UPDATED MY QUESTION
I am using zend.I have the file "stylesettings.php" under css folder. Having the following line to convert php file to css.
header("Content-type: text/css");
stylesetting.php is under application/css/stylesettings.php
Now i want to get the color code from my DB in stylesettings.php.Here i can write basic DB connection code to get values from DB. I guess there might be another way to get all DB values by using zend. How can we connect DB like "Zend_Db_Table_Abstract" in stylesettings file ?
Is it possible to use zend component in that file ? Kindly advice on this.
I hope you understand.
You are breaking the basic separated model assumed in a MVC framework. For such color code, if it's only used in one place, I would suggest that you output the color in the "style="color: <color>"" in-line style in order to keep the dynamic part in HTML and your CSS file static.
If you really want to do this, then you should consider output your dynamic stylesheet in a URL path other than css, and use the controller/views etc to generate the stylesheet.
i am using the following way to apply color settings in layout.phtml
Use below code in bootstrap file
<?php
require_once 'plugins/StyleController.php';
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
protected function _initRouter() {
$front = Zend_Controller_Front::getInstance ();
$front->setControllerDirectory ( dirname ( __FILE__ ) . '/controllers' );
$router = $front->getRouter ();
$front->registerPlugin ( new StyleController ( $router ) );
}
}
Created folder plugins under application and create a new file called stylecontroller.php
<?php
class StyleController extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$layout = Zend_Layout::getMvcInstance();
$view = $layout->getView();
/* code for getting color settings */
$this->settings = new Admin_Model_DbTable_Settings();
$view->colorsettings = $this->settings->getStyleSettings();
//print_obj($view->colorsettings);
}
}
?>
Also to get color code,
<?php
class Admin_Model_DbTable_Settings extends Zend_Db_Table_Abstract
{
public function getStyleSettings()
{
$select = $this->_db->select()
->from('style_settings');
$result = $this->getAdapter()->fetchAll($select);
return $result['0'];
}
}
?>
By using above code i can able to use $this->colorsettings in layout page.
This one fix my dynamic color in layout but not in other template files.

Categories