How to traverse a graph with ArangoDB and PHP - arangodb

ArangoDB provides documents and edges as a low level way to produce graphs.
Let's say we have set up a graph with some vertices and edges.
The graph represents the relationship between the vertices.
v2 is a child of v1
v3 is a child of v2
v4 is a child of v3
v5 is a child of v1
v6 is a child of v5
We want to be able to query:
The path from v4 to v1
All descendants of v1
The children of v1
All ancestors of v4
The parents of v4
How to do that in PHP?

The way to do this is by querying ArangoDB with its AQL (ArangoDB Query language).
There are a few commands for working with graphs in AQL.
With PHP, we have to create a statement with the query and execute that.
The attached code uses the TRAVERSAL function in order to provide the results to the queries mentioned in the question.
This script sets up a document and an edge collection, fills it with vertices and connecting edges, and finally does the querying in order to provide the results.
It can be executed as is and will print out all the results.
<?php
namespace triagens\ArangoDb;
// use this and change it to the path to autoload.php of the arangodb-php client if you're using the client standalone...
// require __DIR__ . '/../vendor/triagens/ArangoDb/autoload.php';
// ...or use this and change it to the path to autoload.php in the vendor directory if you're using Composer/Packagist
require __DIR__ . '/../vendor/autoload.php';
// This function will provide us with our pre-configured connection options.
function getConnectionOptions()
{
$traceFunc = function ($type, $data) {
print "TRACE FOR " . $type . PHP_EOL;
};
return array(
ConnectionOptions::OPTION_ENDPOINT => 'tcp://localhost:8529/',
// endpoint to connect to
ConnectionOptions::OPTION_CONNECTION => 'Close',
// can use either 'Close' (one-time connections) or 'Keep-Alive' (re-used connections)
ConnectionOptions::OPTION_AUTH_TYPE => 'Basic',
// use basic authorization
/*
ConnectionOptions::OPTION_AUTH_USER => '', // user for basic authorization
ConnectionOptions::OPTION_AUTH_PASSWD => '', // password for basic authorization
ConnectionOptions::OPTION_PORT => 8529, // port to connect to (deprecated, should use endpoint instead)
ConnectionOptions::OPTION_HOST => "localhost", // host to connect to (deprecated, should use endpoint instead)
*/
ConnectionOptions::OPTION_TIMEOUT => 5,
// timeout in seconds
//ConnectionOptions::OPTION_TRACE => $traceFunc, // tracer function, can be used for debugging
ConnectionOptions::OPTION_CREATE => false,
// do not create unknown collections automatically
ConnectionOptions::OPTION_UPDATE_POLICY => UpdatePolicy::LAST,
// last update wins
);
}
// This function tries to create vertices and edges for the example
function setupVerticesAndEdges($connection, $vertexCollection, $edgeCollection)
{
echo "We are creating 6 vertices...<br> ";
//create example documents for the vertices
$nameV1 = 'v1';
$documentV1 = Document::createFromArray(
array('name' => $nameV1, '_key' => $nameV1)
);
$nameV2 = 'v2';
$documentV2 = Document::createFromArray(
array('name' => $nameV2, '_key' => $nameV2)
);
$nameV3 = 'v3';
$documentV3 = Document::createFromArray(
array('name' => $nameV3, '_key' => $nameV3)
);
$nameV4 = 'v4';
$documentV4 = Document::createFromArray(
array('name' => $nameV4, '_key' => $nameV4)
);
$nameV5 = 'v5';
$documentV5 = Document::createFromArray(
array('name' => $nameV5, '_key' => $nameV5)
);
$nameV6 = 'v6';
$documentV6 = Document::createFromArray(
array('name' => $nameV6, '_key' => $nameV6)
);
echo "We are creating 5 edges...<br> ";
//create example documents for the edges
$nameE1 = 'e1';
$documentE1 = Edge::createFromArray(
array('name' => $nameE1, 'label' => 'child_of')
);
$nameE2 = 'e2';
$documentE2 = Edge::createFromArray(
array('name' => $nameE2, 'label' => 'child_of')
);
$nameE3 = 'e3';
$documentE3 = Edge::createFromArray(
array('name' => $nameE3, 'label' => 'child_of')
);
$nameE4 = 'e4';
$documentE4 = Edge::createFromArray(
array('name' => $nameE4, 'label' => 'child_of')
);
$nameE5 = 'e5';
$documentE5 = Edge::createFromArray(
array('name' => $nameE5, 'label' => 'child_of')
);
// Get instances of the vertice- and edgehandlers
$documentHandler = new DocumentHandler($connection);
$edgeHandler = new EdgeHandler($connection);
// Save the vertices
try {
// query the given $collectionId by example using the previously declared $exampleDocument array
$result['v'][] = $documentHandler->save($vertexCollection, $documentV1);
$result['v'][] = $documentHandler->save($vertexCollection, $documentV2);
$result['v'][] = $documentHandler->save($vertexCollection, $documentV3);
$result['v'][] = $documentHandler->save($vertexCollection, $documentV4);
$result['v'][] = $documentHandler->save($vertexCollection, $documentV5);
$result['v'][] = $documentHandler->save($vertexCollection, $documentV6);
} catch (Exception $e) {
// any other error
echo ('An error occured. Exception: ' . $e);
}
// Save the edges
try {
echo "$nameV2 is a child of $nameV1<br> ";
$result['e'][] = $edgeHandler->saveEdge(
$edgeCollection,
$vertexCollection . '/' . $nameV2,
$vertexCollection . '/' . $nameV1,
$documentE1,
$options = array()
);
echo "$nameV3 is a child of $nameV2<br> ";
$result['e'][] = $edgeHandler->saveEdge(
$edgeCollection,
$vertexCollection . '/' . $nameV3,
$vertexCollection . '/' . $nameV2,
$documentE2,
$options = array()
);
echo "$nameV4 is a child of $nameV3<br> ";
$result['e'][] = $edgeHandler->saveEdge(
$edgeCollection,
$vertexCollection . '/' . $nameV4,
$vertexCollection . '/' . $nameV3,
$documentE3,
$options = array()
);
echo "$nameV5 is a child of $nameV1<br> ";
$result['e'][] = $edgeHandler->saveEdge(
$edgeCollection,
$vertexCollection . '/' . $nameV5,
$vertexCollection . '/' . $nameV1,
$documentE4,
$options = array()
);
echo "$nameV6 is a child of $nameV5<br> ";
$result['e'][] = $edgeHandler->saveEdge(
$edgeCollection,
$vertexCollection . '/' . $nameV6,
$vertexCollection . '/' . $nameV5,
$documentE5,
$options = array()
);
echo "<font style='font-family: monospace;'>";
echo "$nameV1<br> ";
echo "+ $nameV2<br> ";
echo "| + $nameV3<br> ";
echo "| | + $nameV4<br> ";
echo "+ $nameV5<br> ";
echo "+ $nameV5<br> ";
echo "| + $nameV6<br> ";
echo "</font>";
// return the result;
return $result;
} catch (Exception $e) {
// any other error
echo ('An error occured. Exception: ' . $e);
}
}
// helper function that takes the connection and the query to execute.
function doAQLQuery($connection, $query)
{
// query through AQL
$statement = new \triagens\ArangoDb\Statement($connection, array(
"query" => '',
"count" => true,
"batchSize" => 1000,
"_sanitize" => true,
));
$statement->setQuery($query);
$cursor = $statement->execute();
$result = $cursor->getAll();
return $result;
}
// AQL query example functions
// Function that gets all paths from vertex v4 to v1
function getPathFromV4ToV1($connection)
{
$query = 'FOR p IN PATHS(vertices_20130301_01, edges_20130301_01, "outbound")
FILTER p.source._id == "vertices_20130301_01/v4" && p.destination._id == "vertices_20130301_01/v1"
RETURN p';
$result = doAQLQuery($connection, $query);
return $result;
}
// Function that gets the paths to all descendants of v1
function getPathToAllDescendantsOfV1($connection)
{
$query = 'FOR p IN TRAVERSAL(vertices_20130301_01, edges_20130301_01, "vertices_20130301_01/v1", "inbound", {
strategy: "depthfirst",
minDepth:1,
paths: true,
followEdges: [ { label: "child_of" } ]
})
RETURN p
';
$result = doAQLQuery($connection, $query);
return $result;
}
// Function that gets the paths to all children of v1
function getPathToChildrenOfV1($connection)
{
$query = 'FOR p IN TRAVERSAL(vertices_20130301_01, edges_20130301_01, "vertices_20130301_01/v1", "inbound", {
strategy: "depthfirst",
maxDepth: 1,
minDepth:1,
paths: true,
followEdges: [ { label: "child_of" } ]
})
RETURN p
';
$result = doAQLQuery($connection, $query);
return $result;
}
// Function that gets the paths to all parents of v4
function getPathToParentsOfV4($connection)
{
$query = 'FOR p IN TRAVERSAL(vertices_20130301_01, edges_20130301_01, "vertices_20130301_01/v4", "outbound", {
strategy: "depthfirst",
maxDepth: 1,
minDepth:1,
paths: true,
followEdges: [ { label: "child_of" } ]
})
RETURN p
';
$result = doAQLQuery($connection, $query);
return $result;
}
// Function that gets the paths to all ancestor of v4
function getPathToAllAncestorsOfV4($connection)
{
$query = 'FOR p IN TRAVERSAL(vertices_20130301_01, edges_20130301_01, "vertices_20130301_01/v4", "outbound", {
strategy: "depthfirst",
minDepth:1,
paths: true,
followEdges: [ { label: "child_of" } ]
})
RETURN p
';
$result = doAQLQuery($connection, $query);
return $result;
}
// Function that drops collections given
function dropCollections($connection, $collections)
{
// register a collection handler to work with the 'users' collection
$collectionHandler = new CollectionHandler($connection);
echo "dropping collections...";
try {
foreach ($collections as $collection) {
$collectionHandler->drop($collection);
}
echo "dropped.<br>";
} catch (Exception $e) {
die ('Could not drop collection. Exception: ' . $e . '<br>');
}
}
// *********************************************************************************************************************
// Start example code
// register the connection to ArangoDB
$connection = new Connection(getConnectionOptions());
// register a collection handler to work with the 'users' collection
$collectionHandler = new CollectionHandler($connection);
// assign the collection names...
$vertexCollection = 'vertices_20130301_01';
$edgeCollection = 'edges_20130301_01';
// finally drop the collections...
// remark this line if you want to drop the collections by hand.
dropCollections($connection, array($vertexCollection, $edgeCollection));
// create the vertices and edges collections...
// remark those lines if you want to create the collection by hand.
echo "creating the '$vertexCollection' vertex collection...";
try {
$collection = new Collection();
$collection->setName($vertexCollection);
$collectionHandler->create($collection);
echo "created.<br>";
} catch (Exception $e) {
echo ('Could not create collection. Exception: ' . $e . '<br>');
}
echo "creating the '$edgeCollection' edge collection...";
try {
$collection = new Collection();
$collection->setName($edgeCollection);
$collection->setType(3);
$collectionHandler->create($collection);
echo "created.<br>";
} catch (Exception $e) {
echo ('Could not create collection. Exception: ' . $e . '<br>');
}
// setup our vertices and edges....
echo "trying to setup our vertices and edges... <br>";
$result = setupVerticesAndEdges($connection, $vertexCollection, $edgeCollection);
// AQL Examples
// get the path from vertex v4 to v1
$result = getPathFromV4ToV1($connection);
echo "<br>*****************************************<br>";
echo "get all paths from vertex v4 to v1<br>";
echo "<br>*****************************************<br>";
var_dump($result);
// get the paths to all descendants of v1
$result = getPathToAllDescendantsOfV1($connection);
echo "<br>*****************************************<br>";
echo "get the paths to all descendants of v1<br>";
echo "<br>*****************************************<br>";
var_dump($result);
//get the paths to all children of v1
$result = getPathToChildrenOfV1($connection);
echo "<br>*****************************************<br>";
echo "get the paths to all children of v1<br>";
echo "<br>*****************************************<br>";
var_dump($result);
// get the paths to all ancestors of v4
$result = getPathToAllAncestorsOfV4($connection);
echo "<br>*****************************************<br>";
echo "get the paths to all ancestors of v4<br>";
echo "<br>*****************************************<br>";
var_dump($result);
//get all paths to all parents of v4
$result = getPathToParentsOfV4($connection);
echo "<br>*****************************************<br>";
echo "get all paths to all parents of v4<br>";
echo "<br>*****************************************<br>";
var_dump($result);

Related

How to send customer renewal order to secondary email in Woocommerce subscription

I want to send the renewal order email to a secondary user email(which i have added in user-edit page using ACF).
I have tried many methods,woocommerce_subscription_payment_complete is also not working for me.
The following code i have tried:
add_action( 'woocommerce_order_status_completed', 'action_on_order_status_completed', 20, 2 );
function action_on_order_status_completed( $order_id, $order ){
$order = new WC_Order($order_id);
// Get the user ID from WC_Order methods
$user_id = $order->get_user_id(); // or $order->get_customer_id();
$secondary_recipient = get_field('secondary_email', 'user_'.$user_id );
$subscriptions_ids = wcs_get_subscriptions_for_order( $order_id, array( 'order_type' => 'any' ) );
// We get all related subscriptions for this order
foreach( $subscriptions_ids as $subscription_id => $subscription_obj )
if($subscription_obj->order->id == $order_id) break; // Stop the loop
// $subscription_objc = wcs_get_subscription($subscription_id);
//$userid = $subscription_objc->get_user_id();
$wc_emails = WC()->mailer()->get_emails();
$wc_emails['WCS_Email_Processing_Renewal_Order']->recipient = $secondary_recipient;
$wc_emails['WCS_Email_Processing_Renewal_Order']->trigger($subscription_id);
// $to = $secondary_recipient;
// $subject = "hi";
// $body =$user_id."end".$order_id."hhh".$subscription_id;
// $headers = array('Content-Type: text/html; charset=UTF-8');
// //$headers[] = 'Cc: sarun#cloudspring.in';
// wp_mail( $to, $subject, $body, $headers );
}
FYI:Email is sending if i use the commented wp_mail function.
We can add a secondary email as the recipient, Try the below code tested and it worked.
add_filter( 'woocommerce_email_recipient_customer_completed_renewal_order', 'my_email_recipient_filter_function', 10, 2);
function my_email_recipient_filter_function( $recipient, $order ) {
$user_id = $order->get_user_id(); // or $order->get_customer_id();
$secondary_recipient = get_field('secondary_email', 'user_'.$user_id );
if(! empty($secondary_recipient)){
$recipient = $recipient . ', '. $secondary_recipient;
return $recipient;
}else {
return $recipient;
}
}

Importing images along with other fields (username,subjectname) from excel in laravel

I am using phpspreadsheet. I want to import an excel sheet that have images too, it looks something like this,
I am able to retrieve fields separately and images separately, I want to get them together. Problem I am facing is that Images are being accessed with
$spreadsheet->getActiveSheet()->getDrawingCollection()
and for others field i have to access them like this
$spreadsheet->getRowIterator()
as both of them requires separate loops, should i be merging them into one or what is the right way so that i am able to retrieve both(images and fields) together.
Images retrieve code:
$spreadsheet = IOFactory::load($request->import_file);
$i = 0;
foreach ($spreadsheet->getActiveSheet()->getDrawingCollection() as $key => $drawing) {
if ($drawing instanceof MemoryDrawing) {
ob_start();
call_user_func(
$drawing->getRenderingFunction(),
$drawing->getImageResource()
);
$imageContents = ob_get_contents();
ob_end_clean();
switch ($drawing->getMimeType()) {
case MemoryDrawing::MIMETYPE_PNG :
$extension = 'png';
break;
case MemoryDrawing::MIMETYPE_GIF:
$extension = 'gif';
break;
case MemoryDrawing::MIMETYPE_JPEG :
$extension = 'jpg';
break;
}
} else {
$zipReader = fopen($drawing->getPath(), 'r');
$imageContents = '';
while (!feof($zipReader)) {
$imageContents .= fread($zipReader, 1024);
}
fclose($zipReader);
$extension = $drawing->getExtension();
}
$myFileName = time() .++$i. '.' . $extension;
$imagesCollection['answerImages_'.$key] =$myFileName;
file_put_contents('images/products/' . $myFileName, $imageContents);
$a = Answers::create([
'answerImages'=>$myFileName,
'questionId'=>($key <=4)?1:2,
]);
}
I want to store them into my table in database such that in questionImage column of database it has image name like this
and it is storing it currently but as I mentioned earlier i have to store them separtely
This is how i am storing other fields
$spreadsheet = IOFactory::load($the_file->getRealPath());
$sheet = $spreadsheet->getActiveSheet();
$row_limit = $sheet->getHighestDataRow();
$column_limit = $sheet->getHighestDataColumn();
$row_range = range( 1, $row_limit );
$column_range = range( 'F', $column_limit );
$startcount = 2;
$data = array();
foreach ( $row_range as $row ) {
$data[] = [
'courseName' =>$sheet->getCell( 'A' . $row )->getValue(),
'subjectName' => $sheet->getCell( 'B' . $row )->getValue(),
'question' => $sheet->getCell( 'C' . $row )->getValue(),
'questionImage' => $sheet->getCell( 'D' . $row )->getValue(),
];
$startcount++;
}
DB::table('questions')->insert($data);
How to get them together so that i can store them in one table
you should try maatwebsite/excel package. it will save your time.

Import excel data given in each repeated 5 rows to one row in database. Laravel 5.8

I've an excel file with following data. The below is the data of 2 users. Each user have 5 rows of details. I need to import the following to 2 rows in database.
The below is my table structure
What I need is, I need to import the excel in such a way, in the table there should be only 2 rows like below.
How can I do this in Laravel 5.8.
Here is my controller code
public function importMovementFile (Request $request){
$this->validate($request, [
'mcafile' => 'required|mimes:xls,xlsx,ods'
]);
$path = $request->file('mcafile')->getRealPath();
$data = \Excel::import(new UsersImport,$path);
return back()->with('success', 'Excel Data Imported successfully.');
}
UserImports
use Maatwebsite\Excel\Row;
use Maatwebsite\Excel\Concerns\OnEachRow;
class UsersImport implements OnEachRow
{
public function onRow(Row $row)
{
$rowIndex = $row->getIndex();
$row = $row->toArray();
UploadMovAnalysisDataFiles::create([
'member_name' => $row[0][$rowIndex],
]);
}
}
Okay I found the solution to this,
We can do this by checking the name inside a for loop. First of all, check whether the name is empty or not, if empty place the first name in name variable and loop throughout. Store each scores of the corresponding name in an object. When another name comes insert the first details and loop through the next and so on.
public function insertExcel
{
$obj= new UploadMovAnalysisDataFiles();
$name ='';
for($i=1;$i<$rows->count();$i++){
if($name==''){
$name = $rows[$i][0];
$id = $rows[$i][1];
$date = date('Y-m-d h:i:s', strtotime($rows[$i][2]));
$visit_date = $date;
//function call
score($rows[$i][3],$rows[$i][4];
}elseif($name==$rows[$i][0]){
//function call
score($rows[$i][3],$rows[$i][4];
}else{
UploadMovAnalysisDataFiles::create([
'member_name' => $name,
'mov_analysis_tag_id' => $id ,
'visit_date' => $date,
'fitness_score' => $obj->fscore,
'knee_score' => $obj->kscore,
'hip_score' => $obj->hscore,
'core_score' => $obj->cscore,
'shoulder_score' => $obj->sscore,
]);
$name = $rows[$i][0];
$id = $rows[$i][1];
$date = date('Y-m-d h:i:s', strtotime($rows[$i][2]));
$visit_date = $date;
//function call
score($rows[$i][3],$rows[$i][4]);
}
}
UploadMovAnalysisDataFiles::create([
'member_name' => $name,
'mov_analysis_tag_id' => $id ,
'visit_date' => $date,
'fitness_score' => $obj->fscore,
'knee_score' => $obj->kscore,
'hip_score' => $obj->hscore,
'core_score' => $obj->cscore,
'shoulder_score' => $obj->sscore,
]);
}
Function to keep each score in an object.
function score($rows[$i][3],$rows[$i][4){
if($rows[$i][3]== 'VSFitness_Score'){
$obj->fscore = $rows[$i][4];
}if($rows[$i][3]== 'knee_Score'){
$obj->kscore = $rows[$i][4];
}if($rows[$i][3]== 'Hip_Score'){
$obj->hscore = $rows[$i][4];
}if($rows[$i][3]== 'Core_Score'){
$obj->cscore = $rows[$i][4];
}if($rows[$i][3]== 'Shoulder_Score'){
$obj->sscore = $rows[$i][4];
}
}

assign users to group modx

How can i assign newly created user to the particular group in modx Programmaticaly ? Below is my code
if(isset($_POST) && count($_POST)){
$oUser = $modx->newObject('modUser');
$oUser->set('username', "test");
//$oUser->set('password', "test");
$oProfile = $modx->newObject('modUserProfile');
$oProfile->set('fullname', $_POST['fname']);
$oProfile->set('email', $_POST['email']);
$oUser->addOne($oProfile);
if($oUser->save()===false){
echo "Error";
}else
echo "Done";
}
I googled but all i find is graphical tutorial how to create groups and edit user and then assign roles, If you know any tutorial then also its fine.
Here is how I have been doing it, this is a posthook snippet that fires after a user registers [and the user is created]
<?php
$specialty = $hook->getValue('specialty');
$country = strtolower($hook->getValue('excountry'));
$username = $hook->getValue('username');
$staff = $hook->getValue('staff-or-resident'); //Staff | Resident
$joingroup = '';
$joinrole = '';
$blockuser = 'false';
switch ($specialty){
case 'Other' :
$joingroup = 15; // Other
$joinrole = 1; //member
$blockuser = 'true';
break;
// there are about 15 different groups and roles here...
default :
$joingroup = '0'; // none
$joinrole = '0'; // none
break;
}
if($joingroup > 0 && $joinrole > 0){
$user = $modx->getObject('modUser', array('username'=>$username));
$internalkey = $user->get('id');
$profile = $user->getOne('Profile',array('internalKey'=>$internalkey));
$user->joinGroup($joingroup, $joinrole);
if($blockuser == 'true'){ //block user if they belong to the "other" group
$profile->set('blocked',1);
}
if(!$user->save()){
return false;
};
}
return true;
The key is the: $user->joinGroup($joingroup, $joinrole); where joingroup is the group id ~ or name and the joinrole is the role id ~ or name. It's documented here: http://api.modx.com/revolution/2.1/_model_modx_moduser.class.html#%5CmodUser::joinGroup()
The best way to create/edit something in revo >2.2 this is use "Class-based Processors" - https://www.markhamstra.com/modx-blog/2012/2/getting-started-with-class-based-processors-2.2/ to add user to group use this processor https://github.com/modxcms/revolution/blob/develop/core/model/modx/processors/security/user/update.class.php with this -
http://rtfm.modx.com/display/revolution20/Using+runProcessor
$param = array(
'id' => 1, // user id
'groups' => array(
array(
"usergroup" => 1,
"name" => "Administrator",
"member" => 1,
"role" => 2,
"rolename" => "Super User",
"primary_group" => true,
"rank" => 0,
"menu" => null
),
array( .... )
)
);
$response = $modx->runProcessor('security/user/update',$param );
if ($response->isError()) {
return $response->getMessage();
}

Drupal hook_search function location

I can't for the life of me figure out where the hook_search function in drupal is located. Is it something I need to add to a file to access?
Hook functions don't exist by name -- they indicate a naming convention that can be followed to respond to that particular "hook"...
An example would be the node_search() function. When the search module calls module_invoke_all('search'), all functions named foo_search(), where foo is the name of an enabled module, will be called. The details of the search hook in particular are found on api.drupal.org.
function hook_search($op = 'search', $keys = null) {
switch ($op) {
case 'name':
return t('content');
case 'reset':
variable_del('node_cron_last');
return;
case 'search':
$find = do_search($keys, 'node', 'INNER JOIN {node} n ON n.nid = i.sid '. node_access_join_sql() .' INNER JOIN {users} u ON n.uid = u.uid', 'n.status = 1 AND '. node_access_where_sql());
$results = array();
foreach ($find as $item) {
$node = node_load(array('nid' => $item));
$extra = node_invoke_nodeapi($node, 'search result');
$results[] = array('link' => url('node/'. $item),
'type' => node_invoke($node, 'node_name'),
'title' => $node->title,
'user' => theme('username', $node),
'date' => $node->changed,
'extra' => $extra,
'snippet' => search_excerpt($keys, check_output($node->body, $node->format)));
}
return $results;
}
}

Resources