Link.class.php (3782B)
1 <?php 2 3 namespace template_engine\components; 4 5 use cms\action\RequestParams; 6 use template_engine\components\html\Component; 7 use template_engine\element\CMSElement; 8 use util\json\JSON; 9 10 /** 11 * Erzeugt einen HTML-Link. 12 * 13 * @author dankert 14 * 15 */ 16 class LinkComponent extends Component 17 { 18 19 public $var1; 20 21 public $var2; 22 23 public $var3; 24 25 public $var4; 26 27 public $var5; 28 29 public $value1; 30 31 public $value2; 32 33 public $value3; 34 35 public $value4; 36 37 public $value5; 38 39 public $target; 40 41 public $type; 42 43 public $action; 44 45 public $subaction; 46 47 public $title; 48 49 public $class; 50 51 public $url; 52 53 public $config; 54 55 public $id; 56 57 public $accesskey; 58 59 public $name; 60 61 public $anchor; 62 63 public $frame = '_self'; 64 65 public $modal = false; 66 67 public $afterSuccess; 68 69 70 71 public function createElement() 72 { 73 74 $link = new CMSElement('a'); 75 if ( $this->afterSuccess ) 76 $link->addAttribute('data-after-success',$this->afterSuccess); 77 78 if ( $this->class ) 79 $link->addStyleClass($this->class); 80 81 if ( $this->title ) 82 $link->addAttribute('title',$this->title); 83 84 if ( $this->accesskey ) 85 $link->addAttribute('accesskey',$this->accesskey); 86 87 if ( $this->frame ) 88 $link->addAttribute('target',$this->frame); 89 90 if ( $this->name ) 91 $link->addAttribute('date-name',$this->name)->addAttribute('name',$this->name); 92 93 if ( $this->url ) 94 $link->addAttribute('data-url',$this->url); 95 96 if ( $this->type ) 97 $link->addAttribute('data-type',$this->type); 98 99 if ( $this->action ) 100 $link->addAttribute('data-action',$this->action); 101 else 102 $link->addAttribute('data-action',''); 103 104 if ( $this->subaction ) 105 $link->addAttribute('data-method',$this->subaction); 106 else 107 $link->addAttribute('data-method',''); 108 109 if ( $this->id ) 110 $link->addAttribute('data-id',$this->id); 111 else 112 $link->addAttribute('data-id',''); 113 114 $json = new JSON(); 115 $arrayvalues = array(); 116 foreach( $this->getExtraParamArray() as $varname => $varvalue ) { 117 118 $link->addAttribute('data-extra-'.$varname,$varvalue); 119 $arrayvalues[ $varname ] = $varvalue; 120 } 121 $link->addAttribute('data-extra',str_replace('"',"'",str_replace(array("\t", "\r", "\n"),'',$json->encode($arrayvalues)))); 122 123 switch ($this->type) 124 { 125 case 'post': 126 127 // Zusammenbau eines einzeligen JSON-Strings. 128 // Aufpassen: Keine doppelten Hochkommas, keine Zeilenumbrüche. 129 $data = array(); 130 131 $data['action' ] = (!empty($this->action ))?$this->action :$this->request->action; 132 $data['subaction'] = (!empty($this->subaction))?$this->subaction:$this->request->method; 133 $data['id' ] = (!empty($this->id ))?$this->id :''; 134 $data[RequestParams::PARAM_TOKEN] = '${_token}'; 135 136 foreach( $this->getExtraParamArray() as $varname => $varvalue ) 137 $data[$varname] = $varvalue; 138 139 $data['none'] = '0'; 140 141 $link->addAttribute('data-data',str_replace(array("\t", "\r", "\n"),'',$json->encode($data))); 142 break; 143 144 case 'html': 145 case 'external': 146 147 $link->addAttribute('href',$this->url); 148 break; 149 150 default: 151 $link->addAttribute('href','/#/'.$this->action.'/'.$this->id); 152 153 } 154 155 return $link; 156 } 157 158 private function getExtraParamArray() 159 { 160 $vars = array(); 161 if (! empty($this->var1)) $vars[$this->var1] = $this->value1; 162 if (! empty($this->var2)) $vars[$this->var2] = $this->value2; 163 if (! empty($this->var3)) $vars[$this->var3] = $this->value3; 164 if (! empty($this->var4)) $vars[$this->var4] = $this->value4; 165 if (! empty($this->var5)) $vars[$this->var5] = $this->value5; 166 167 // Bei Dialogen kann der Dialog auch beim Öffnen in einem neuen Fenster direkt geöffnet werden. 168 if ( $this->type=='dialog') 169 $vars += array('dialogAction'=>$this->action,'dialogMethod'=>$this->subaction); 170 171 return $vars; 172 } 173 } 174 ?> 175