วันศุกร์ที่ 24 มิถุนายน พ.ศ. 2559

Drupal 8
Refer : // https://www.sitepoint.com/drupal-8-version-entityfieldquery/


Services Login/Logout

Custom login/logou/register


#ใน Drupal 8 การจะเช็กว่า User ใหน Login อยู่
$account = \Drupal::currentUser();
if ($account->id() == 1) {
   return "Hiya, boss!";
}else {
   return "You are not the site administrator.";
}

1. การ สร้าง Node  ใหม่ && file
// Create Node Object ----------------------------
// Create file object from remote URI.

// การที่เรา post image file โดยเข้ารหัส base64 
$data = base64_decode("xxx");  

// ดึง file จาก URI 
$data = file_get_contents('https://www.google.co.th/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png');

// บันทึก ลงใน folder users(โดยที่เราจำเป็นต้องสร้างไว้ก่อน แล้วก็ chmod = 777 ด้วย)
$file = file_save_data($data, 'public://users/druplicon.png', FILE_EXISTS_REPLACE);
// Create node object with attached file.
$node = Node::create([    
      'type'        => 'article',    
      'title'       => 'Druplicon test',    
      'body'        => 'My Body',    
      'field_image' => [        
          'target_id' => $file->id(),        
          'alt' => 'Hello world',        
          'title' => 'Goodbye world'    
      ],
]);

// บันทึก
$node->save();


2. การ Update Node เราจำเป็นต้องรู้ Node ID ด้วยถึงจะทำการ Update Node นั้นได้
// Update Node Object ----------------------------
// เราจะทำการ Load Node ID = 52 
$node = Node::load(52);
//set value for field
$node->type  = "article";
$node->title = "Update Title - article";
$node->body->value = 'My Body';
$node->body->format = 'full_html';
//save to update 
node$node->save();

3. ผมขอยกตัวอย่าง Module Profile
3.1 การ Query
$uid = 1;  // User ID  
$query = \Drupal::entityQuery('profile')
    ->condition('uid', $uid); /* การ query จาก entity profile โดย condition uid = 1 */
// จะได้ Node ID
$nids = $query->execute(); 

กรณีเราต้องการดึง  field ของ entity == profile เราสามารถเรียกใช้งานได้ 2 แบบด้วยกัน
// สามารถ load entity ได้ 2 แบบ
1. $profile = Profile::load(2);  // แบบที่ 1
2. $profile = entity_load("profile", reset($nids) /* reset จะเป้นการ ดึง ค่า index = 0 เป็นคำสั่งของ php นั้น เอง */);   // แบบที่ 2

// การ access value field
// dsm($profile->get('field_parse_id')->getValue());  
// dsm($profile->get('field_parse_id')->getValue()[0]['value']);
// กรณีต้องการ update ค่า
$profile->field_parse_id = "654321";
$profile->save();

3.2 การ Create
/* กรณียังไม่ได้ สร้าง Profile ให้สร้างให้ uid */
$profile = Profile::create([
    'type' => 'main',    
    'uid' => $uid,   
    'field_parse_id'=>array('value'=>'-x-'),
]);
$profile->save();


4. Query Database
  4.1 ตัวอยา่งการ Query Database 

$query = \Drupal::entityQuery('node')
    ->condition('status', 1)
    ->condition("type", "article");
$nids = $query->execute();

อธิบาย
 เป็น การ Query Table = node โดยที่ status == 1 And type=='acticle' จากการ execute จากได้ array nide ทั่งหมดออกมา

จากการที่เราได้ array nids ออกมาทั้งหมดแล้ว เราสามารถ load node มาใช้งานได้ 2 แบบ
แบบที่ 1 
   $node = entity_load('node', 40);  // 40 คือ node id ที่เราต้องการดึงออกมาใช้งาน

การอ้างอิง field สามารถทำได้ดังนี้
   dsm($node->get('nid')->getValue());
   dsm($node->get('type')->getValue());
   dsm($node->get('body')->getValue());
   dsm($node->get('langcode')->getValue());

แบบที่ 2 
   $nodes = entity_load_multiple('node', $nids); 
   foreach ($nodes as $value) {
      dsm($value);  
      dsm($value->get('nid')->getValue()); 
      dsm($value->get('type')->getValue());  
      dsm($value->get('body')->getValue());  
      dsm($value->get('langcode')->getValue());
   }

   4.2 Join Table
   Refer : http://blog.danjuls.se/drupal-8-database-select-with-join/
         : http://drupal.stackexchange.com/questions/133553/how-to-get-all-rows-from-a-database-table
   $query = \Drupal::database()->select('node_revision', 'nr');
   $query->join('node', 'n', 'n.vid = nr.vid');   
   $query->leftJoin('users_field_data', 'u', 'u.uid = nr.revision_uid');   
   $query->fields('nr', ['revision_uid'])
     ->fields('u', ['name', 'mail'])
     ->condition('n.nid', 23);

   $result = $query->execute();
   while($record = $result->fetchAssoc()){     
     print_r($record);   
    }
5. การหา path file จาก fid
   Refer : http://drupal.stackexchange.com/questions/105064/how-to-get-the-absolute-path-for-files-based-on-fid

   # การ get path file image
   $file = file_load(11 /* field_image_target_id, fid */);
   dsm($file->get('uri')->getValue()); 
   //public://2016-06/generateImage_fTIMI3.jpeg# Example การ ดึง URL ตัวอยา่ง เช่น http://localhost/sites/default/files/2016-06/generateImage_fTIMI3.jpeg
   $url = file_create_url("public://2016-06/generateImage_fTIMI3.jpeg");  
   dsm($url);


Drupal 8
Refer : // https://www.sitepoint.com/drupal-8-version-entityfieldquery/


Services Login/Logout

Custom login/logou/register


#ใน Drupal 8 การจะเช็กว่า User ใหน Login อยู่
$account = \Drupal::currentUser();
if ($account->id() == 1) {
   return "Hiya, boss!";
}else {
   return "You are not the site administrator.";
}

1. การ สร้าง Node  ใหม่ && file
// Create Node Object ----------------------------
// Create file object from remote URI.

// การที่เรา post image file โดยเข้ารหัส base64 
$data = base64_decode("xxx");  

// ดึง file จาก URI 
$data = file_get_contents('https://www.google.co.th/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png');

// บันทึก ลงใน folder users(โดยที่เราจำเป็นต้องสร้างไว้ก่อน แล้วก็ chmod = 777 ด้วย)
$file = file_save_data($data, 'public://users/druplicon.png', FILE_EXISTS_REPLACE);
// Create node object with attached file.
$node = Node::create([    
      'type'        => 'article',    
      'title'       => 'Druplicon test',    
      'body'        => 'My Body',    
      'field_image' => [        
          'target_id' => $file->id(),        
          'alt' => 'Hello world',        
          'title' => 'Goodbye world'    
      ],
]);

// บันทึก
$node->save();


2. การ Update Node เราจำเป็นต้องรู้ Node ID ด้วยถึงจะทำการ Update Node นั้นได้
// Update Node Object ----------------------------
// เราจะทำการ Load Node ID = 52 
$node = Node::load(52);
//set value for field
$node->type  = "article";
$node->title = "Update Title - article";
$node->body->value = 'My Body';
$node->body->format = 'full_html';
//save to update 
node$node->save();

3. ผมขอยกตัวอย่าง Module Profile
3.1 การ Query
$uid = 1;  // User ID  
$query = \Drupal::entityQuery('profile')
    ->condition('uid', $uid); /* การ query จาก entity profile โดย condition uid = 1 */
// จะได้ Node ID
$nids = $query->execute(); 

กรณีเราต้องการดึง  field ของ entity == profile เราสามารถเรียกใช้งานได้ 2 แบบด้วยกัน
// สามารถ load entity ได้ 2 แบบ
1. $profile = Profile::load(2);  // แบบที่ 1
2. $profile = entity_load("profile", reset($nids) /* reset จะเป้นการ ดึง ค่า index = 0 เป็นคำสั่งของ php นั้น เอง */);   // แบบที่ 2

// การ access value field
// dsm($profile->get('field_parse_id')->getValue());  
// dsm($profile->get('field_parse_id')->getValue()[0]['value']);
// กรณีต้องการ update ค่า
$profile->field_parse_id = "654321";
$profile->save();

3.2 การ Create
/* กรณียังไม่ได้ สร้าง Profile ให้สร้างให้ uid */
$profile = Profile::create([
    'type' => 'main',    
    'uid' => $uid,   
    'field_parse_id'=>array('value'=>'-x-'),
]);
$profile->save();


4. Query Database
  4.1 ตัวอยา่งการ Query Database 

$query = \Drupal::entityQuery('node')
    ->condition('status', 1)
    ->condition("type", "article");
$nids = $query->execute();

อธิบาย
 เป็น การ Query Table = node โดยที่ status == 1 And type=='acticle' จากการ execute จากได้ array nide ทั่งหมดออกมา

จากการที่เราได้ array nids ออกมาทั้งหมดแล้ว เราสามารถ load node มาใช้งานได้ 2 แบบ
แบบที่ 1 
   $node = entity_load('node', 40);  // 40 คือ node id ที่เราต้องการดึงออกมาใช้งาน

การอ้างอิง field สามารถทำได้ดังนี้
   dsm($node->get('nid')->getValue());
   dsm($node->get('type')->getValue());
   dsm($node->get('body')->getValue());
   dsm($node->get('langcode')->getValue());

แบบที่ 2 
   $nodes = entity_load_multiple('node', $nids); 
   foreach ($nodes as $value) {
      dsm($value);  
      dsm($value->get('nid')->getValue()); 
      dsm($value->get('type')->getValue());  
      dsm($value->get('body')->getValue());  
      dsm($value->get('langcode')->getValue());
   }

   4.2 Join Table
   Refer : http://blog.danjuls.se/drupal-8-database-select-with-join/
         : http://drupal.stackexchange.com/questions/133553/how-to-get-all-rows-from-a-database-table
   $query = \Drupal::database()->select('node_revision', 'nr');
   $query->join('node', 'n', 'n.vid = nr.vid');   
   $query->leftJoin('users_field_data', 'u', 'u.uid = nr.revision_uid');   
   $query->fields('nr', ['revision_uid'])
     ->fields('u', ['name', 'mail'])
     ->condition('n.nid', 23);

   $result = $query->execute();
   while($record = $result->fetchAssoc()){     
     print_r($record);   
    }
5. การหา path file จาก fid
   Refer : http://drupal.stackexchange.com/questions/105064/how-to-get-the-absolute-path-for-files-based-on-fid

   # การ get path file image
   $file = file_load(11 /* field_image_target_id, fid */);
   dsm($file->get('uri')->getValue()); 
   //public://2016-06/generateImage_fTIMI3.jpeg# Example การ ดึง URL ตัวอยา่ง เช่น http://localhost/sites/default/files/2016-06/generateImage_fTIMI3.jpeg
   $url = file_create_url("public://2016-06/generateImage_fTIMI3.jpeg");  
   dsm($url);


วันอังคารที่ 24 พฤษภาคม พ.ศ. 2559

Config Paser server ให้สามารถ ส่ง push ได้

index.js
var api = new ParseServer({
  databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
  cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
  appId: process.env.APP_ID || 'myAppId',
  masterKey: process.env.MASTER_KEY || '', //Add your master key here. Keep it secret!
  // fileKey: 'XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
  serverURL: process.env.SERVER_URL || 'http://localhost:1337/parse',  // Don't forget to change to https if needed
  liveQuery: {
    classNames: ["Posts", "Comments"] // List of classes to support for query subscriptions
  },
  push: {   
    android: {
        senderId: '...',
        apiKey: '...'
      },
    ios: [
      {
        pfx: 'xxx.p12',  // ชือ file .p12
        bundleId: 'xx.xxx',  // ชือ package name
        production: false
      }
    ]
  }
});

Test send push 
curl -X POST \
  -H "X-Parse-Application-Id: ${APPLICATION_ID}" \
  -H "X-Parse-REST-API-Key: ${REST_API_KEY}" \
  -H "Content-Type: application/json" \
  -d '{
        "where": {
          "deviceType": "ios"
        },
        "data": {
          "alert": "Hello World!"
        }
      }' \
  https://api.parse.com/1/push

วันจันทร์ที่ 23 พฤษภาคม พ.ศ. 2559

Heroku  deploy Parse-dashboard
Step 
  1. Login heroku
  2. Create New App
  3. ...
  4. settings->Reveal Config Vars  add PARSE_DASHBOARD_ALLOW_INSECURE_HTTP = 1
  5. download https://github.com/ParsePlatform/parse-dashboard to local add cd  parse-dashboard/Parse-Dashboard/public  run command npm install
  6. edit your .gitignore file and remove the following lines   (parse-dashboard/.gitignore)
bundles/
Parse-Dashboard/public/bundles/
Parse-Dashboard/parse-dashboard-config.json

   7. edit your parse-dashboard-config.json (parse-dashboard/Parse-Dashboard/)
                {
  "apps": [
    {
        "serverURL": "http://url-to-your-parse-server/parse", // If your Parse Server's endpoint is not at /parse, you need to replace /parse with the correct endpoint
        "appId": "your-app-Id",
        "masterKey": "your-master-key",
        "appName": "your-app-name"
    }
  ],
  "users": [
    {
      "user":"username1", // Used to log in to your Parse Dashboard
      "pass":"password1"
    }
  ]
}

  1. open/edit Procfile add web: bin/parse-dashboard --config Parse-Dashboard/parse-dashboard-config.json --allowInsecureHTTP

   9. deploy to heroku