-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrmFindContactWidget.php
More file actions
338 lines (312 loc) · 10.7 KB
/
Copy pathCrmFindContactWidget.php
File metadata and controls
338 lines (312 loc) · 10.7 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<?php
namespace app\components;
use yii\base\Widget;
use yii\helpers\Html;
use yii\helpers\Url;
/*
usage:
1. define layout:
<div id='entire_finder_group'>
<input type='text' id='my_contact' value='...' />
<input type='text' id='my_contact_label' value='...' />
<button id='my_contact_buscar'
class='btn btn-success' title='Buscar Contacto'>
<span class='glyphicon glyphicon-search'></span></button>
</div>
2. setup the widget:
<?=\app\components\CrmFindContactWidget::widget([
'selector'=>'#my_contact',
'selector_label'=>'#my_contact_label',
'selector_activator'=>'#my_contact_buscar',
'selector_finder'=>'#entire_finder_group',
]);?>
how it works:
1. Clicking the 'selector_activator' will show the finder and will
hide the entire finder group.
2. User perform a search using provided controls
3. User click a contact, the selectors receive result.
4. after selection, try:
console.log($('#my_contact').data('contact'));
*/
class CrmFindContactWidget extends Widget
{
public $mode='finder'; // finder or browser
public $crm_field = 'list'; // which attribute in meta will be show
public $placeholder='Busque tipeando algo y de click en lupa';
public $selector; // jQuery selector of the element receiving the contact
public $selector_label; // jQuery selector of the element receiving the label
public $selector_activator; // jQuery selector of launcher
public $selector_finder; // jQuery selector of the entire finder
public $form; // a user provided html layout containing fields, or null
public $readonly = true; // set to false to enable add and update oprs.
public $default_form_layout = "
<hr/>
<div class='panel panel-default'>
<div class='panel-heading'>Datos del Contacto</div>
<div class='panel-body'>
<form class='default-form'>
%form%
<div class='buttons'>
<button type='button'
class='btn btn-primary save'>Guarda</button>
<button type='button'
class='btn btn-default cancel'>Cierra</button>
</div>
</form>
</div>
</div>
";
public $find_action_url = ['/crm/ajaxfind'];
public $get_action_url = ['/crm/ajaxget'];
public $save_action_url = ['/crm/ajaxsave'];
public $create_action_url = ['/crm/ajaxcreate'];
// THE SELECTED COLUMNS TO BE DISPLAYED ARE CONFIGURED IN CRM-CONFIG FILE
// VIA SETTING THE 'list' ATTRIBUTE TO A VALUE GREATHER THAN ZERO.
public function getapi(){
return \Yii::$app->crm;
}
public function init() {
parent::init();
$this->find_action_url = Url::toRoute($this->find_action_url);
$this->get_action_url = Url::toRoute($this->get_action_url);
$this->save_action_url = Url::toRoute($this->save_action_url);
$this->create_action_url = Url::toRoute($this->create_action_url);
if($this->readonly)
$this->save_action_url = null;
}
public function run() {
$readonly = $this->readonly ? 'true' : 'false';
$c = 'crm-find-contact-widget';
$add_form = $this->getAddForm();
$html = "
<div class='{$c}'>
<div class='{$c}-finder' style='display: none;'>
<div class='input-group'>
<input type='text' placeholder='{$this->placeholder}'
class='form-control {$c}-input' maxlength=30 />
<span class='input-group-btn'>
<button title='buscar' class='btn btn-primary {$c}-button'>
<span class='glyphicon glyphicon-search'></span>
</button>
<button title='crear' class='btn btn-success {$c}-add'>
<span class='glyphicon glyphicon-plus'></span>
</button>
<button title='volver' class='btn btn-default {$c}-close'>
<span class='glyphicon glyphicon-share-alt'></span>
</button>
</span>
</div>
<div class='{$c}-form'>$add_form</div>
<div class='{$c}-list'></div>
</div>
</div>
";
$this->view->registerJs("
console.log('initialize $c');
var _crm_handle_choose_contact = function(d){
d.list.find('[type=radio]').click(function(e){
var current = $('{$this->selector}');
var current_label = $('{$this->selector_label}');
var _c = JSON.parse(window.atob($(this).attr('data')));
console.log(_c);
var full='',spc='';
$.each(_c,function(attr,value){
if('id' != attr){
full += spc+value;
spc=' ';
}
});
current_label.val(full);
current.val(_c.id);
current.data('contact',_c); // <-- FOR PUBLIC USAGE
current.trigger('change');
$('{$this->selector_finder}').show();
d.finder.hide();
});
d.list.find('tbody tr').each(function(i1,tr){
var data = null;
var col1 = null;
$(tr).find('td').each(function(i2,td){
if(i2==0) data = $(td).find('input[name=crm-contact]').attr('data');
if(i2==1) {
col1 = $(td).html().trim();
if('' == col1) col1 = '(sin nombre)';
col1 = '<a href=\"#\" title=\"click edit\" '
+'data=\"'+data+'\" class=\"edit-contact\">'+col1+'</a>';
$(td).html(col1);
}
});
});
d.list.find('.edit-contact').click(function(e){
e.preventDefault();
var a = $(this);
var data = JSON.parse(window.atob(a.attr('data')));
console.log('edit contact:',a.html(),data);
_launch_form(data);
});
};
$('.{$c}').each(function(){
var widget = $(this);
console.log('initialize $c, widget detected.');
var find_action_url = '{$this->find_action_url}';
var get_action_url = '{$this->get_action_url}';
var save_action_url = '{$this->save_action_url}';
var create_action_url = '{$this->create_action_url}';
var find = widget.find('.{$c}-button');
var close = widget.find('.{$c}-close');
var add = widget.find('.{$c}-add');
$('.{$c}-list').addClass('result-list');
// form begin:
var form_save = widget.find('.default-form .save');
var form_cancel = widget.find('.default-form .cancel');
// form end.
close.click(function(e){
$('{$this->selector_finder}').show();
$('.{$c}-finder').hide();
});
find.click(function(e){
var crmdata = widget.data('crmdata');
var input = widget.find('.{$c}-input');
var finder = widget.find('.{$c}-finder');
var list = widget.find('.{$c}-list');
var keywords = input.val().trim();
//if(!keywords.length) return;
$('.{$c}-list').show();
$('.{$c}-form').hide();
var _clear = function(){
}
//--find click ajax begins
var view_mode = 'choose';
if('browser'=='{$this->mode}')
view_mode = 'browse';
var _d = { keywords: keywords , view: view_mode,
button_class: 'none' , crm_field : '{$this->crm_field}'};
console.log('ajax',_d);
$.ajax({ cache: false, type: 'post', async: true, data: _d,
url: find_action_url, success: function(resp){
console.log('success',resp);
_clear();
list.html('');
if(true == resp.status){
list.html(resp.html);
_crm_handle_choose_contact(
{ widget: widget , finder: finder,
list: list , data : crmdata });
try{
var evt = 'crm:find:list:updated';
console.log('crm:firing:event',evt);
$( document ).trigger(evt, [list,keywords,resp]);
console.log('crm:fired:event',evt);
}catch(e){ console.log(
'crm:list:excepcion when calling event',e); }
}else{
list.html('sin resultados');
}
}, error: function(e){
console.log(e.responseText); _clear();
list.html('(sin resultados. ocurrio un error)');
}
});
//--find click ajax ends
}); // find click
add.click(function(e){
console.log('crm add clicked');
$('.crmfield').val('');//cleared
var form = $('.{$c}-form input[name=id]').val('');
_launch_form(null);
}); // add click
form_save.click(function(e){
var form = $('.{$c}-form');
var url = save_action_url;
if('' == form.find('[name=id]').val())
url = create_action_url;
var _data = [];
form.find('.crmfield').each(function(){
var value = $(this).val().trim();
var attr = $(this).attr('name');
_data.push({ name: attr, value: value });
});
console.log('sending',_data);
$.ajax({ cache: false, type: 'post', async: true,
data: _data,
url: url, success: function(resp){
console.log('success save',resp);
form.hide();
}, error: function(e){
console.log(e.responseText); _clear();
}
});
});
form_cancel.click(function(e){
$('.{$c}-form').hide();
});
_render_form = function(form, contact){
console.log('initialize form with',contact);
$.each(contact,function(attr,value){
console.log(attr,value);
form.find('[name='+attr+']').val(value);
});
};
_launch_form = function(current_contact){
console.log('launch form:',current_contact);
var widget = $('.{$c}');
try{
var evt = 'crm:form:launch';
console.log('crm:firing:event',evt);
$( document ).trigger(evt, [widget,current_contact]);
}catch(e){ console.log(
'exception '+evt+' when calling event',e); }
//$('.{$c}-list').hide();
var form = $('.{$c}-form');
form.find('.crmfield').val('');//cleared
if(null != current_contact){
$.ajax({ cache: false, type: 'post', async: true,
data: { select : true ,
contact_id : current_contact.id },
url: get_action_url, success: function(resp){
try{
var evt = 'crm:form:render:contact';
console.log('crm:firing:event',evt);
$( document ).trigger(evt,
[widget,current_contact,resp]);
}catch(e){ console.log(
'exception '+evt+' when calling event',e); }
_render_form(form, resp);
}, error: function(e){
console.log(e.responseText); _clear();
}
});
}
form.show();
};
});
console.log('initialize activator: {$this->selector_activator}');
$('{$this->selector_activator}').click(function(){
console.log('activator clicked');
$('{$this->selector_finder}').hide();
$('.{$c}-form').hide();
$('.{$c}-finder').show();
$('.{$c}-input').focus();
console.log('activator clicked ends.');
});
if(true == {$readonly})$('.{$c}-form .save').remove();
if(true == {$readonly})$('.{$c}-add').remove();
if('browser'=='{$this->mode}'){
$('.{$c}-close').remove();
$('{$this->selector_activator}').trigger('click');
}
console.log('initialize $c is done');
",\yii\web\View::POS_READY,'crm-find-contact-widget-scripts');
return $html;
}
private function getAddForm(){
if(null != $this->form) return $this->form;
$form = "<input type='hidden' name='id' />";
$form .= $this->readonly ?
$this->api->formViewConstructor() :
$this->api->formEditConstructor();
$form = str_replace("%form%",$form,
$this->default_form_layout);
return $form;
} // getAddForm
}