How I can move contact from one list to another using PHP API - constants

I am using PHP SDK for constant contact plugin. I want to move contact from one list to another. I have tried following code it will generate BAD response error.
$action = "Updating Contact";
$contact = $response->results[0];
foreach ($contact->lists as $key => $value) {
unset($contact->lists[$key]);
}
$cc->contactService->updateContact(WPYog_ACCESS_TOKEN, $contact,true);
$contact->addList((string)$_POST['list_id']);
$contact->first_name = 'Sudhir';
$contact->status = 'ACTIVE';
$contact->email_addresses[0]->status = 'ACTIVE';
$contact->last_name = 'Pandey';
try {
$cc->contactService->updateContact(WPYog_ACCESS_TOKEN, $contact,true);
}catch (CtctException $ex) {
var_dump($ex->getErrors());
}

I have found answer. First of all fetch the record by using this
$action = "Updating Contact";
$contact = $response->results[0];
// Now empty the list
$contact->lists = array();
$contact->addList((string)$_POST['list_id']);
$contact->first_name = 'Sudhir';
$contact->status = 'ACTIVE';
$contact->email_addresses[0]->status = 'ACTIVE';
$contact->last_name = 'Pandey';
try {
$cc->contactService->updateContact(WPYog_ACCESS_TOKEN, $contact,true);
}catch (CtctException $ex) {
var_dump($ex->getErrors());
}

Related

Retrieve Lead Ads Facebook API

I have problems retrieving Lead Ads.
I have the Ad-ID and the Page-ID. I haven't created them, but was added as a developer.
I was trying to use the PHP SDK and this https://developers.facebook.com/docs/marketing-api/guides/lead-ads/v2.9
Nothing is working. I cannot find a nice tutorial about that.
I just want to retrieve the leading Ads!
Anyone?
Assuming you have already installed FB API SDK and configured your FB app, you can use this to get all results from all LeadAds of your $page_id
use FacebookAds\Api;
use FacebookAds\Object\Page;
use FacebookAds\Object\Ad;
$access_token = 'YOUR TOKEN';
$app_id = 'YOUR APP ID';
$app_secret = 'YOUR APP SECRET';
$page_id = 'YOUR PAGE ID';
Api::init($app_id, $app_secret, $access_token);
$ads = getAllLeadsAds($page_id);
$result = array();
foreach ($ads->data as $item) {
$leads = getLeadAdInfo($item->id);
$i = 0;
foreach ($leads->data as $value) {
$result[$i]['ad_id'] = $item->id;
$result[$i]['lead_id'] = $value->id;
$result[$i]['form'] = $value->field_data;
$i++;
}
}
print_r($result);
function getAllLeadsAds($page)
{
$page = new Page($page);
return $page->getLeadgenForms()->getResponse()->getBody();
}
function getLeadAdInfo($ad)
{
$ad = new Ad($ad);
return $ad->getLeads()->getResponse()->getBody();
}
Its possible that you not calling all ads inside adset.
You can use facebook cursor.
$cursor->fetchAfter();
use FacebookAds\Object\AdAccount;
use FacebookAds\Object\Values\ArchivableCrudObjectEffectiveStatuses;
use FacebookAds\Object\Fields\CampaignFields;
use FacebookAds\Object\AdCampaign;
use FacebookAds\Object\Fields\AdSetFields;
use FacebookAds\Object\AdSet;
use FacebookAds\Object\Fields\AdFields;
use FacebookAds\Object\Ad;
use FacebookAds\Object\Campaign;
function getadsetname($adset_id,$acc_id){
$adset = new AdSet($adset_id, $acc_id);
$adset->read(array(
AdSetFields::NAME,
));
return $adset->name; // Outputs name of adset.
}
function get_campaignname($campaign_id){
$campaign = new Campaign($campaign_id);
$campaign->read(array(
CampaignFields::NAME,
));
return $campaign->name;
}
$account = new AdAccount($account_id);
echo $adset_name = getadsetname($adset_id,$account_id);
echo $campaign_name = get_campaignname($campaign_id);
echo "<hr>";
$adcampaign = new AdAccount($campaign_id);
$adset = new AdSet($adset_id);
$ads = $adset->getAds(array(
AdFields::NAME,
AdFields::CONFIGURED_STATUS,
AdFields::EFFECTIVE_STATUS,
AdFields::CREATIVE,
));
$ads->fetchAfter();
foreach ($ads as $ad) {
$ad_id = $ad->id;
$ad_name = $ad->name;
if($ad->configured_status=="ACTIVE"){
$ad1 = new Ad($ad_id);
$leads = $ad1->getLeads();
$leads->fetchAfter();
foreach ($leads as $lead) {
$fname = $lead->field_data;
//var_dump($fname);
$data = array();
$data['lead_id'] = $lead->id;
$data['created_time'] = $lead->created_time;
$data['ad_id'] = $ad_id;
$data['ad_name'] = $ad_name;
$data['adset_id'] = $adset_id;
$data['adset_name'] = $adset_name;//$adset_name;
$data['campaign_id'] = $campaign_id;
$data['campaign_name'] = $campaign_name;
$data['form_id'] = $lead->form_id;//$lead->id;
$data['is_organic'] = $lead->is_organic;//$lead->id;
}
}
}
$ads->fetchAfter(); will get the next list of ads and
$leads->fetchAfter(); will get the next list of leads

Zend1 form upload document file

I've got an "addForm" and a "editForm". After I have added a document file in the addForm, it will be saved in my db. If I want to edit this form, I have to upload this document again. The old document will be deleted. I would like to make a function to check whether the document is already uploaded or not, so I do not have to upload it every time I want to edit an item. I just do not know where to start. A little help will be great.
my add/editform:
$pdf = new Zend_Form_Element_File('document');
$pdf->setLabel('Nieuwe PDF')
->addValidator('extension', true, array('docx',
'docx','pdf','txt'))
->addValidator('Count', false, 1)
->addValidator('Size', false, 10240000)
->setDestination( PUBLIC_PATH . '/../data/invoicespdf/')
->setRequired(false);
Try using this in the controller...modify it to fit your needs :
if ($request->isPost()) {
if ($form->isValid($request->getPost())) {
if ('administrator' == $user->role) {
$oldFileName = $form->getElement('oldfilename')->getValue(); //the hidden field
$data = $form->getValues();
$model->populate($data);
if (file_exists('uploads/cv/' . $oldFileName)) {
$form->getElement('cv')->setIgnore(true); //this is my Form File Element - the file exists, I don't need to store the filename
} else { // if you want you can unlink $oldFileName
$upload = new Zend_File_Transfer_Adapter_Http();
$info = $upload->getFileInfo('cv');
$upload->setDestination("uploads/cv/");
if (file_exists('uploads/cv/' . $info['cv']['name'])) {
$newFileName = time() . rand(0, 100000) . "-" . $info['cv']['name']; // I need to avoid overwriting file
} else {
$newFileName = $info['cv']['name'];
$upload->addFilter('Rename', $newFileName);
}
try {
$upload->receive();
} catch (Zend_File_Transfer_Exception $e) {
$e->getMessage();
}
}
$model->save();
return $this->_helper->redirector('list');
} else {
//some error message
$this->_helper->redirector('list');
}
} else { //form not valid
$this->view->form = $form;
}
} else {
$model->find($id);
$data = array();
$data = $model->toArray();
$data['oldfilename'] = $model->get_cv(); //the filename stored in db
$form->getElement('cv')->setRequired(false);
$form->populate($data);
$this->view->form = $form;
}

Hide Bundle Product if one of the Children is outofstock

how can i filter product collection so it will return the collection which does not contain bundle product whose one of children is Out of stock.
Got the solution for my question
$collection = Mage::getResourceModel('catalog/product_collection');
$bundled_items = array();
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
foreach ($collection->getAllIds() as $proId)
{
$bundled_product=Mage::getModel('catalog/product')->load($proId);
if($bundled_product->getTypeId()=="bundle")
{
$selectionCollection = $bundled_product->getTypeInstance(true)->getSelectionsCollection(
$bundled_product->getTypeInstance(true)->getOptionsIds($bundled_product), $bundled_product
);
foreach($selectionCollection as $option)
{
$product = Mage::getModel('catalog/product')->load($option->getProductId());
$stockItem = $product->getStockItem();
if($product->stock_item->is_in_stock == 0)
{
$bundled_items[] = $proId;
}
}
}
}
if(isset($bundled_items) && !empty($bundled_items))
{
$collection->addFieldToFilter('entity_id',array( 'nin' => array_unique($bundled_items)));
}
Alternate answer
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
$otherProductIds = $collection->getAllIds();
//get only bundle
$collection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('type_id','bundle');
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
$bundleIds = $collection->getAllIds();
//checking bundle associate product stock status
$readAdapter = Mage::getSingleton('core/resource')->getConnection('core_read');
$select = $readAdapter->select()
->from(array('css'=> Mage::getSingleton('core/resource')->getTableName('cataloginventory/stock_status')),array())
->join(
array('bs'=> Mage::getSingleton('core/resource')->getTableName('bundle/selection')),
'bs.product_id = css.product_id',
array('parent_product_id')
)
->where('bs.parent_product_id IN (?)',$bundleIds)
->where('css.stock_status = 0')
->group('bs.parent_product_id');
$excludeBundleIds = $readAdapter->fetchCol($select);//return outstock associated products parent ids
$allIds = array_merge($otherProductIds, array_diff($bundleIds,$excludeBundleIds));
$collection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('entity_id',array( 'in' => $allIds))
->addMinimalPrice()
->addFinalPrice();
return $collection
Hope this will helpful

Zend Mail and encodings, content-transfer etc. - Unified?

Is there any class in the Zend Framework that allows me to easily read emails?
The Zend_Mail class does allow me to easy get headers, subject and the content body. But transferring everything to UTF-8 and human-readable format is still a pain.
Or am I doing something wrong? As far as I can tell, Zend Framework does not allow me to easily get UTF-8 strings that I can just use, I still have to do some post-processing. Right?
The key thing is that you need to iterate over the parts within the Message and find the text. Once you have it, then you can use quoted_printable_decode to get the text itself in a useful way.
This is some rough and ready code that reads IMAP email boxes with Zend_Mail:
<?php
$mail = new Zend_Mail_Storage_Imap(array(
'host' => EMAIL_ACCOUNT_HOST,
'user' => EMAIL_ACCOUNT_USERNAME,
'password' => EMAIL_ACCOUNT_PASSWORD,
));
echo (int)$mail->countMessages() . " messages found\n";
foreach ($mail as $message) {
$from = $message->getHeader('from');
$subject = trim($message->subject);
$to = trim($message->to);
$body = getBody($message);
// do something with message here
}
function getBody(Zend_Mail_Message $message)
{
// find body
$part = $message;
$isText = true;
while ($part->isMultipart()) {
$foundPart = false;
$iterator = new RecursiveIteratorIterator($message);
foreach ($iterator as $part) {
// this detection code is a bit rough and ready!
if (!$foundPart) {
if (strtok($part->contentType, ';') == 'text/html') {
$foundPart = $part;
$isText = false;
break;
} else if (strtok($part->contentType, ';') == 'text/plain') {
$foundPart = $part;
$isText = true;
break;
}
}
}
if($foundPart) {
$part = $foundPart;
break;
}
}
$body = quoted_printable_decode($part->getContent());
}

Zend_Form_Element fails when i addElements

I have been having trouble adding a hidden zend form element.
when i invoke addElements the form fails and prints the following error to the page.
but only when i try and add $formContactID and $formCustomerID.
Fatal error: Call to a member function getOrder() on a non-object in /home/coder123/public_html/wms2/library/Zend/Form.php on line 3291
My code is as follows.
private function buildForm()
{
$Description = "";
$FirstName = "";
$LastName = "";
$ContactNumber = "";
$Fax = "";
$Position = "";
$Default = "";
$custAddressID = "";
$CustomerID = "";
$Email = "";
$ContactID = "";
if($this->contactDetails != null)
{
$Description = $this->contactDetails['Description'];
$CustomerID = $this->contactDetails['CustomerID'];
$FirstName = $this->contactDetails['FirstName'];
$LastName = $this->contactDetails['LastName'];
$ContactNumber = $this->contactDetails['ContactNumber'];
$Position = $this->contactDetails['Position'];
$Fax = $this->contactDetails['Fax'];
$Email = $this->contactDetails['Email'];
$Default = $this->contactDetails['Default'];
$custAddressID = $this->contactDetails['custAddressID'];
$ContactID = $this->contactDetails['custContactID'];
}
$formfirstname = new Zend_Form_Element_Text('FirstName');
$formfirstname->setValue($FirstName)->setLabel('First Name:')->setRequired();
$formlastname = new Zend_Form_Element_Text('LastName');
$formlastname->setLabel('Last Name:')->setValue($LastName)->setRequired();
$formPhone = new Zend_Form_Element_Text('ContactNumber');
$formPhone->setLabel('Phone Number:')->setValue($ContactNumber)->setRequired();
$formFax = new Zend_Form_Element_Text('FaxNumber');
$formFax->setLabel('Fax Number:')->setValue($Fax);
$FormPosition = new Zend_Form_Element_Text('Position');
$FormPosition->setLabel('Contacts Position:')->setValue($Position);
$FormDescription = new Zend_Form_Element_Text('Description');
$FormDescription->setLabel('Short Description:')->setValue($Description)->setRequired();
$formEmail = new Zend_Form_Element_Text('Email');
$formEmail->setLabel('Email Address:')->setValue($Email);
$FormDefault = new Zend_Form_Element_Checkbox('Default');
$FormDefault->setValue('Default')->setLabel('Set as defualt contact for this business:');
if($Default == 'Default')
{
$FormDefault->setChecked(true);
}
$formCustomerID = new Zend_Form_Element_Hidden('customerID');
$formCustomerID->setValue($customerID);
if($this->contactID != null)
{
$formContactID = new Zend_Form_Element_Hidden('ContactID');
$formContactID->setValue($this->contactID);
}
// FORM SELECT
$formSelectAddress = new Zend_Form_Element_Select('custAddress');
$pos = 0;
while($pos < count($this->customerAddressArray))
{
$formSelectAddress->addMultiOption($this->customerAddressArray[$pos]['custAddressID'], $this->customerAddressArray[$pos]['Description']);
$pos++;
}
$formSelectAddress->setValue(array($this->contactDetails['custAddressID']));
$formSelectAddress->setRequired()->setLabel('Default Address For this Contact:');
// END FORM SELECT
$this->setMethod('post');
$this->setName('FormCustomerEdit');
$formSubmit = new Zend_Form_Element_Submit('ContactSubmit');
$formSubmit->setLabel('Save Contact');
$this->setName('CustomerContactForm');
$this->setMethod('post');
$this->addElements(array($FormDescription, $formfirstname, $formlastname,
$FormPosition, $formPhone, $formFax, $FormDefault,
$formEmail, $formSelectAddress, $formContactID, $formCustomerID, $formSubmit));
$this->addElements(array($formSubmit));
}
Maybe you've already fixed, but just in case.
I was having the same issue and the problem was the name of certain attributes within the form. In your case you have:
if($this->contactID != null){
$formContactID = new Zend_Form_Element_Hidden('ContactID');
$formContactID->setValue($this->contactID);
}
In the moment that you have added $formContactID to the form a new internal attribute has been created for the form object, this being 'ContactID'. So now we have $this->ContactID and $this->contactID.
According to PHP standards this shouldn't be a problem because associative arrays keys and objects attribute names are case sensitive but I just wanted to use your code to illustrate the behaviour of Zend Form.
Revise the rest of the code in your form to see that you are not overriding any Zend Element. Sorry for the guess but since you didn't post all the code for the form file it's a bit more difficult to debug.
Thanks and I hope it helps.
I think the problem is on $this->addElements when $formContactID is missing because of if($this->contactID != null) rule.
You can update your code to fix the problem:
.....
$this->addElements(array($FormDescription, $formfirstname, $formlastname,
$FormPosition, $formPhone, $formFax, $FormDefault,
$formEmail, $formSelectAddress, $formCustomerID, $formSubmit));
if(isset($formContactID)) {
$this->addElements(array($formContactID));
}
......