QAjaxAutoCompleteTextBox as metacontrol
Hello
I've being using the QAjaxAutoCompleteTextBox plugin, it's great and very very useful, specially with the new onSelect event. Now, what I tried to accomplish is to generate a new metacontrol for it but something is not working.
In this line in QAjaxAutoCompleteTexBox:
$arrToSerialize = $parent->{$this->strCallback}($strParameter);
the function that gets called is that of the form, not the one in DocMetaControl, I will try some fix of my own, but I'm kind of newbie in php and qcubed, so, if some off you can help me with a nice solution it would be great!
here is what I did so far:
In DocMetaControl.class.php:
<?php
public function acAlu_Create($strControlId = null) {
$this->acAlu = new QAjaxAutoCompleteTextBox($this->objParentObject, 'acAluCallback');
$this->acAlu->Name = QApplication::Translate('Alumno');
$this->acAlu->Required = true;
$this->acAlu->SetSelectCallback($this, 'acAluOnSelect');
$this->acAlu->Width = 250;
$this->acAlu->MinChars = 3;
$this->acAlu->MustMatch = false;
$this->acAlu->Text = $this->objDoc->AluIDObject->Apellido;
return $this->acAlu;
function acAluCallback($buscar) {
$objAluArray = Alu::QueryArrayCached(
QQ::OrCondition(
QQ::Like(QQN::Alu()->Nombre, $buscar . '%'),
QQ::Like(QQN::Alu()->Apellido, $buscar . '%')
),
QQ::Clause(QQ::OrderBy(QQN::Alu()->Nombre)));
$alus = array();
foreach ($objAluArray as $alu) {
$alus[] = $alu->Apellido . '|' . $alu->Id;
return $alus;
}
function acAluOnSelect($sel) {
$selArray = explode('|', $sel);
//fb::log($selArray);
$this->objDoc->AluIDObject = Alu::Load($selArray[1]);
}
}
?>p.d.
It wold be incredible useful, I think for many, that the code generator generate a metacontrol of this kind!!

jpsala - what you're asking for absolutely makes sense.
The key to the issue is here:
$parent = isset($this->objParentControl) ? $this->objParentControl : $this->objForm;$arrToSerialize = $parent->{$this->strCallback}($strParameter);
Note that because the MetaControl isn't the real "parent" of the QAjaxAutoCompleteTextBox, the $parent variable contains the QForm, and thus, the strCallback function is called on the form, not the metacontrol.
How to fix this: you need to modify the plugin's code. Good news: it's easy :-).
1) Download the latest plugin bits form the SVN.
2) Add a new class variable called $objParent. Change the constructor QAjaxAutoCompleteTextBox to accept one more parameter - $objParent - and have it have the default value of null.
3) If the new parameter is null, follow the same parent definition logic that is there now (if objParentControl is set, use it for the new class variable, otherwise use objForm). If it is NOT null - which it won't be in your case, it will be set to the instance of the MetaControl.
4) Modify the
$parent->{$this->strCallback}($strParameter)to be
$this->objPparent->{$this->strCallback}($strParameter)That's really it, let me know if this works out for you. If it does, my only ask is that you share your code with the community :-)
Alex, you are very! kind, didn't expect your answer so fast
That is exactly what I was trying, except that I was creating a class from QAjaxAutoCompleteTextBox and doing the modifications there, do you think it's ok?
You can certainly do that, but that would mean that integrating your work back into the plugin would be hard (i.e. you won't be able to share the code with the community).
I tryed to extend QAjaxAutoCompleteTextBox:
<?php
class QAjaxAutoCompleteTextBoxJP extends QAjaxAutoCompleteTextBox{
protected $objForCallbak;
public function __construct($objParentObject, $strCallback, $strControlId = null, $objForCallback = null) {
parent::__construct($objParentObject, $strControlId);
$this->objForCallbak = $objForCallback;
}
public function callbackAjax($strFormId, $strControlId, $strParameter) {
if (isset($this->objForCallbak)) {
$parent = $this->objForCallbak;
} else
$parent = (isset($this->objParentControl) ? $this->objParentControl : $this->objForm);
try {
$arrToSerialize = $parent->{$this->strCallback}($strParameter);
} catch (Exception $e) {
echo "There's been an error processing auto-complete matches: \n";
throw $e;
}
if (is_null($arrToSerialize)) {
return;
}
if (!is_array($arrToSerialize)) {
echo 'Error: the callback method for QAutoCompleteTextBox must return an array of strings';
throw new QCallerException("callback method for QAutoCompleteTextBox must return an array of strings");
}
foreach ($arrToSerialize as $item) {
echo $item . "\n";
}
// Critically important - stop the processing and don't allow any other
// QCubed events to fire in this case
exit;
}
}
?>
but the callbackAjax function gets called from the QAjaxAutoCompleteTextBox, not from mine, any tips? thanks!
oh, it's ok then, I'll try to change the plugin instead, again, thanks a lot for your help!