Category: PHP

10/12/09

Permalink 02:27:05 pm, by admin Email , 171 words, 2699 views   English (US)
Categories: PHP

Button to "Select All" checkboxes in P4A

Given the following which creates a set of checkboxes:

$db =& P4A_DB::singleton();
$clientlist = array();
$items = $db->fetchAll("SELECT * FROM microsites WHERE status=1 ORDER BY sitetitle");
foreach ($items as $item) {
$ci = $item['user'];
$cd = $item['sitetitle'];
$clientlist[] = array('id' => $ci, 'desc' => $cd);
}

$client_array_source =& $this->build("p4a_array_source", "client_array_source");
$client_array_source->load($clientlist)
->setPk("id");


$selectallbutton = $this->build("p4a_button","selectallbutton")
->setLabel('Select All');
$this->intercept($selectallbutton, 'onClick', 'selectall');

$this->build("p4a_field","clients")
->setWidth(300)
->setType("multicheckbox")
->setSource($client_array_source)
->setSourceDescriptionField("desc")
->setSourceValueField("id");

This function will allow the user to click the "select all" button and have their checkboxes populated:

function selectall() {
$array_id = array();
$num_rows = $this->clients->data->getNumRows();
for ($i = 1; $i $#60;= $num_rows; $i++) {
$this->clients->data->row($i);
$array_id[$i] = $this->clients->data->getPkValues();
}
$this->clients->setNewValue($array_id);
$this->clients->load();
}

This works in P4A v3.4.1 but not 3.4.0. I've not tested on later versions.

Permalink 02:13:07 pm, by admin Email , 1138 words, 6443 views   English (US)
Categories: PHP

Basic native P4A File Manager

This code can be used as a P4A mask to provide very basic File Manager functionality outside of the RTE.

class fm extends p4a_base_mask
{
public $rootfolder = null;
public $htmlrootfolder = null;

//yes I know P4A has it's own system for this built around defined constants. Constants that we can't change! so lets do it here on a mask by mask basis.
public $valid_preview_images = array('gif','jpg','ico','png'); //'mp3','mp4','avi','pdf','doc','xls','odt','ods','txt'
public $valid_preview_text = array('txt');

public function __construct() {
parent::__construct();

$site = '/'.P4A::singleton()->user_data['user'];

$this->rootfolder = P4A_UPLOADS_DIR.$site;
$this->htmlrootfolder = P4A_UPLOADS_PATH.$site;

$iconpath = P4A_ROOT_URL.'/icons/default/16';
$this->setTitle('File Manager');

//File Handling
$this->build("P4A_Dir_Source", "fileset")
->setDir($this->rootfolder);

$this->build("P4A_Field", "upload_field")
->setLabel('Upload:')
->setUploadSubpath('')
->label->setWidth(50);
$this->upload_field->setType("file");
$this->intercept($this->upload_field, "afterUpload", "afterUpload");

$this->build("p4a_table",'table')
->setSource($this->fileset);
$this->table->addActionCol ('delete')
->cols->delete->setLabel('<img src="'.P4A_ROOT_URL.'/icons/default/16/actions/edit-delete.png" alt="[D]" title="Delete this file" />');
$this->intercept($this->table->cols->delete, 'afterClick', 'DeleteColClick');
$this->table->addActionCol ('preview')
->cols->preview->setLabel('<img src="'.P4A_ROOT_URL.'/icons/default/16/actions/thumbnail.png" alt="[P]" title="View this file" />');
$this->intercept($this->table->cols->preview, 'afterClick', 'TableRowClick');
$this->table->rows->addAction('afterClick');
$this->intercept($this->table->rows, 'afterClick', 'TableRowClick');

$this->build("P4A_Fieldset","f_table")
->setLabel('Files ['.str_replace($this->rootfolder,'.',$this->fileset->getDir()).']')
->anchorLeft($this->table)
->anchorCenter($this->upload_field);

//Directory Handling
$this->build("P4A_Toolbar","dtb")->setSize(16);
$this->dtb->addButton('new_',$iconpath.'/mimetypes/folder_green.png');
$this->dtb->addButton('home',$iconpath.'/actions/go-home.png');
$this->dtb->addButton('delete',$iconpath.'/actions/edit-delete.png');
$this->intercept($this->dtb->buttons->new_, 'onClick', 'createFolder');
$this->intercept($this->dtb->buttons->home, 'onClick', 'goFolderHome');
$this->intercept($this->dtb->buttons->delete, 'onClick', 'deleteFolder');

$navme = $this->build("P4A_Dir_Navigator","navme")
->setBaseDir($this->rootfolder);
$this->navme->addAction('afterClick');
$this->intercept($this->navme, 'afterClick', 'NavRowClick');

$this->build("P4A_Field", "create_folder_field")
->setWidth(100)
->label->setWidth(30)
->setLabel('New: ');

$this->build("P4A_Fieldset","f_navme")
->setLabel('Directory ['.str_replace($this->rootfolder,'.',$this->navme->getBaseDir()).']')
->setWidth(250)
->anchor($this->create_folder_field)
->anchorleft($this->dtb)
->anchor($this->navme);
//Preview Handling
$this->build("p4a_image","imagepreview")
->setStyleProperty('position','relative') //a bit of elbow room
->setStyleProperty('padding','10px'); //a bit of elbow room

$this->build("p4a_box","textpreview")
->setWidth(500)
->setStyleProperty('padding','10px'); //a bit of elbow room

$this->build("P4A_Button", "close_imagepreview_button")
->setSize(16)
->setLabel('Close Preview',false)
->setIcon($iconpath.'/actions/window-close.png')
->implement("onclick", $this, "closeImagePreview");

$this->build("P4A_Button", "close_textpreview_button")
->setSize(16)
->setLabel('Close Preview',false)
->setIcon($iconpath.'/actions/window-close.png')
->implement("onclick", $this, "closeTextPreview");

$this->build("P4A_Fieldset","f_imagepreview")
->setLabel('')
->setVisible(false)
->anchorRight($this->close_imagepreview_button)
->anchorCenter($this->imagepreview);

$this->build("P4A_Fieldset","f_textpreview")
->setLabel('')
->setVisible(false)
->anchorRight($this->close_textpreview_button)
->anchorCenter($this->textpreview);

//Display
$this->frame->anchor($this->f_navme)
->setWidth(700)
->addCssClass('mr_frame')
->addCssClass('ui-corner-all')
->anchorLeft($this->f_table)
->anchorCenter($this->f_imagepreview)
->anchorCenter($this->f_textpreview);

$this->build("P4A_Quit_Toolbar","toolbar");

$this->display("top",$this->toolbar);

$this->info('File Manager Loaded...');

}

public function closeImagePreview() {
$this->f_imagepreview->setVisible(false);
}
public function closeTextPreview() {
$this->f_textpreview->setVisible(false);
}

public function goFolderHome() {
$d = $this->rootfolder;
$this->f_navme->setLabel('Directory [.]');
$this->fileset->setDir($d)
->reload();
$this->navme->setCurrentSubdir($this->rootfolder);
$this->uploadfield->setUploadSubpath('');
$this->f_table->setLabel('Files [.]');
}

public function NavRowClick($a) {
$v = $this->navme->getCurrentSubdir();
$d = $this->rootfolder;
$this->f_table->setLabel('Files [./'.$v.']');
$this->f_navme->setLabel('Directory [./'.$v.']');
$this->fileset->setDir($d.'/'.$v)
->reload();
$this->uploadfield->setUploadSubpath($d.'/'.$v);
}

public function TableRowClick($o, $a) {
$f = $this->table->data->fields->filename->getValue();
$s = $this->table->data->fields->size->getValue();
$e = trim(end(explode(".", $f)));
if (in_array($e,$this->valid_preview_images)) {
$relativepathfilename = $this->navme->getCurrentSubdir().'/'.$f;
$this->imagepreview->setIcon($this->htmlrootfolder.'/'.$relativepathfilename);
$this->f_textpreview->setVisible(false);
$this->f_imagepreview->setLabel('Preview: '.$f)
->setVisible(true);
} elseif(in_array($e,$this->valid_preview_text)) {
$relativepathfilename = $this->navme->getCurrentSubdir().'/'.$f;
$fileContent = file_get_contents($this->rootfolder.'/'.$relativepathfilename,FILE_TEXT);
$this->textpreview->setValue('<pre>'.$fileContent.'</pre>');
$this->f_imagepreview->setVisible(false);
$this->f_textpreview->setLabel('Preview: '.$f)
->setVisible(true);

} else {
$this->info('Sorry. Preview unavailable for this file');
}
}

public function DeleteColClick($o, $a) {
$this->info('Deleting file...');
$fullpathfilename = $this->fileset->getDir().'/'.$this->table->data->fields->filename->getValue();
unlink($fullpathfilename);
$this->fileset->reload();
$this->info('Done...');
}

public function createFolder() {
$this->info('Creating...');
$v = $this->navme->getCurrentAbsoluteDir();
$folder = $this->create_folder_field->getNewValue();
if ($v == $this->rootfolder.'/') {
$fullnewpath = $v.$folder;
} else {
$fullnewpath = $v.'/'.$folder;
}
mkdir($fullnewpath,0777,true);
$this->create_folder_field->setNewValue(null);
$this->info('Done...');
}

public function afterUpload() {
$tmpname = $this->upload_field->getNewValue(0);
$name = $this->upload_field->getNewValue(6);
$fullpath = $this->fileset->getDir();

rename(P4A_UPLOADS_TMP_DIR . "/$tmpname", "$fullpath/".$name);
$this->upload_field->setNewValue(null);
$this->fileset->reload();
}

public function deleteFolder() {
$this->info('Deleting directory...');
$v = $this->navme->getCurrentAbsoluteDir();
$folder = $this->create_folder_field->getNewValue();
if ($v == $this->rootfolder.'/') {
$this->error('You cannot delete your root directory');
} else {
$fulldelpath = $v.'/'.$folder;
$oneup = realpath($fulldelpath.'/../');
$this->_delete_directory($fulldelpath);
$this->navme->setBaseDir($oneup); //move navme up one
$this->fileset->setDir($oneup) //move filelist up one
->reload();
$this->info('Done...');
}
}

function _delete_directory($dirname) { //remove containing files & directories then remove what I've asked for
if (is_dir($dirname))
$dir_handle = opendir($dirname);
if (!$dir_handle)
return false;
while($file = readdir($dir_handle)) {
if ($file != "." && $file != "..") {
if (!is_dir($dirname."/".$file))
unlink($dirname."/".$file);
else
$this->_delete_directory($dirname.'/'.$file);
}
}
closedir($dir_handle);
rmdir($dirname);
return true;
}

}

You may need to add the necessary icons to the correct location in the

./icons

directory. I used the Open Source Crystal iconset.

This was developed using P4A v3.4.1

I'm sure this can be improved upon, feel free to do so and let me know...

Permalink 01:42:01 pm, by admin Email , 218 words, 4196 views   English (UK)
Categories: PHP

Upgrading the P4A FCKEditor File Manager

I use P4A a lot. One thing I've found though is that the default File Manager which comes with it's implementation of FCKEditor is a bit basic for my customers.

As a result, I now use TinyBrowser as a replacement. This article outlines how to implement it.

First off, download the latest TinyBrowser from http://www.lunarvis.com, currently this is v1.41.

Extract the contents of the TinyBrowser download into
./themes/default/widgets/rich_textarea/editor/filemanager/browser/tinybrowser

Edit ./themes/default/widgets/rich_textarea/rich_textarea.php and place the following code inside the first PHP code block:


// Introduced by Fear of Mice to support tinybrowser
$mydir = P4A_UPLOADS_DIR. '/' . $this->getUploadSubpath(); //set
$prep = false;
if (substr($mydir, 0, 7) == 'http://' || substr($mydir, 0, 8) == 'https://') { //Strip_Slashes
$prep = TRUE;
}
$mydir = str_replace('https://','',$mydir);
$mydir = str_replace('http://','',$mydir);
$mydir = preg_replace('|(/)+|','/',$mydir);
if($prep === TRUE) {
$mydir = prep_url($mydir);
}
$mydir=base64_encode($mydir); //encode it

then replace:

rte.Config['LinkBrowserURL'] = rte.BasePath + 'editor/filemanager/browser/default/browser.html?Connector=<?php echo $connector ?>';

with

rte.Config['LinkBrowserURL'] = rte.BasePath + "editor/filemanager/browser/tinybrowser/tinybrowser.php?path=<?php echo $mydir ?>";

That's it. You should now have a nice File Manager which supports multiple uploads and thumbnails.

30/11/09

Permalink 12:40:10 pm, by admin Email , 51 words, 18952 views   English (UK)
Categories: PHP, Linux

CodeIgniter .htaccess for use with Heart Internet

Here's a working .htaccess file for CodeIgniter projects hosted on Heart Internet.

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]

*please note, I'm note endorsing the use of Heart Internet Hosting. You'll probably find better facilities elsewhere ;)

fear of mice

This is the official blog of

We aim to post comments (and possibly solutions) to some of the more interesting IT Support issues we come across in our daily travels around the South Hams of Devon.

September 2010
Mon Tue Wed Thu Fri Sat Sun
<< <     
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      

Search

Categories

Misc

XML Feeds

What is RSS?

Who's Online?

  • Guest Users: 3

powered by
b2evolution