I am new to Laravel. I have a table of information and a master view page (master.blade.php). All the other views extend it. I want to see if certain session variables exist and if not set them. This is to stop reading a table unnecessarily. In standard PHP I would write:
<?php
if ( !isset($_SESSION['name'] )) {
$db = new smplPDO();
$co = $db->get_row('config',array(), array() );
$_SESSION['name'] = $co['name'] ;
$_SESSION['thisyear'] = ReturnCurrentYear() ;
}
?>
How can I test the master view and set them if necessary so they can be used elsewhere?
I've worked it out:
if ( !session()->has('name'))
{
$db = new smplPDO();
$co = $db->get_row('config',array(), array() );
session()->put('name',$co['name']);
session()->put('thisyear',ReturnCurrentYear());
}
Related
I am working on codeigniter. Controller name is "project" and 4 functions are there.."get_city_data", "get_store_data", "get_currency", "get_description".
Parameters I am passing in above 2 functions are - get_city_data($state, $city), get_place_data($state, $city, $store).
So on browser to render these functions I am using the urls respectively as-
http://localhost/project/get_city_data/state1/city1
I want to change the urls like
http://localhost/state1/city1
In routes.php if I define a route like this
$route['(:any)/(:any)'] = 'project/get_city_data/$1/$2' then it reflects for all other functions as well but I want to change urls for these 2 functions only.
Also I do not want to use _remap function as it doesn't go well with my needs.
Can anybody help me in this?
ThankYou
I suggest you to check in the database at first, then determine the routes required. Here is my answer for your requirement.
In your routes.php, try this code:
require_once( BASEPATH . 'database/DB' . EXT );
$url_source = $_SERVER['REDIRECT_URL'];
$arr_url = explode("/", $url_source);
$state_name = $arr_url[3];
$city_name = $arr_url[4];
$store_name = $arr_url[4];
$db = & DB();
//Let say grab 3 columns to be check
$db->select('state_name, city_name, store_name');
if(!empty($state_name)):
$db->where('state_name', $state_name);
endif;
if(!empty($city_name)):
$db->where('city_name', $city_name);
endif;
if(!empty($store_name)):
$db->where('store_name', $store_name);
endif;
$query = $db->get('table', 1);
$obj = $query->row();
if ($obj):
$route["$obj->state_name/$obj->city_name/$obj->store_name"] = "project/get_city_data/$state_name/$city_name/$store_name";
endif;
You can try by this url to test:
http://localhost/state_name/city_name/store_name
I got it solved using regex.
In routes.php i wrote
$url_source = $_SERVER['REQUEST_URI'];
if(!(preg_match('%^/project/get_description\?state=[a-zA-Z_-]*%', $url_source) || preg_match('%^/project/get_currency\?state=[a-zA-Z_-]*%', $url_source))){
if(!preg_match('%^\/[a-zA-Z_-]+\/[a-zA-Z_-]+\/[a-zA-Z_-]+$%', $url_source)){
$route['(:any)/(:any)'] = 'project/get_city_data/$1/$2';
}else{
$route['(:any)/(:any)/(:any)'] = 'project/get_store_data/$1/$2/$3';
}
}
This way I was able to route for particular functions.
I am trying to integrate jomsocial events with jomsocial groups.
What i am trying to achieve is to automatically create a group when the event is being created.
Would anyone have some hints regarding such functionality?
The approach i have in mind is to utilize a function onEventCreate($event) from jomsocial API
to call the group creation mechanism. Is that the right way to do it?
Yes, this is the approach I'd take.
You can find method save() in groups.php controller. There you've got all of the required code to implement this.
The rough code :
$my =& CFactory::getUser();
$config =& CFactory::getConfig();
$group =& JTable::getInstance( 'Group' , 'CTable' );
$group->name = $name;
$group->description = $description;
$group->categoryid = $categoryId; // Category Id must not be empty and will cause failure on this group if its empty.
$group->website = $website;
$group->ownerid = $my->id;
$group->created = gmdate('Y-m-d H:i:s');
$group->approvals = 0;
$params = new CParameter( '' );
// Here you need some code from private _bindParams()
$group->params = $params->toString();
$group->published = ( $config->get('moderategroupcreation') ) ? 0 : 1;
$group->store();
// Other useful stuff:
// - store the creator / admin into the groups members table
// - add into activity stream
Fledgling Joomla / PHP developer, hitting a wall understanding how to do this. Everything I found searching has been for older versions of Joomla or other frameworks and so it's all confusing the first time around.
I want to have a helper function that I can call from anywhere in my component. Basically it takes a userID input and returns their full name, let's say hair color and height. Here's the function:
function get_profile_info($userID) {
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$query->SELECT('u.id as UserID
, u.name AS Name
, up.profile_value AS Hair
, up2.profile_value AS Height
');
$query->FROM (' #__users AS u');
$query->LEFTJOIN (' #__user_profiles AS up ON u.id = up.user_id AND up.ordering = 1');
$query->LEFTJOIN (' #__user_profiles AS up ON u.id = up.user_id AND up.ordering = 2');
$query->WHERE(' u.id = '.$userID.'');
$query->GROUPBY (' u.id
, u.name
, up.profile_value
');
$db->setQuery($query);
return $query;
}
I'd put this in a file called "lookups.php" within the "helpers" folder of my component...but here's where I'm not sure what to do next. Top of lookups.php has the obligatory:
<?php defined ( '_JEXEC' ) or die;
So two questions:
1) Do I put everything in a class or keep it as a series of functions (since there will be others)?
2) How do I pass the userID and get back the values of Name, Hair, and Height in the view (view.html.php / default.php)?
Thank you!
==================================
Edit: Based on #Shaz 's response below here's where I'm at (again, just starting off and trying to wrap my head around some of this):
lookups.php
abstract class LookupHelper {
var $Name;
var $Hair;
var $Height;
public function get_profile_info($userID) {
...
(same as above until the next line)
$db->setQuery($query);
$result=$db->loadRow();
$Name = $result[1];
$Hair = $result[2];
$Height = $result[3];
$getprofileinfo = array(Name=>$Name, Hair=>$Hair, Height=>$Height);
$return $getprofileinfo;
}
}
Then in my default.php (will probably move to view.html.php):
$getprofileinfo = Jview::loadHelper('lookups'); //got an error without this
$getprofileinfo = LookupHelper::get_profile_info($userID);
$Name = $getprofileinfo[Name];
$Hair = $getprofileinfo[Hair];
$Height = $getprofileinfo[Height];
So...it's working - but it seems like there's a much easier way of doing this (specifically manually creating an array then recalling by position). Thoughts?
Thank you!!
Create the helper class and include there all your functions:
abstract class HelloWorldHelper
{
/* functions */
}
Call the function and store the result:
$var = HelloWorldHelper::get_profile_info($thatId);
Is not possible to write your helper class inside of an existing helper Joomla file that's already being calling by all your components?
By default Joomla have helper classes doing stuff, so all you have to do is to expand the core helper classes with your owns.
I have two forms: (login form, register form) and they submitted to my component.
Login form:
I want to check login and pass and set status to "logged" in joomla.
Register form:
I want register new user in joomla.
How to do it?
$inplogin = JRequest::getVar('inplogin');
$inppass = JRequest::getVar('inppass');
???? JFactory::login($inplogin, $inppass); ????
???? JFactory::registeruser($inplogin, $inppass); ????
???
For Login
$result = JFactory::getApplication()->login(array('username'=>$username, 'password'=>$password));
For Registration
require_once JPATH_ROOT.DS.'components'.DS.'com_users'.DS.'models'.DS.'registration.php';
$model = new UsersModelRegistration();
jimport('joomla.mail.helper');
// Attempt to save the data.
jimport('joomla.user.helper');
$username = ; //your user name here
$password = ; //your password here
$data = array( 'username'=>$email,'name'=>$email,'email1'=>$email,'password1'=>$password, 'password2'=>$password, 'block'=>1 );
$return = $model->register($data);
I don't see how this is related specifically to joomla.
you need to use the same $_SESSION variable in both cases. after you declare the variable, you can check on the beginning of your index page if this var exists (and if so, whether the credentials are correct or not). something like:
<?php
session_start();
if (!isset($_SESSION['logged']) {
header("location:login.php");
exit();
}
?>
you can use JUser::bind($userData) and JUser::save($userData) for registering the user.
Expanding upon Gaurav's answer:
If you're using just the Joomla framework and you don't have a site loaded, you will still need to initialize the site.
That said, these two lines should do the trick:
$app = JFactory::getApplication('site');
$app->initialise();
I'm sorry if this question is trivial but I've been struggling to find what I am doing wrong here. I am trying to change the value of an attribute on a store view level but the default is also changed whereas it shouldn't be. Of course, this attribute is set up to be "store-view-scoped". To keep it simple, I've tried with the product name. No success.
Below are unsuccessful tests I've tried...
Do you see what I am doing wrong here?
Many thanks.
My tries :
$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->setStoreId(STORE_ID)->setName('new_name')->save();
$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->setStoreId(STORE_ID)->setStore(STORE_CODE)->setName('new_name')->save();
$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->setStoreId(STORE_CODE)->setName('new_name')->save();
$product = Mage::getModel('catalog/product')->setStoreId(STORE_ID)->load(PRODUCT_ID);
$product->setName('new_name')->save();
$product = Mage::getModel('catalog/product')->setStoreId(STORE_ID)->load(PRODUCT_ID);
$product->setStoreId(STORE_ID)->setName('new_name')->save();
I even tried by adding the line below before the product model load...
Mage::app()->setCurrentStore(STORE_ID);
So here is the complete snippet to change attribute value for a specific product attribute on a specific store view. Example with the product name :
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->setStoreId(STORE_ID)->setName('my_new_product_name')->save();
And as an additional answer, one could be interested in changing the attribute value to the default one. In this case, the argument 'false' must be passed to the setAttribute :
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->setStoreId(STORE_ID)->setName(false)->save();
You need to set the current store to admin at the top of your code block:
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
note when loading product with data for some store view, also default values get loaded. saving such product will save default values as store values (thus unset "Use Default Value" for fields)
i ended up with following function to clean-up product data from default values
public static function _removeDefaults($item) {
static $attributeCodes = null;
if($attributeCodes == null) {
$attributes = Mage::getSingleton('eav/config')
->getEntityType(Mage_Catalog_Model_Product::ENTITY)->getAttributeCollection();
$attributeCodes = array();
foreach($attributes as $attribute) {
$ac = $attribute->getAttributeCode();
if(in_array($ac, array('sku','has_options','required_options','created_at','updated_at','category_ids'))) {
continue;
}
$attributeCodes[] = $ac;
}
}
foreach($attributeCodes as $ac) {
if(false == $item->getExistsStoreValueFlag($ac)) {
$item->unsetData($ac);
}
}
}
remember to send only product loaded for some store view