2014年8月18日 星期一

CCS在Editable Grid中防止個別記錄的動作



關鍵在於 DataSource->CmdExecution

這是一個 Run time 的 Property,決定在 BeforeExecuteInsert, BeforeExecuteUpdate or BeforeExecuteDelete 這幾個事件執行 Insert, Update or Delete 前是否要被執行。


function Tasks_DataSource_BeforeExecuteInsert(& $sender)  {
  global $Tasks;
   if (CCGetUserID() == $Tasks->user_id_assign_to->GetValue()) {
   $Tasks->DataSource->CmdExecution = false;
  }
}

function Tasks_DataSource_BeforeExecuteUpdate(& $sender)  {
global $Tasks;
   if (CCGetUserID() != $Tasks->user_id_assign_to->GetValue()) {
   $Tasks->DataSource->CmdExecution = false;
  }
}

function Tasks_DataSource_BeforeExecuteDelete(& $sender)  {
global $Tasks;
   if (CCGetUserID() != $Tasks->user_id_assign_by->GetValue()) {
   $Tasks->DataSource->CmdExecution = false;
  }
}

2014年8月16日 星期六

免費的 icon 圖檔

http://www.softicons.com/free-icons

這裡有一大堆免費的 icon 圖檔可以用,太感謝了。



2014年8月15日 星期五

CCS fileupload 中文檔名檔案上傳下載的問題


CCS 內附有一個物件:FileUpload,可以用來處理檔案上傳的問題。可是,經過筆者實際使用過,發現就跟 php 一樣,中文檔名會有很大的問題。

html/php 中處理檔案上傳的程序是:

1.  html 的 <form ...><input type=file..> <input type=summit...>這是給使用者選擇上傳檔案的介面
2. 使用者選擇檔案後,瀏覽器傳回一個檔案格式回伺服器,放在一個伺服器指定的 tmp 目錄裡,副檔名應該是 .tmp 的。然後,這些檔案資訊就放在 $_FILE['file']陣列裡面(name, size, tmp_name..等)
3. 程式設計者必須再使用 move_uploaded_file() 將 tmp 檔案搬移到設計者指定的上傳目錄及檔案名稱。
4. 完成。

然而, CodeCharge的做法,基本上當然是循 html/php 的方式,但是,又稍微多此一舉一下。
CodeCharge 是這樣做的:
1. 在頁面上加入一個 FileUpload Control,自動產生需要的 html 以及相關的 Property、物件。
2. 只要填寫這個物件的屬性上去,就可以用了。包括:指定 temporary Folder , File Folder等。
3. 完成

這樣很簡單,對吧!這就是 CodeCharge  的優點。但是...

如果是英文檔名,這些都沒有問題。但中文檔名就有問題了!
主要的問題,還是在中文檔名無法經過 move_uploaded_file()
原本想說可以使用 Event 來攔截中文檔名,將之改為英數字檔名,然後中文檔名就記錄在資料庫中。
一直試了都失敗。 Before Process File / Before Show 兩個 EVENT 最有可能可以用,但是都不行!
後來,直接去研究  Code Charge 裡面 Class.php 關於 Fileupload 的程式碼發現,Code Charge 的處理,他是這樣的:

1. html/php 前端這裡跟前面一樣。
2. 使用者瀏覽了檔案,按了確定以後,CodeCharge 就直接處理到了自動加上一個日期時間序號的前置編碼在原檔名前面,然後就直接將 tmp move_uploaded_file 到 指定的 Temporary Folder,
3. 然後,再 MOVE 到 指定的 Folder 中
4. 所謂的 before process File,是指在這個 MOVE 之前的動作,而不是在 move_uploaded_file()之前。難怪不管我怎樣修改檔名為英數字都還是無效。

最後,我只好捨棄使用 CodeCharge的 FileUpload Control,而是直接寫 php 的程式!

Code Charge 的優勢還是在資料庫部分,Record / Grid / Editable Grid 等。

我就直接運用一個 Editable Grid 來顯示 上傳的檔案,加上聯結,當然使用者看到的是原始中文檔名,而實際上伺服器端放的是我修改過的英數字檔名,當使用者點擊該中文檔名的連結,就再呼叫 download.php 程式,將 英數字檔名,以及 原始中文檔名 傳給他,實際上傳的檔案是 後端的英數字檔案,而下載的檔案名稱去原始的中文檔名。

這樣問題就解決了。



php 讀取檔案副檔名的方法

php 讀取檔案副檔名的方法,有以下六個:


$filename = 'mypic.gif';

// 1. The "explode/end" approach
$ext = end(explode('.', $filename));

// 2. The "strrchr" approach
$ext = substr(strrchr($filename, '.'), 1);

// 3. The "strrpos" approach
$ext = substr($filename, strrpos($filename, '.') + 1);

// 4. The "preg_replace" approach
$ext = preg_replace('/^.*\.([^.]+)$/D', '$1', $filename);

// 5. The "never use this" approach
//   From: http://php.about.com/od/finishedphp1/qt/file_ext_PHP.htm
$exts = split("[/\\.]", $filename);
$n = count($exts)-1;
$ext = $exts[$n];

//6.
$filename = 'mypic.gif';
$ext = pathinfo($filename, PATHINFO_EXTENSION);
註: pathinfo(),取得副檔名應該沒有問題,因為副檔名都是英文的,若是讀中文檔名可能會有問題。










2014年8月14日 星期四

php 的檔案處理系統

php 的檔案處理系統


執行階段的設定

php的檔案處理函數皆會受到 php.ini 裡面的設定選項影響。所以,要先了解有哪些設定:

NameDefaultDescriptionChangeable
allow_url_fopen"1"Allows fopen()-type functions to work with URLs (available since PHP 4.0.4)PHP_INI_SYSTEM
user_agentNULLDefines the user agent for PHP to send (available since PHP 4.3)PHP_INI_ALL
default_socket_timeout"60"Sets the default timeout, in seconds, for socket based streams (available since PHP 4.3)PHP_INI_ALL
from""Defines the anonymous FTP password (your email address)PHP_INI_ALL
auto_detect_line_endings"0"When set to "1", PHP will examine the data read by fgets() and file() to see if it is using Unix, MS-Dos or Mac line-ending characters (available since PHP 4.3)PHP_INI_ALL


路徑 "/" "\"  的相容性 Unix / Windows Compatibility

Unix的路徑使用 (/),而Windows則是(/)(\)  都可以。

PHP 5  的檔案系統函數


函數 Function敘述 
basename()傳回一個路徑的 檔案名稱 部分
chgrp()改變檔案所屬的 群組 group
chmod()改變檔案 模式 mode
chown()改變檔案擁有者 file owner
clearstatcache()清除檔案狀態快取 file status cache
copy()複製一個檔案
delete()請閱  unlink() 或 unset()
dirname()傳回一個路徑的目錄部分
Returns the directory name component of a path
disk_free_space()傳回一個目錄的自由可用空間
Returns the free space of a directory
disk_total_space()傳回一目錄的總共空間
Returns the total size of a directory
diskfreespace()跟 disk_free_space() 一樣的。別名而已。
fclose()關閉一個開啟的檔案
feof()測試一個開啟檔案的 end-of-file
fflush()Flushes buffered output to an open file
fgetc()傳回開啟檔案的一個字元
Returns a character from an open file
fgetcsv()傳回開啟檔案的一行,CSV格式
Parses a line from an open file, checking for CSV fields
fgets()傳回開啟檔案的一行
Returns a line from an open file
fgetss()傳回開啟檔案的一行,移除HTML, PHP的標籤
Returns a line, with HTML and PHP tags removed, from an open file
file()讀取檔案到一個陣列中
Reads a file into an array
file_exists()檢查檔案或目錄是否存在
Checks whether or not a file or directory exists
file_get_contents()讀取檔案進入一個字串變數
Reads a file into a string
file_put_contents()將一個字串變數寫入檔案
Writes a string to a file
fileatime()傳回檔案最後一次存取的時間
Returns the last access time of a file
filectime()傳回檔案最後一次改變的時間
Returns the last change time of a file
filegroup()傳回檔案的 群組 ID
Returns the group ID of a file
fileinode()傳回檔案的 inode 號碼
Returns the inode number of a file
filemtime()傳回檔案最後修改的時間
Returns the last modification time of a file
fileowner()傳回檔案擁有者的 user ID
Returns the user ID (owner) of a file
fileperms()傳回檔案的權限資料
Returns the permissions of a file
filesize()傳回檔案大小
Returns the file size
filetype()傳回檔案格式
Returns the file type
flock()鎖定或釋放檔案
Locks or releases a file
fnmatch()比對 檔案名稱或字串
Matches a filename or string against a specified pattern
fopen()開啟檔案
Opens a file or URL
fpassthru()讀取一個開啟的檔案,直到EOF,然後將結果寫到output buffer
Reads from an open file, until EOF, and writes the result to the output buffer
fputcsv()將一行格式成CSV,然後寫到開啟的檔案
Formats a line as CSV and writes it to an open file
fputs()跟 fwrite()一樣
Alias of fwrite()
fread()從開啟的檔案讀取
Reads from an open file
fscanf()Parses input from an open file according to a specified format
fseek()Seeks in an open file
fstat()Returns information about an open file
ftell()Returns the current position in an open file
ftruncate()Truncates an open file to a specified length
fwrite()Writes to an open file
glob()Returns an array of filenames / directories matching a specified pattern
is_dir()Checks whether a file is a directory
is_executable()Checks whether a file is executable
is_file()Checks whether a file is a regular file
is_link()Checks whether a file is a link
is_readable()Checks whether a file is readable
is_uploaded_file()Checks whether a file was uploaded via HTTP POST
is_writable()Checks whether a file is writeable
is_writeable()Alias of is_writable()
lchgrp()Changes group ownership of symlink
lchown()Changes user ownership of symlink
link()Creates a hard link
linkinfo()Returns information about a hard link
lstat()Returns information about a file or symbolic link
mkdir()Creates a directory
move_uploaded_file()Moves an uploaded file to a new location
parse_ini_file()Parses a configuration file
parse_ini_string()Parses a configuration string
pathinfo()Returns information about a file path
pclose()Closes a pipe opened by popen()
popen()Opens a pipe
readfile()Reads a file and writes it to the output buffer
readlink()Returns the target of a symbolic link
realpath()Returns the absolute pathname
realpath_cache_get()Returns realpath cache entries
realpath_cache_size()Returns realpath cache size
rename()Renames a file or directory
rewind()Rewinds a file pointer
rmdir()Removes an empty directory
set_file_buffer()Sets the buffer size of an open file
stat()Returns information about a file
symlink()Creates a symbolic link
tempnam()Creates a unique temporary file
tmpfile()Creates a unique temporary file
touch()Sets access and modification time of a file
umask()Changes file permissions for files
unlink()Deletes a file




完整的刪除一個目錄:

function delTree($dir) {  
  1.     $files = glob( $dir . '*', GLOB_MARK );  
  2.     foreach( $files as $file ){  
  3.         if( substr( $file, -1 ) == '/' )  
  4.             delTree( $file );  
  5.         else  
  6.             unlink( $file );  
  7.     }   
  8.   
  9.     if (is_dir($dir)) rmdir( $dir );   
  10.   
  11. }  

2014年8月13日 星期三

Code Charge Studio 如何 Debug

Code Charge 並沒有附加 Debugger,沒有偵測錯誤的系統,而是透過自行撰寫一小段在事件中的程式碼來顯示變數的值。

在 PHP 環境裡,大部分的狀況可以這樣寫:

1. 將該變數設定為 global變數,然後用 echo 來顯示

global $variable_name;

echo $variable_name;


2. 在 before_show event 中顯示該 control 變數值

global $TextBox1;

echo $TextBox1->GetValue();


3. 如果有 SQL 碼,可以在Before Execute Select 事件裡顯示檢查 SQL是否正確


global $GridName;

echo "SQL:" . $GridName->DataSource->SQL . "<br>";
echo "ORDER BY:" . $GridName->DataSource->Order . "<br>";
echo "WHERE:" . $GridName->DataSource->Where . "<br>";


註:有時候,如果  ECHO 沒有成功,可能必須加上這一段:暫時關閉 "output_buffering"
ini_set("output_buffering", "Off");



CCS 如何處理錯誤訊息


範例一:如果該頁沒有收到 task_id,則不予顯示:

function Page_AfterInitialize(& $sender) { 
if (CCGetFromGet("task_id","") == "") { 
    print ("Error: \"task_id\" parameter is not specified.");
    exit;
  }
}




範例二:如果該頁沒有收到 task_id 參數,則 Tasks 物件不予顯示,並顯示錯誤訊息。 
$Tasks_Error 是自行建立的一個 Label Control。

function Page_AfterInitialize() {
global $Tasks;
global $Tasks_Error;

  if (CCGetFromGet("task_id","") == "") { 
    $Tasks->Visible = False;
    $Tasks_Error->SetValue("Error: \"task_id\" parameter is not specified.");
  }

}

如何判斷現在FORM是在 insert mode? 還是 update mode?

只要用  if (empty({primary_key})) 就可以知道是否為新增模式了。 如果 {promary_key} 是空白的,那麼就是在新增模式;反之,就是更新模式。 以上。