Help (php amateur hour)

From: CHYRON (DSMITHHFX)13 Apr 2012 21:25
To: ALL1 of 7
I'm trying to insert an if statement into an array constructor which (as it turns out) is part of a class definition which (as it turns out) you can't do that, except by x,y and z.

The object is to conditionally hide a control panel thingy 'categories', which is part of a larger array, from non-admin users:
code:
//hide from non-admins
if ($this->current_user->group) == 'admin')
{
	'categories' => array(
		'name' => 'cat_list_title',
		'uri' => 'admin/blog/categories',
		'shortcuts' => array(
			array(
				'name' => 'cat_create_title',
				'uri' => 'admin/blog/categories/create',
				'class' => 'add'
			),
		),
	),
} //end hide


which is throwing the error "Parse error: syntax error, unexpected T_IF, expecting ')'", probably because... you can't do that, I read in several different places, e.g.
You can't put "naked" PHP code in a class definition. YOu'll have to wrap it in a method, probably a constructor.
.

Who has the magic coding pixie dust?
From: Matt14 Apr 2012 10:33
To: CHYRON (DSMITHHFX) 2 of 7
You can't put control structures in the class definition and you can't put them inside array constructs either.

The little bit of code you've posted tells me you're using Wordpress, but without the whole file it's difficult to know if what you're trying to do will work.

In PHP (and other OO languages) there are things to consider like visibility of the array (whether or not it is public and thus might be accessible before your if statement happens, or protected or private) and whether it's static or a class property and is accessible via getters and whether the class itself is static and a lot more.

If you post which file from Wordpress you're trying to modify, we'd be in a better positioned to be able to help you.
From: CHYRON (DSMITHHFX)14 Apr 2012 12:39
To: Matt 3 of 7
It's pyrocms. I was going to zip up the file and post a link but it's really quite small so I'll just post it here. Dude at pyro forums says I need to break it up into separate arrays, and then string the bits back together using control structures at that point. My php might be a bit shaky for that, so I'm considering other possibilities.

Anyway, here it is:
code:
<?php defined('BASEPATH') or exit('No direct script access allowed');
 
class Module_Blog extends Module {
 
	public $version = '2.0';
 
	public function info()
	{
		return array(
			'name' => array(
				'en' => 'Opportunity Listings',
				'ar' => 'المدوّنة',
				'el' => 'Ιστολόγιο',
				'br' => 'Blog',
				'pl' => 'Blog',
				'he' => 'בלוג',
				'lt' => 'Blogas',
				'ru' => 'Блог',
				'zh' => '文章',
				'id' => 'Blog'
			),
			'description' => array(
				'en' => 'Add/edit/delete listings.',
				'nl' => 'Post nieuwsartikelen en blogs op uw site.',
				'es' => 'Escribe entradas para los artículos y blog (web log).', #update translation
				'fr' => 'Envoyez de nouveaux posts et messages de blog.', #update translation
				'de' => 'Veröffentliche neue Artikel und Blog-Einträge', #update translation
				'pl' => 'Dodawaj nowe wpisy na blogu',
				'br' => 'Escrever publicações de blog',
				'zh' => '發表新聞訊息、部落格等文章。',
				'it' => 'Pubblica notizie e post per il blog.', #update translation
				'ru' => 'Управление записями блога.',
				'ar' => 'أنشر المقالات على مدوّنتك.',
				'cs' => 'Publikujte nové články a příspěvky na blog.', #update translation
				'sl' => 'Objavite blog prispevke',
				'fi' => 'Kirjoita uutisartikkeleita tai blogi artikkeleita.', #update translation
				'el' => 'Δημιουργήστε άρθρα και εγγραφές στο ιστολόγιο σας.',
				'he' => 'ניהול בלוג',
				'lt' => 'Rašykite naujienas bei blog\'o įrašus.',
				'da' => 'Skriv blogindlæg',
				'id' => 'Post entri blog'
			),
			'frontend'	=> TRUE,
			'backend'	=> TRUE,
			'skip_xss'	=> TRUE,
			'menu'		=> 'content',
 
			'roles' => array(
				'put_live', 'edit_live', 'delete_live'
			),
 
			'sections' => array(
			    'posts' => array(
				    'name' => 'blog_posts_title',
				    'uri' => 'admin/blog',
				    'shortcuts' => array(
						array(
					 	   'name' => 'blog_create_title',
						    'uri' => 'admin/blog/create',
						    'class' => 'add'
						),
					),
				),
				'categories' => array(
				    'name' => 'cat_list_title',
				    'uri' => 'admin/blog/categories',
				    'shortcuts' => array(
						array(
						    'name' => 'cat_create_title',
						    'uri' => 'admin/blog/categories/create',
						    'class' => 'add'
						),
				    ),
			    ),
		    ),
		);
	}
 
	public function install()
	{
		$this->dbforge->drop_table('blog_categories');
		$this->dbforge->drop_table('blog');
 
		$blog_categories = "
			CREATE TABLE " . $this->db->dbprefix('blog_categories') . " (
			  `id` int(11) NOT NULL auto_increment,
			  `slug` varchar(20) collate utf8_unicode_ci NOT NULL default '',
			  `title` varchar(20) collate utf8_unicode_ci NOT NULL default '',
			  PRIMARY KEY  (`id`),
			  UNIQUE KEY `slug - unique` (`slug`),
			  UNIQUE KEY `title - unique` (`title`),
			  KEY `slug - normal` (`slug`)
			) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Blog Categories.';
		";
 
		$blog = "
			CREATE TABLE " . $this->db->dbprefix('blog') . " (
			  `id` int(11) NOT NULL auto_increment,
			  `title` varchar(100) collate utf8_unicode_ci NOT NULL default '',
			  `slug` varchar(100) collate utf8_unicode_ci NOT NULL default '',
			  `category_id` int(11) NOT NULL,
			  `attachment` varchar(255) collate utf8_unicode_ci NOT NULL default '',
			  `intro` text collate utf8_unicode_ci NOT NULL,
			  `body` text collate utf8_unicode_ci NOT NULL,
			  `parsed` text collate utf8_unicode_ci NOT NULL,
			  `keywords` varchar(32) NOT NULL default '',
			  `author_id` int(11) NOT NULL default '0',
			  `created_on` int(11) NOT NULL,
			  `updated_on` int(11) NOT NULL default 0,
              `comments_enabled` INT(1)  NOT NULL default '1',
			  `status` enum('draft','live') collate utf8_unicode_ci NOT NULL default 'draft',
			  `type` set('html','markdown','wysiwyg-advanced','wysiwyg-simple') collate utf8_unicode_ci NOT NULL,
			  PRIMARY KEY  (`id`),
			  UNIQUE KEY `title` (`title`),
			  KEY `category_id - normal` (`category_id`)
			) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Blog posts.';
		";
 
		if ($this->db->query($blog_categories) && $this->db->query($blog))
		{
			return TRUE;
		}
	}
 
	public function uninstall()
	{
		//it's a core module, lets keep it around
		return FALSE;
	}
 
	public function upgrade($old_version)
	{
		// Your Upgrade Logic
		return TRUE;
	}
 
	public function help()
	{
		/**
		 * Either return a string containing help info
		 * return "Some help info";
		 *
		 * Or add a language/help_lang.php file and
		 * return TRUE;
		 *
		 * help_lang.php contents
		 * $lang['help_body'] = "Some help info";
		*/
		return TRUE;
	}
}
 
/* End of file details.php */
 
EDITED: 14 Apr 2012 12:39 by DSMITHHFX
From: Matt14 Apr 2012 13:02
To: CHYRON (DSMITHHFX) 4 of 7
Try this (not tested, but should give you an idea):

php code:
<?php defined('BASEPATH') or exit('No direct script access allowed');
 
class Module_Blog extends Module {
 
    public $version = '2.0';
 
    public function info()
    {
        $info = array(
            'name' => array(
                'en' => 'Blog',
                'ar' => 'المدوّنة',
                'el' => 'Ιστολόγιο',
                'br' => 'Blog',
                'pl' => 'Blog',
                'he' => 'בלוג',
                'lt' => 'Blogas',
                'ru' => 'Блог',
                'zh' => '文章',
                'id' => 'Blog'
            ),
            'description' => array(
                'en' => 'Post blog entries.',
                'nl' => 'Post nieuwsartikelen en blogs op uw site.',
                'es' => 'Escribe entradas para los artículos y blog (web log).', #update translation
                'fr' => 'Envoyez de nouveaux posts et messages de blog.', #update translation
                'de' => 'Veröffentliche neue Artikel und Blog-Einträge', #update translation
                'pl' => 'Dodawaj nowe wpisy na blogu',
                'br' => 'Escrever publicações de blog',
                'zh' => '發表新聞訊息、部落格等文章。',
                'it' => 'Pubblica notizie e post per il blog.', 
…[Message Truncated] View full message.
EDITED: 14 Apr 2012 13:03 by MATT
From: CHYRON (DSMITHHFX)14 Apr 2012 16:45
To: Matt 5 of 7
That removes the categories tab from everyone, including admins. At least it doesn't throw an error.

Let me try this again. I missed a bit of your code, so I just pasted in the whole thing. Now it behaves correctly, but is throwing the following errors (which are appearing on every page):
---
Message: Undefined property: CI::$current_user
Filename: libraries/Module.php
Line Number: 137
---
AND
---
Message: Trying to get property of non-object
Filename: blog/details.php
Line Number: 78
---
the details.php array is somehow referenced by Module.php, I guess.
EDITED: 14 Apr 2012 16:55 by DSMITHHFX
From: Matt14 Apr 2012 17:11
To: CHYRON (DSMITHHFX) 6 of 7
I don't know PyroCMS nor codeignitor so I'm not going to be able to help you beyond the basic PHP syntax.

I can tell you that the error means the Module_Blog class nor it's parent Module class have a property named current_user, which is referenced in the if statement you had in your first post and which I simply copied and pasted wholesale into my example.

If you can find some other code in PyroCMS to get the current user then you can possibly copy that to use that and modify the if statement to use that code.

Honestly, you'll be better off asking the PyroCMS team / community I reckon.
From: CHYRON (DSMITHHFX)14 Apr 2012 17:26
To: Matt 7 of 7
OK thanks. I thought it might be a (relatively) simple syntax issue, as is often the case with my hacking efforts.