2.2 Форма выбора адресатов
/****************************************************************************
Description: Форма выбора адресатов
Author: Potapenko Vladimir
Created: 23.04.2013
Version: 1.0.0 Changes info:
= 23.04.2013 (Potapenko Vladimir): создан
****************************************************************************/
class OFormRecipients extends OForm
{
public $UsersTable;
/**
* Поле с именем получателя
*
* @var string
*
*/
public $NameField;
/**
* Поле с адресом получателя
*
* @var string
*
*/
public $MailField;
/**
* Имя поля, куда надо записать адреса
*
* @var string
*
*/
public $ReturnToEdit;
/**
* Выделеные получатели
*
* @var string
*
*/
public $SelectedRecipients;
/**
* массив всех получателей
*
* @var array
*
*/
public $CurrentRecipients;
public $UseFilters=true;
public function __construct($_name, $_parent, $_users_table="", $_name_field="",
$_mail_field="")
{
parent::__construct($_name, $_parent);
$this->UsersTable=$_users_table;
$this->NameField=$_name_field;
$this->MailField=$_mail_field;
$this->SelectedRecipients=array();
$this->CreateFromXML(ProjectPath()."forms/mail_sender/LayoutOFormRecipients.xml");
$this->GenFilters();
$this->FillCheckBox();
}
public function UpdateList()
{
$this->CheckListBox_RECIPIENTS->Clear();
$this->FillCheckBox();
AddToChangedComponents($this->CheckListBox_RECIPIENTS);
}
public function ClearFilters()
{
$this->CheckListBox_RECIPIENTS->Clear();
$pre=$this->UseFilters;
$this->UseFilters=false;
$this->FillCheckBox();
$this->UseFilters=$pre;
}
public function Close()
{
$this->Hide();
}
/**
* добавление фильтров
*
*/
protected function GenFilters()
{
$curr_col=0;
$label1=new OLabel("label1", $this->Panel_FILTERS);
$label1->Style->Left=10;
$label1->Style->Top=40;
$label1->Style->FontWeight="bold";
$label1->Style->Width=100;
$label1->Text="Статус";
$ds = new ODBDataSet("", $this, GetConnection("MainDBConnection"));
$query="SELECT * FROM table_client_types";
$cnt=0;
if($ds->Open($query) && $ds->RowCount>0)
{
do
{
$cb=new OCheckBox("filter_cb_".$ds->GetFieldValue("ID"), $this->Panel_FILTERS);
$cb->Text=$ds->GetFieldValue("NAME");
$cb->Style->Left=12+150*$curr_col;
$cb->Style->Top=65+25*($cnt);
$cb->Style->Width=150;
$cnt++;
if($cnt>2)
{
$curr_col++;
$cnt=0;
}
}while($ds->NextRow());
}
$curr_col++;
$cnt=0;
$label2=new OLabel("label2", $this->Panel_FILTERS);
$label2->Style->Left=10+150*$curr_col;
$label2->Style->Top=40;
$label2->Style->FontWeight="bold";
$label2->Style->Width=100;
$label2->Text="Атрибуты";
$ds = new ODBDataSet("", $this, GetConnection("MainDBConnection"));
$query="SELECT * FROM table_client_params";
if($ds->Open($query) && $ds->RowCount>0)
{
do
{
$cb=new OCheckBox("filter2_cb_".$ds->GetFieldValue("ID"), $this->Panel_FILTERS);
$cb->Text=$ds->GetFieldValue("NAME");
$cb->Style->Left=12+150*$curr_col;
$cb->Style->Top=65+25*$cnt;
$cb->Style->Width=110;
$cnt++;
if($cnt>2)
{
$curr_col++;
$cnt=0;
}
}while($ds->NextRow());
}
}
/**
* заполенеие дерева по массиву, который возвращает метод LoadRecipients
*
* @param array $_array информация о получателе
* @param ONode $_parent родительский узел
*
*/
protected function FillCheckBox($_array=null, $_parent="")
{
if($_array==null)
{
if($this->UsersTable!="" && $this->MailField!="")
$_array=$this->FillCheckBoxByFields();
else
$_array=$this->LoadRecipients();
$this->CurrentRecipients=array();
}
$main_cnt=count($_array);
foreach($_array as $id=>$info)
{
$mail=$info['MAIL'];
if($mail=="") $mail=$info['NAME'];
if(!isset($this->CurrentRecipients[$mail]))
{
$this->CurrentRecipients[$mail]=array();
foreach($info as $field=>$val)
{
$this->CurrentRecipients[$mail][$field]=$val;
}
}
$text=$info['NAME'];
if(trim($info['MAIL'])!="") $text.=" <".$info['MAIL'].">";
$current_node = new ONode($text, $this->CheckListBox_RECIPIENTS,
array($id, isset($this->SelectedRecipients[$id])), "");
$this->CheckListBox_RECIPIENTS->AddNode($current_node, $_parent);
if(isset($info['CHILDREN']) && count($info['CHILDREN'])>0)
{
$this->FillCheckBox($info['CHILDREN'], $current_node);
}
}
}
public function RememberCheckbox($_id, $_flag)
{
if($_flag)
$this->SelectedRecipients[$_id]=true;
else
unset($this->SelectedRecipients[$_id]);
}
/**
* Возвращает строкe для запросов
*
* @return mixed фильтр
*/
protected function GenFilterString()
{
$result=array("","");
$keys=$this->Panel_FILTERS->Components->GetKeys();
$attrs=array();
for($i=0; $i {
if(strpos(
$this->Panel_FILTERS->Components->Items[$keys[$i]]->Name, "filter_cb_"
)!==false && $this->Panel_FILTERS->Components->Items[$keys[$i]]->Value==1)
{
$info=explode("_", $this->Panel_FILTERS->Components->Items[$keys[$i]]->Name);
$id=$info[2];
if($result[0]!="") $result[0].=" OR ";
$result[0].=" ID_TYPE=$id";
}
if(strpos(
$this->Panel_FILTERS->Components->Items[$keys[$i]]->Name, "filter2_cb_"
)!==false && $this->Panel_FILTERS->Components->Items[$keys[$i]]->Value==1)
{
$info=explode("_", $this->Panel_FILTERS->Components->Items[$keys[$i]]->Name);
$id=$info[2];
$attrs[]=$id;
}
}
if($result[0]!="") $result[0]="(".$result[0].")";
if($this->Panel_FILTERS->Edit_SEARCH->Value!="")
{
$name=$this->Panel_FILTERS->Edit_SEARCH->Value;
if($result[1]!="") $result[1].=" AND ";
$result[1].=" (NAME LIKE '%$name%' OR EMAIL LIKE '%$name%')";
if($result[0]!="") $result[0].=" AND ";
$result[0].=" (NAME LIKE '%$name%' OR FULLNAME LIKE '%$name%' OR EMAIL LIKE '%$name%')";
}
if(count($attrs)!=0)
{
if($result[0]!="") $result[0].=" AND ";
$result[0].="(ID IN (SELECT ID_CLIENT FROM table_cp_relation
WHERE ID_PARAM IN (".implode(",", $attrs).")))";
}
return $result;
}
/**
* заполнение массива с получателями по значениям, переданным в конструкторе
*
* @return array Массив получателей
*/
protected function LoadRecipients()
{
if($this->UseFilters)
$filters=$this->GenFilterString();
else
$filters=array("","");
$result=array();
$ds = new ODBDataSet("", $this, GetConnection("MainDBConnection"));
$query="SELECT ID, FULLNAME, EMAIL FROM table_clients";
if($filters[0]!="") $query.=" WHERE ".$filters[0];
if($filters[1]!="")
{
if($filters[0]!="") $query.=" OR ";
else $query.=" WHERE ";
$query.=" (SELECT COUNT(*) FROM table_clients_employees
WHERE ID_CLIENT=table_clients.ID";
if($filters[1]!="") $query.=" AND ".$filters[1];
$query.=")>0";
}
$parent_ids=array();
//echo $query;
if($ds->Open($query) && $ds->RowCount>0)
{
do
{
$parent_ids[]=$ds->GetFieldValue("ID");
$result["t1_".$ds->GetFieldValue("ID")]=array("NAME"=>$ds->GetFieldValue("FULLNAME"),
"MAIL"=>$ds->GetFieldValue("EMAIL"), "CHILDREN"=>array());
}while($ds->NextRow());
}
if(sizeof($parent_ids) > 0)
{
$ds = new ODBDataSet("", $this, GetConnection("MainDBConnection"));
$query="SELECT ID, NAME, EMAIL, ID_CLIENT FROM table_clients_employees
WHERE ID_CLIENT IN (0,".implode(",",$parent_ids).")";
if($filters[1]!="") $query.=" AND ".$filters[1];
if($ds->Open($query) && $ds->RowCount>0)
{
do
{
$result["t1_".$ds->GetFieldValue("ID_CLIENT")]["CHILDREN"]
[$ds->GetFieldValue("ID")]=array("NAME"=>$ds->GetFieldValue("NAME"),
"MAIL"=>$ds->GetFieldValue("EMAIL"), "CHILDREN"=>array());
}while($ds->NextRow());
}
}
return $result;
}
public function ButtonCancel_OnClick()
{
$this->Close();
}
public function ButtonOk_OnClick()
{
$this->Close();
if($this->ReturnToEdit!=null)
{
$edit=$this->ReturnToEdit;
$this->Parent->$edit->Value=$this->GetRecipients();
}
}
/**
* Метод, возвращающий массив получателей
*
* @return array $result[ID]=>array("NAME"=>$name, "MAIL"=>$email, "CHILDREN"=>...)
*/
public function FillCheckBoxByFields()
{
$result=array();
$ds = new ODBDataSet("", $this, GetConnection("MainDBConnection"));
$query="SELECT ID, ".$this->MailField.($this->NameField!=""?", ".$this->NameField:"")."
FROM ".$this->UsersTable;
$parent_ids=array();
if($ds->Open($query) && $ds->RowCount>0)
{
do
{
$parent_ids[]=$ds->GetFieldValue("ID");
$result[$ds->GetFieldValue("ID")]=
array("NAME"=>$ds->GetFieldValue($this->NameField),
"MAIL"=>$ds->GetFieldValue($this->MailField), "CHILDREN"=>array());
}while($ds->NextRow());
}
return $result;
}
public function CheckAll()
{
$this->CheckListBox_RECIPIENTS->CheckAll(null, $this->CB_ALL->Value);
AddToChangedComponents($this->CheckListBox_RECIPIENTS);
}
/**
* Метод получения списка выбранных пользователей
*
* @return string адреса через запятую
*
*/
public function GetRecipients()
{
$result=array();
$cnt=count($this->CheckListBox_RECIPIENTS->AllNodes);
for($i=0; $i<$cnt; $i++)
{
if(isset($this->CheckListBox_RECIPIENTS->AllNodes[$i]) &&
$this->CheckListBox_RECIPIENTS->AllNodes[$i]->Object[1])
{
$result[]=$this->CheckListBox_RECIPIENTS->AllNodes[$i]->Text;
}
}
$result=implode(", ", $result);
$result=str_replace("<", "<", $result);
$result=str_replace(">", ">", $result);
return $result;
}
} ?>
|