this is looking good!
I think you should check for the heighest MIGX_id. The count must not allways be the heighest one.
<!--?php $value = $doc--->getTVValue('myMigx'); $items = $modx->fromJSON($value); $next_id = 1; //get the next MIGX_id if (is_array($items)){ foreach ($items as $item){ $id = $modx->getOption('MIGX_id',$item,0)+1; if ($id > $next_id){ $next_id = $id; } } }else{ $items = array(); } $item = array( 'MIGX_id' => $next_id, 'name' => $hook->getValue('name'), 'message' => $hook->getValue('message'), 'email' => $hook->getValue('email'), 'date' => $hook->getValue('date'), ); $items[] = $item; if (!$doc->setTVValue('myMigx', $modx->toJson($items))) { $modx->log(modX::LOG_LEVEL_ERROR,'There was a problem saving your data!'); return false; } return true;
Hello,
I have a snippet that connects to an API and creates/updates resources from the information imported.
Right now, I am updating standard resource fields and text TV's. However, there are a few objects that I would like to loop through and store in a MIGX TV, but, I'm not sure how to write to a MIGX TV from a snippet and haven't been able to find much information online.
Here's a basic example of what I'm doing now:
foreach ($apiResults->data as $api_value){ $document = $modx->getObject('modResource', array('longtitle' => $api_value->animalID)); if(!empty($document)){ // update existing document $document = $modx->getObject('modResource',array('longtitle' => $api_value->animalID)); $document->set('pagetitle', $api_value->animalName); $document->set('content', $api_value->animalDescription); if ($document->save() === false){ // Save the document $modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while saving your Resource!'); return false; } $page = $modx->getObject('modResource',array('longtitle' => $api_value->animalID)); //this is where my issues are $i = 0; $pics = array(); foreach($api_value->animalPictures as $animalPictures){ if($i < 4){ $pics[$i] = $animalPictures->urlSecureFullsize; if (!$page->setTVValue('pet_picture_'.$i, $pics[$i])) { $modx->log(modX::LOG_LEVEL_ERROR,'There was a problem saving your TV animalPictures!'); return false; } } $i++; } unset($pics); //cleanup } //endif }//end foreach
Currently I have four "picture" TVs called pet_picture_0 - pet_picture_3 and am limiting the results to fit into these. However, sometimes the API returns more than four results. So, ideally I'd like to loop through the $pictures array and write it to a MIGX TV on the current resource.
I'm pretty sure I have to convert it to JSON first, but after that I have no idea what the actual syntax to update the values would be.
Any help is appreciated!
<?php
foreach ($apiResults->data as $api_value) {
$document = $modx->getObject('modResource', array('longtitle' => $api_value->animalID));
if (!empty($document)) { // update existing document
$document = $modx->getObject('modResource', array('longtitle' => $api_value->animalID));
$document->set('pagetitle', $api_value->animalName);
$document->set('content', $api_value->animalDescription);
if ($document->save() === false) { // Save the document
$modx->log(modX::LOG_LEVEL_ERROR, 'An error occurred while saving your Resource!');
return false;
}
$page = $modx->getObject('modResource', array('longtitle' => $api_value->animalID));
//this is where my issues are
$i = 1;
$pics = array();
foreach ($api_value->animalPictures as $animalPictures) {
$item = array(
'MIGX_id' => $i,
'image' => $animalPictures->urlSecureFullsize
);
$pics[] = $item;
$i++;
}
if (!$page->setTVValue('pet_pictures', $modx->toJson($pics))) {
$modx->log(modX::LOG_LEVEL_ERROR, 'There was a problem saving your TV animalPictures!');
return false;
}
unset($pics); //cleanup
} //endif
} //end foreach
<?php
/* AddNote Snippet */
/* Load our class */
$path = MODX_CORE_PATH . 'components/leads/';
$result = $modx->addPackage('leads',$path .
'model/','crm_');
if (! $result) {
return 'Failed to add package';
}
/* Get the existing Quote */
$leadId = $scriptProperties['leadId'];
$lead = $modx->getObject('Leads',
array('name'=>$leadId));
/* Show error message if quote is not found */
if (empty($lead)) {
return ('Could not find Lead with ID: ' . $leadId);
}
if (isset($_POST['submit']) &&
($_POST['submit'] == 'SubmitNote')) {
/* Form has been submitted */
[b] $value = $lead->getTVValue('notes');[/b]
$items = $modx->fromJSON($value);
$next_id = 1;
//get the next MIGX_id
if (is_array($items)){
foreach ($items as $item){
$id = $modx->getOption('notes',$item,0)+1;
if ($id > $next_id){
$next_id = $id;
}
}
}else{
$items = array();
}
$item = array(
'notes' => $next_id,
'note' => $hook->getValue('note'),
'author' => $hook->getValue('author'),
'date' => $hook->getValue('date'),
);
$items[] = $item;
if (!$lead->setTVValue('notes', $modx->toJson($items))) {
$modx->log(modX::LOG_LEVEL_ERROR,'There was a problem saving your data!');
return false;
}
} else {
/* Not a repost, just display the form */
/* The second argument to getChunk() tells MODX
* to replace the placeholders with the existing
* quote's values
*/
$output = $modx->getChunk('addNoteTpl',$lead->toArray() );
}
return $output;$lead->set('notes', $modx->toJson($items));
if (!$lead->save()) {
$modx->log(modX::LOG_LEVEL_ERROR,'There was a problem saving your data!');
return false;
}this should be:
$value = $lead->get('notes');
<?xml version="1.0" encoding="UTF-8"?>
<model package="leads" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
<object class="Leads" table="leads" extends="xPDOSimpleObject">
<field key="name" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
<field key="email" dbtype="text" phptype="string" null="false" />
<field key="phone" dbtype="text" phptype="string" null="true" />
<field key="agent" dbtype="text" phptype="string" null="true" />
<field key="prop1" dbtype="text" phptype="string" null="true" />
<field key="prop2" dbtype="text" phptype="string" null="true" />
<field key="prop3" dbtype="text" phptype="string" null="true" />
<field key="description" dbtype="text" phptype="string" null="true" />
<field key="createdon" dbtype="datetime" phptype="datetime" null="true" />
<field key="createdby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
<field key="editedon" dbtype="datetime" phptype="datetime" null="true" />
<field key="editedby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
<field key="deleted" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />
<field key="published" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />
<composite alias="Notes" class="LeadNotes" local="id" foreign="lead_id" cardinality="many" owner="local" />
</object>
<object class="Sellers" table="sellers" extends="xPDOSimpleObject">
<field key="name" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
<field key="prop" dbtype="text" phptype="string" null="false" />
<field key="agent" dbtype="text" phptype="string" null="false" />
<field key="description" dbtype="text" phptype="string" null="false" />
<field key="createdon" dbtype="datetime" phptype="datetime" null="true" />
<field key="createdby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
<field key="editedon" dbtype="datetime" phptype="datetime" null="true" />
<field key="editedby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
<field key="deleted" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />
<field key="published" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />
<composite alias="Notes" class="SellerNotes" local="id" foreign="seller_id" cardinality="many" owner="local" />
</object>
<object class="LeadNotes" table="leadnotes" extends="xPDOSimpleObject">
<field key="lead_id" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
<field key="note" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
<field key="author" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
<field key="createdon" dbtype="datetime" phptype="datetime" null="true" />
<field key="createdby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
<field key="editedon" dbtype="datetime" phptype="datetime" null="true" />
<field key="editedby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
<field key="deleted" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />
<field key="published" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />
<aggregate alias="Lead" class="Leads" local="lead_id" foreign="id" cardinality="one" owner="foreign" />
</object>
<object class="SellerNotes" table="leadnotes" extends="xPDOSimpleObject">
<field key="seller_id" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
<field key="note" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
<field key="author" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
<field key="createdon" dbtype="datetime" phptype="datetime" null="true" />
<field key="createdby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
<field key="editedon" dbtype="datetime" phptype="datetime" null="true" />
<field key="editedby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
<field key="deleted" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />
<field key="published" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />
<aggregate alias="Seller" class="Sellers" local="seller_id" foreign="id" cardinality="one" owner="foreign" />
</object>
</model>